• Flink SQL 批处理



    <dependencies>
    <dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-table-planner_2.11</artifactId>
    <version>1.9.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-table-api-java-bridge_2.11</artifactId>
    <version>1.9.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-table-api-scala-bridge_2.11</artifactId>
    <version>1.9.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-streaming-scala_2.11</artifactId>
    <version>1.9.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-table-common</artifactId>
    <version>1.9.0</version>
    </dependency>
    </dependencies>




    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;

    public class FLinkSqlBatch {
    public static void main(String[] args) throws Exception {
    //1) 获取执行环境
    ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    BatchTableEnvironment tableEnv = BatchTableEnvironment.create(env);
    /**
    * 1.csv
    * channel,subject,refer,reg,ord,pv,uv
    * 朋友圈,三科,H5,100,100,100,100
    * 朋友圈,数学,H5,100,100,100,100
    * 朋友圈,理科,H5,100,100,100,100
    * 朋友圈,编程,H5,100,100,100,100
    * 朋友圈,英语,H5,100,100,100,100
    * 朋友圈,通用,H5,100,100,100,100
    */
    // 2)读取数据
    DataSet<AdPojo> csvInput = env
    .readCsvFile("D:\code\learn\flink-sql\src\main\resources\1.csv")
    .ignoreFirstLine() //忽略第一行
    .pojoType(AdPojo.class, "channel", "subject", "refer", "reg", "ord", "pv", "uv");

    //3)将DataSet转换为Table,并注册为table1
    Table topScore = tableEnv.fromDataSet(csvInput);
    tableEnv.registerTable("table1", topScore);

    //4)自定义sql语句
    Table groupedByCountry = tableEnv.sqlQuery("select channel,subject,refer,reg,ord,pv,uv from table1");
    //5)转换回dataset
    DataSet<AdPojo> result = tableEnv.toDataSet(groupedByCountry, AdPojo.class);
    //6)打印
    result.print();
    }
    }




    AdPojo{channel='朋友圈', subject='英语', refer='H5', reg='100', ord='100', pv='100', uv='100'}
    AdPojo{channel='朋友圈', subject='通用', refer='H5', reg='100', ord='100', pv='100', uv='100'}
    AdPojo{channel='朋友圈', subject='三科', refer='H5', reg='100', ord='100', pv='100', uv='100'}
    AdPojo{channel='朋友圈', subject='理科', refer='H5', reg='100', ord='100', pv='100', uv='100'}
    AdPojo{channel='朋友圈', subject='数学', refer='H5', reg='100', ord='100', pv='100', uv='100'}
    AdPojo{channel='朋友圈', subject='编程', refer='H5', reg='100', ord='100', pv='100', uv='100'}




  • 相关阅读:
    暴力枚举 --- 多方法求解
    图论 --- 骑士周游问题,DFS
    数论 --- 同余定理
    数论 --- 筛法求素数进一步优化
    求大素数
    codeforces --- Round #244 (Div. 2) B. Prison Transfer
    codeforces --- Round #244 (Div. 2) A. Police Recruits
    线段树 --- (区间维护+逆推)
    线段数 --- (单点更新、求逆序对)
    线段树 --- (单点更新、区间求和、模板题)
  • 原文地址:https://www.cnblogs.com/maoxiangyi/p/11586461.html
Copyright © 2020-2023  润新知