flink的SQL入门二

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值