flink fromSource
时间: 2023-09-09 16:13:10 浏览: 77
`fromSource` is a method in Apache Flink that allows you to create a data stream from a given source. This method is used to specify the source of data for your Flink job. The source can be any data source supported by Flink, such as Apache Kafka, Apache Pulsar, or Hadoop Distributed File System (HDFS).
Here is an example of how to use `fromSource` to create a data stream from a Kafka topic in Flink:
```
import org.apache.flink.streaming.api.scala._
val env = StreamExecutionEnvironment.getExecutionEnvironment
val kafkaProps = new Properties()
kafkaProps.setProperty("bootstrap.servers", "localhost:9092")
kafkaProps.setProperty("group.id", "my-group")
val kafkaSource = new FlinkKafkaConsumer[String]("my-topic", new SimpleStringSchema(), kafkaProps)
val stream = env.addSource(kafkaSource)
stream.print()
env.execute("Flink Job")
```
In this example, we create a Kafka consumer using the `FlinkKafkaConsumer` class and pass it to the `fromSource` method using the `addSource` method. We then print the data stream and execute the Flink job.
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)