1、Flik關係型api概念
最底層的Stateful event driven application是最難以使用的,因爲接近底層;中層的DataStream api和DataSet api是對於開發人員來說是可以接受的;最上層的High-level編程格式是對各個程序員來說基本都會使用的,基於SQL的操作,同時也是流處理和批處理的一種統一的高級API。
注意:(目前博客是1.9版本)Table API和SQL尚未完成功能,正在積極開發中。[Table API,SQL]和[stream,batch]輸入的每種組合都不支持所有操作。請嚴謹使用。
使用之前需要添加pom依賴
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-api-scala-bridge_2.11</artifactId>
<version>1.9.0</version>
</dependency>
此外,如果想在IDE中本地運行表API和SQL程序,必須添加以下依賴之一
<!-- Either... (for the old planner that was available before Flink 1.9) -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner_2.11</artifactId>
<version>1.9.0</version>
<scope>provided</scope>
</dependency>
<!-- or.. (for the new Blink planner) -->
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner-blink_2.11</artifactId>
<version>1.9.0</version>
<scope>provided</scope>
</dependency>
使用Table API和SQL
源文件
name,age,job
zhangsan,30,Developer
lisi,32,Developer
邏輯處理
package com.kun.flink.chapter06
import org.apache.flink.api.scala._
import org.apache.flink.table.api.scala.BatchTableEnvironment
import org.apache.flink.types.Row
object TableSQLAPI {
def main(args: Array[String]): Unit = {
val fbEnv = ExecutionEnvironment.getExecutionEnvironment
val fbTableEnv = BatchTableEnvironment.create(fbEnv)
val filePath = "test_files/test_csv/test01.csv"
//拿到DataSet
val csv = fbEnv.readCsvFile[People](filePath,ignoreFirstLine=true)
//DataSet轉爲Table
val salesTable = fbTableEnv.fromDataSet(csv)
//Table--table
fbTableEnv.registerTable("sales",salesTable)
//sql
val resultTable = fbTableEnv.sqlQuery("select * from sales")
fbTableEnv.toDataSet[Row](resultTable).print()
}
case class People(name:String,
age:Int,
job:String)
}
結果
zhangsan,30,Developer
lisi,32,Developer