table
package com.ccj.pxj.wy.zhaoq.sql
import org.apache.flink.api.scala._
import org.apache.flink.table.api.{Table, TableEnvironment}
object WordCountBatchTable {
case class WordCount(word:String,frequency:Int)
def main(args: Array[String]): Unit = {
//创建一个运行环境执行DataSet API
val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment
val tableEnv = TableEnvironment.getTableEnvironment(env)
val source: DataSet[String] = env.fromElements("I love Beijing", "I love China", "Beijing is the capital of China")
val datas: DataSet[WordCount] = source.flatMap(_.split(" ")).map(x=>WordCount(x.toString,1))
source.flatMap(datas => {
(datas.split(" ").map(x => WordCount(x, 1)))
}).print()
println("--------------------11111---")
datas.print()
//创建一张表
val table: Table = tableEnv.fromDataSet(datas)
val tableResult: Table = table.groupBy("word").select("word,frequency.sum as frequency")
//转换回DataSet
println("-----------------------------------")
tableEnv.toDataSet[WordCount](tableResult).print()
}
}
package com.ccj.pxj.wy.zhaoq.sql
import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.api.{Table, TableEnvironment}
import org.apache.flink.table.api.scala.StreamTableEnvironment
object WordCountStreamTable {
case class WordCount(word:String,tms:Int)
def main(args: Array[String]): Unit = {
val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
val tableEnv: StreamTableEnvironment = TableEnvironment.getTableEnvironment(env)
val sockData: DataStream[String] = env.socketTextStream("192.168.25.52", 1234)
import org.apache.flink.api.scala._
val sockDatas: DataStream[String] = sockData.flatMap(_.split(" "))
//.map(x => WordCount(x, 1))
val dataStream: DataStream[WordCount] = sockDatas.map(x => {
WordCount(x, 1)
})
val table = tableEnv.fromDataStream(dataStream)
val tableResult: Table = table.groupBy("word").select("word,tms.sum as tms ")
val result: DataStream[(Boolean, WordCount)] = tableEnv.toRetractStream[WordCount](tableResult)
result.print()
env.execute("pxj")
}
}
SQL
package com.ccj.pxj.wy.zhaoq.sql
import org.apache.flink.api.scala._
import org.apache.flink.table.api.{Table, TableEnvironment}
import org.apache.flink.table.api.scala.BatchTableEnvironment
object WordCountBatchSQL {
def main(args: Array[String]): Unit = {
val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment
val tableEnv: BatchTableEnvironment = TableEnvironment.getTableEnvironment(env)
val datsSet = env.fromElements("I love Beijing", "I love China", "Beijing is the capital of China")
val datss: DataSet[WordCount] = datsSet.flatMap(dats => {
dats.split(" ").map(x => WordCount(x, 1))
})
//注册一张表
tableEnv.registerDataSet("t",datss)
val table: Table = tableEnv.sqlQuery("select word,sum(tms) as tms from t group by word")
tableEnv.toDataSet[WordCount](table).print()
}
}
case class WordCount(word :String ,tms:Int)
package com.ccj.pxj.wy.zhaoq.sql
import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.api.{Table, TableEnvironment}
object WordCountStreamSQL {
def main(args: Array[String]): Unit = {
val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
val tableEnv = TableEnvironment.getTableEnvironment(env)
val dataStream: DataStream[String] =env.socketTextStream("192.168.25.52", 1234)
import org.apache.flink.api.scala._
val datas: DataStream[WordCount] = dataStream.flatMap(_.split(" ")).map(x => WordCount(x, 1))
// 注册一张表
val table: Table = tableEnv.fromDataStream[WordCount](datas)
val tableResult: Table = tableEnv.sqlQuery("select word,sum(tms) from " + table + " group by word")
val result: DataStream[(Boolean, WordCount)] = tableEnv.toRetractStream[WordCount](tableResult)
result.print()
env.execute("pxj")
}
}
case class WordCount(word:String,tms:Int)
作者:pxj
日期:2021-07-21