使用Flink Table & SQL实现WordCount功能
1.添加依赖
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-api-java-bridge_2.11</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner-blink_2.11</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-planner_2.11</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-table-api-scala-bridge_2.11</artifactId>
<version>1.10.0</version>
</dependency>
2.编写代码
package org.myorg.quickstart;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.java.BatchTableEnvironment;
import java.util.ArrayList;
public class WordCountSQL {
public static void main(String[] args) throws Exception{
//获取运行环境
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
//创建一个tableEnvironment
BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env);
//读取一行模拟数据作为输入
String words="a a b a c d e d b";
String[] split=words.split("\\s");
ArrayList<WordWithCount> list = new ArrayList<>();
for (String word:split){
WordWithCount wc = new WordWithCount(word, 1);
list.add(wc);
}
DataSet<WordWithCount> data = env.fromCollection(list);
//DataSet转sql,指定字段名
Table table = tableEnv.fromDataSet(data, "word,frequency");
//把表结构输出到控制台
table.printSchema();
//注册为一个表
tableEnv.createTemporaryView("WordCount",table);
Table table1 = tableEnv.sqlQuery("select word,sum(frequency) as frequency from WordCount group by word");
//将表转换成DataSet
DataSet<WordWithCount> data1 = tableEnv.toDataSet(table1, WordWithCount.class);
data1.printToErr();
}
public static class WordWithCount {
public String word;
public long frequency;
public WordWithCount() {
}
public WordWithCount(String word, long frequency) {
this.word = word;
this.frequency = frequency;
}
@Override
public String toString() {
return "WordWithCount{" +
"word='" + word + '\'' +
", frequency='" + frequency + '\'' +
'}';
}
}
}
遇到问题:
启动程序报错:
Error:(5, 34) java: 程序包org.apache.flink.table.api不存在
在Terminal中输入mvn idea:idea
注意事项:
sql查询列要命名成类相同的名字,不然无法将表转换成DataSet