栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Flink——表的创建,转换,输出

Flink——表的创建,转换,输出

读取本地CSV文件流处理,按表处理

pom


    org.apache.flink
    flink-java
    1.10.1


    org.apache.flink
    flink-streaming-java_2.12
    1.10.1


    org.apache.flink
    flink-table-planner_2.12
    1.10.1


    org.apache.flink
    flink-table-planner-blink_2.12
    1.10.1


    org.apache.flink
    flink-csv
    1.10.1
package com.yrl.table;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Csv;
import org.apache.flink.table.descriptors.FileSystem;
import org.apache.flink.table.descriptors.Schema;
import org.apache.flink.types.Row;

public class Test03 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        //1.1基于老版本planner的流处理
        EnvironmentSettings oldEnvSettings = EnvironmentSettings.newInstance().useOldPlanner().inStreamingMode().build();
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env,oldEnvSettings);


        //2.1创建表
        String path="D:\大数据组件API\Flink\Flink01\src\main\resources\test.txt";
        tableEnv.connect(new FileSystem().path(path))   //创建连接
                .withFormat(new Csv())                 //数据格式
                .withSchema(new Schema()                //数据结构,顺序一致
                        .field("id", DataTypes.STRING())
                        .field("timestamp", DataTypes.BIGINT())
                        .field("temperature", DataTypes.DOUBLE())
                ).createTemporaryTable("inputTable");

        Table inputTable = tableEnv.from("inputTable");
        inputTable.printSchema();

        //3.1Table API
        Table res1 = inputTable.select("id,temperature").where("id='sensor_6'");
        Table res2 = inputTable.groupBy("id").select("id,id.count as count,temperature.avg as tempAvg");

        //3.2SQL
        String sql ="select id ,count(*) as `count` from inputTable group by id";
        Table res3 = tableEnv.sqlQuery(sql);


        tableEnv.toAppendStream(res1, Row.class).print("res1");
        tableEnv.toRetractStream(res2, Row.class).print("res2");
        tableEnv.toRetractStream(res3, Row.class).print("res3");

        //4.1表的输出
        String path2="D:\大数据组件API\Flink\Flink01\src\main\resources\test2.csv";
        tableEnv.connect(new FileSystem().path(path2))   //创建连接
                .withFormat(new Csv())                 //数据格式
                .withSchema(new Schema()                //数据结构,顺序一致
                        .field("id", DataTypes.STRING())
                        .field("temp", DataTypes.DOUBLE())
                ).createTemporaryTable("outputTable");
        
        //res2,res3,聚合操作有更新,不支持直接写入文件
        res1.insertInto("outputTable");
        

        env.execute();


    }
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/612935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号