本文的基础环境可以参考flink 1.10.1 java版本wordcount演示 (nc + socket)
本案例只是对table api的简单测试,对table api和sql有一个最基本的了解。sql语句执行时针对的是每次接受到的一个数据,在实际业务环境中,不会这样操作的。
1. 添加依赖
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner_2.11</artifactId>
<version>1.10.1</version>
</dependency>
不同版本,这里可能需要添加到包不同
2. 程序代码
package com.demo.sql;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.java.StreamTableEnvironment;
import org.apache.flink.types.Row;
public class FlinkSqlDemo {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
// 1. 读取数据
DataStream<String> inputStream = env.readTextFile("data/sensor.txt");
// 2. 转换成POJO
DataStream<SensorData> dataStream = inputStream.map(line -> {
String[] fields = line.split(",");
return new SensorData(fields[0], new Long(fields[1]), new Double(fields[2]));
});
// 3. 创建表环境
StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
// 4. 基于流创建一张表
Table dataTable = tableEnv.fromDataStream(dataStream);
// 5. 调用table API进行转换操作,这里的sql只针对每次接收到的一条数据
// select中指定的字段名称就是实体类的属性字段名
Table resultTable = dataTable.select("id, dt, temperature")
.where("id = 'sensor_1'");
// 将查询的结果添加到一个新的流中
DataStream<Row> rowDataStream = tableEnv.toAppendStream(resultTable, Row.class);
// 控制台输出流的结果
rowDataStream.print("result");
// 6. 执行SQL,这里的sql只针对每次接收到的一条数据
tableEnv.createTemporaryView("sensor", dataTable);
// select中指定的字段名称就是实体类的属性字段名
String sql = "select id, dt, temperature from sensor where id = 'sensor_1'";
Table resultSqlTable = tableEnv.sqlQuery(sql);
// 将查询的结果添加到一个新的流中
DataStream<Row> rowDataStream1 = tableEnv.toAppendStream(resultSqlTable, Row.class);
// 控制台输出流的结果
rowDataStream1.print("sql");
env.execute();
}
}
3. 辅助代码SensorData
package com.demo.sql;
public class SensorData {
private String id;
private Long dt;
private Double temperature;
public SensorData() {
}
public SensorData(String id, Long dt, Double temperature) {
this.id = id;
this.dt = dt;
this.temperature = temperature;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Long getDt() {
return dt;
}
public void setDt(Long dt) {
this.dt = dt;
}
public Double getTemperature() {
return temperature;
}
public void setTemperature(Double temperature) {
this.temperature = temperature;
}
@Override
public String toString() {
return "SensorData{" +
"id='" + id + '\'' +
", time=" + dt +
", temperature=" + temperature +
'}';
}
}
4. 测试数据
sensor_1,1547718199,35.8
sensor_6,1547718201,15.4
sensor_7,1547718202,6.7
sensor_10,1547718205,38.1
sensor_1,1547718207,36.3
sensor_1,1547718209,32.8
sensor_1,1547718212,37.1
5. 执行程序输出结果
result> sensor_1,1547718199,35.8
sql> sensor_1,1547718199,35.8
result> sensor_1,1547718207,36.3
sql> sensor_1,1547718207,36.3
result> sensor_1,1547718209,32.8
sql> sensor_1,1547718209,32.8
result> sensor_1,1547718212,37.1
sql> sensor_1,1547718212,37.1