Flink中 Table 和 DataStream 的相互转换 创建表环境 val bsEnv = StreamExecutionEnvironment.getExecutionEnvironment bsEnv.setParallelism(1) val bsSettings = EnvironmentSettings.newInstance().useBlinkPlanner().inStreamingMode().build() val bsTableEnv = StreamTableEnvironment.create(bsEnv, bsSettings) DataStream -> Table val dataStream: DataStream[TrainAlarm] = bsEnv.readTextFile("D:\\Note\\Projects\\02\\Flink\\cha01\\file\\train2.txt") .map(line => { val splits = line.split(",") TrainAlarm(splits(0), splits(1).toLong, splits(2).toDouble) }) val table1 = bsTableEnv.fromDataStream(dataStream,'id,'ts,'temp) Table -> DataStream bsTableEnv.toAppendStream[Row](table1).print() DataStream -> SQL中的表 bsTableEnv.createTemporaryView("v_train",dataStream,'id,'ts,'temp) val table2 = bsTableEnv.sqlQuery( """ select id,max(temp) max_temp from v_train group by id """.stripMargin) bsTableEnv.toRetractStream[Row](table2).print() bsEnv.execute()