Flink学习(二)

使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值