DataSource:是程序的数据源输入,可以通过StreamExecutionEnvironment.addSource(sourceFuntion)为程序添加一个数据源
3.1、基于文件
import org.apache.flink.streaming.api.scala._
object SourceTest {
def main(args: Array[String]): Unit = {
// 创建执行环境
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
// 2. 从文件中读取数据
val resource = getClass.getResource("/test.txt")
val inputStream = env.readTextFile(resource.getPath)
// val inputPath = "D:\\Projects\\BigData\\FlinkTutorial\\src\\main\\resources\\test.txt"
val stream = env.readTextFile(inputPath)
stream.print()
// 执行
env.execute("source test")
}
}
3.2、基于socket
nc -l 7777
import org.apache.flink.streaming.api.scala._
import scala.util.Random
object SourceTest {
def main(args: Array[String]): Unit = {
// 创建执行环境
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
val stream=env.socketTextStream("localhost",7777)
stream.print()
// 执行
env.execute("source test")
}
}
3.3、集合内部
import org.apache.flink.streaming.api.scala._
object SourceTest {
def main(args: Array[String]): Unit = {
// 创建执行环境
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
// 1. 从集合中读取数据
val dataList = List(
SensorReading("id_1", 1547718199, 35.8),
SensorReading("id_6", 1547718201, 15.4),
SensorReading("id_7", 1547718202, 6.7),
SensorReading("id_10", 1547718205, 38.1)
)
val stream = env.fromCollection(dataList)
// 测试的时候可以方便使用
// env.fromElements(1.0, 35, "hello")
stream.print()
// 执行
env.execute("source test")
}
}