FLINK 1.12 支持upsertSql 不再去使用了JDBCUpsertSINK了,kafka也支持upsert了

Stella981
• 阅读 622
package com.konka.dsp;


import org.apache.flink.api.common.JobExecutionResult;
import org.apache.flink.api.common.restartstrategy.RestartStrategies;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.connector.jdbc.dialect.MySQLDialect;
import org.apache.flink.connector.jdbc.internal.options.JdbcOptions;
import org.apache.flink.connector.jdbc.table.JdbcUpsertTableSink;
import org.apache.flink.formats.json.JsonFormatFactory;
import org.apache.flink.formats.json.canal.CanalJsonFormatFactory;
import org.apache.flink.shaded.curator4.org.apache.curator.framework.schema.Schema;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.*;
import org.apache.flink.table.api.bridge.java.BatchTableEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.expressions.TimeIntervalUnit;
import org.apache.flink.table.types.DataType;
import org.apache.flink.types.Row;
import org.apache.flink.util.CloseableIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.time.Duration;
import java.util.concurrent.ExecutionException;

import static org.apache.flink.table.api.Expressions.*;


public class SalesOrderStream {

    private static Logger log = LoggerFactory.getLogger(SalesOrderStream.class.getName());
    public static Table report(Table transactions) {

        return transactions.select(
                $("customer_name"),
                $("created_date"),
                $("total_amount"))
                .groupBy($("customer_name"),$("created_date"))
                .select(
                        $("customer_name"),
                        $("total_amount").sum().as("total_amount"),
                        $("created_date")
                        );

    }

    public static void main(String[] args) throws Exception {


        StreamExecutionEnvironment env = StreamExecutionEnvironment
                .getExecutionEnvironment();
//        env.setParallelism(4);
//        env.getConfig().setRestartStrategy(RestartStrategies.noRestart());
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);

     // set default parallelism to 4


//        tEnv.executeSql("CREATE TABLE sales_order_header_stream (\n" +
////                "   id  BIGINT not null,\n" +
//                "    customer_name STRING,\n"+
////                "    dsp_org_name STRING,\n"+
//                "    total_amount      DECIMAL(38,2),\n" +
////                "    total_discount      DECIMAL(16,2),\n" +
////                "    pay_amount      DECIMAL(16,2),\n" +
////                "    total_amount      DECIMAL(16,2),\n" +
//                "    created_date TIMESTAMP(3)\n" +
//                ") WITH (\n" +
//                " 'connector' = 'mysql-cdc',\n" +
//                " 'hostname' = '192.168.8.73',\n" +
//                " 'port' = '4000',\n"+
//                " 'username' = 'flink',\n"+
//                " 'password' = 'flink',\n"+
//                " 'database-name' = 'dspdev',\n"+
//                " 'table-name' = 'sales_order_header'\n"+
//                ")");
        //pay_type,over_sell
        tEnv.executeSql("CREATE TABLE sales_order_header_stream (\n" +
                        " `id` BIGINT,\n"+
                        " `total_amount` DECIMAL(16,2) ,\n"+
                        " `customer_name` STRING,\n"+
                        " `order_no` STRING,\n"+
                        " `doc_type` STRING,\n"+
                        " `sales_org` STRING,\n"+
                        " `distr_chan` STRING,\n"+
                        " `division` STRING,\n"+
                        " `sales_grp` STRING,\n"+
                        " `sales_off` STRING,\n"+
                        " `purch_no_c` STRING,\n"+
                        " `purch_date` STRING,\n"+
                        " `sold_to` STRING,\n"+
                        " `ship_to` STRING,\n"+
                        " `r3_sales_order` STRING,\n"+
                        " `created_by_employee_name` STRING,\n"+
                        " `created_by_dept_name` STRING,\n"+
                        " `created_by_dept_name` STRING,\n"+
                        " `is_enable` BIGINT,\n"+
                        " `is_delete` BIGINT,\n"+
                        " `sale_order_status` STRING,\n"+
                        " `created_by_parent_dept_name` STRING,\n"+
                        " `total_discount` DECIMAL(16,2),\n"+
                        " `customer_sapcode` STRING,\n"+
                        " `sold_to_name` STRING,\n"+
                        " `ship_to_name` STRING,\n"+
                        " `total_discount_amount` DECIMAL(16,2),\n"+
                        " `other_discount` DECIMAL(16,2),\n"+
                        " `other_amount` DECIMAL(16,2),\n"+
                        " `pay_amount` DECIMAL(16,2),\n"+
                        " `dsp_org_name` STRING,\n"+
                        " `delivery_address` STRING,\n"+
                        " `delivery_person` STRING,\n"+
                        " `delivery_phone` STRING,\n"+
                        " `pay_type` STRING,\n"+
                        " `over_sell` STRING,\n"+
                        " `created_date` TIMESTAMP(3),\n"+
                        " PRIMARY KEY (`id`) NOT ENFORCED "+
                ") WITH (\n" +
                "'connector' = 'kafka',\n"+
                "'topic' = 'canal-data',\n"+
                "'properties.bootstrap.servers' = '192.168.8.71:9092',\n"+
                "'properties.group.id' = 'test',\n"+
                "'format' = 'canal-json'\n"+
                ")");

//        tEnv.executeSql("CREATE TABLE total_day_report (\n" +
//                "    customer_name STRING,\n" +
////                "    total_amount    DECIMAL(16,2),\n" +
////                "    total_discount  DECIMAL(16,2),\n" +
////                "    pay_amount      DECIMAL(16,2),\n" +
//                "    total_amount    DECIMAL(16,2),\n" +
//                "    created_date STRING,\n" +
//                "    PRIMARY KEY (created_date) NOT ENFORCED" +
//                ") WITH (\n" +
//                "  'connector' = 'upsert-kafka',\n" +
//                "  'topic' = 'customer_amount',\n" +
//                "  'properties.bootstrap.servers' = '192.168.8.71:9092',\n"+
//                "  'key.format' = 'json',\n"+
//                "  'value.format' = 'json',\n"+
//                "  'value.fields-include' = 'ALL'\n"+
//                ")");



        tEnv.executeSql("CREATE TABLE upsertSink (\n" +
                "    customer_name STRING,\n" +
//                "    total_amount    DECIMAL(16,2),\n" +
//                "    total_discount  DECIMAL(16,2),\n" +
//                "    pay_amount      DECIMAL(16,2),\n" +
                "    total_amount    DECIMAL(16,2),\n" +
                "    created_date STRING,\n" +
                "    PRIMARY KEY (customer_name,created_date) NOT ENFORCED" +
                ") WITH (\n" +
                        "  'connector' = 'tidb',\n" +
                        "  'tidb.database.url' = 'jdbc:mysql://192.168.8.73:4000/dspdev',\n" +
                        "  'tidb.username' = 'flink',\n"+
                        "  'tidb.password' = 'flink',\n"+
                        "  'tidb.database.name' = 'dspdev',\n"+
                        "  'tidb.table.name' = 'spend_report'\n"+
//                "  'connector.type'='jdbc'," +
//                "  'connector.url'='jdbc:mysql://192.168.8.73:4000/dspdev',\n" +
//                "  'connector.username' = 'flink',\n"+
//                "  'connector.password' = 'flink',\n"+
//                "  'connector.table' = 'spend_report'" +
                ")");
//        TableSchema tableSche = TableSchema.builder()
//                .field("customer_name",DataTypes.STRING().notNull())
//                .field("total_amount",DataTypes.DECIMAL(16,2))
//                .field("created_date",DataTypes.STRING().notNull()).build();
//
//        JdbcOptions jdbcOptions = JdbcOptions.builder()
//                .setDBUrl("jdbc:mysql://192.168.8.73:4000/dspdev")
//                .setTableName("spend_report")
//                .setUsername("flink")
//                .setPassword("flink")
//                .setDialect(new MySQLDialect())
//                .setDriverName("com.mysql.jdbc.Driver")
//                .build();
//        JdbcUpsertTableSink jdbcUpsertTableSink = JdbcUpsertTableSink.builder()
//                .setTableSchema(tableSche)
//                .setOptions(jdbcOptions)
//                .build();
//        jdbcUpsertTableSink.setKeyFields(new String[]{"id"});
        /**
         * SINK End
         */
//        tEnv.re("spend_report",jdbcUpsertTableSink);
        Table transactions = tEnv.from("sales_order_header_stream");
//      tEnv.executeSql("delete from total_day_report");
       tEnv.executeSql("insert into upsertSink select dsp_org_name as customer_name,cast(sum(t.pay_amount) as decimal(16,2)) as amount,DATE_FORMAT(t.created_date,'yyyy-MM-dd') as created_date from sales_order_header_stream t group by DATE_FORMAT(t.created_date,'yyyy-MM-dd'),dsp_org_name").print();
//      tEnv.executeSql("insert into spend_report select * from total_day_report");
//      Table transactions = tEnv.from("total_day_report");
//      report(transactions).executeInsert("spend_report");
        tEnv.execute("-----------");
    }


}

最后数据库结果如下: FLINK 1.12 支持upsertSql 不再去使用了JDBCUpsertSINK了,kafka也支持upsert了 每次都是更新替换,这样的话省去很多麻烦,不用转datastream在处理了,而且1.12支持upsert-kafka,最后数据叠加如下: FLINK 1.12 支持upsertSql 不再去使用了JDBCUpsertSINK了,kafka也支持upsert了 upsert-kafka上面已经体现,flink越来越强大了支持!~~~

点赞
收藏
评论区
推荐文章
浅梦一笑 浅梦一笑
2个月前
初学 Python 需要安装哪些软件?超级实用,小白必看!
编程这个东西是真的奇妙。对于懂得的人来说,会觉得这个工具是多么的好用、有趣,而对于小白来说,就如同大山一样。其实这个都可以理解,大家都是这样过来的。那么接下来就说一下python相关的东西吧,并说一下我对编程的理解。本人也是小白一名,如有不对的地方,还请各位大神指出01名词解释:如果在编程方面接触的比较少,那么对于软件这一块,有几个名词一定要了解,比如开发环
光头强的博客 光头强的博客
2个月前
Java面向对象试题
1、 请创建一个Animal动物类,要求有方法eat()方法,方法输出一条语句“吃东西”。 创建一个接口A,接口里有一个抽象方法fly()。创建一个Bird类继承Animal类并实现 接口A里的方法输出一条有语句“鸟儿飞翔”,重写eat()方法输出一条语句“鸟儿 吃虫”。在Test类中向上转型创建b对象,调用eat方法。然后向下转型调用eat()方
刚刚好 刚刚好
2个月前
css问题
1、 在IOS中图片不显示(给图片加了圆角或者img没有父级) <div<img src""/</div div {width: 20px; height: 20px; borderradius: 20px; overflow: h
blmius blmius
1年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录 问题 用navicat导入数据时,报错: 原因这是因为当前的MySQL不支持datetime为0的情况。 解决修改sql\mode: sql\mode:SQL Mode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。 全局s
晴空闲云 晴空闲云
2个月前
css中box-sizing解放盒子实际宽高计算
我们知道传统的盒子模型,如果增加内边距padding和边框border,那么会撑大整个盒子,造成盒子的宽度不好计算,在实务中特别不方便。boxsizing可以设置盒模型的方式,可以很好的设置固定宽高的盒模型。 盒子宽高计算假如我们设置如下盒子:宽度和高度均为200px,那么这会这个盒子实际的宽高就都是200px。但是当我们设置这个盒子的边框和内间距的时候,那
艾木酱 艾木酱
1个月前
快速入门|使用MemFire Cloud构建React Native应用程序
> MemFire Cloud是一款提供云数据库,用户可以创建云数据库,并对数据库进行管理,还可以对数据库进行备份操作。它还提供后端即服务,用户可以在1分钟内新建一个应用,使用自动生成的API和SDK,访问云数据库、对象存储、用户认证与授权等功能,可专
Wesley13 Wesley13
1年前
MySQL查询按照指定规则排序
1.按照指定(单个)字段排序 select * from table_name order id desc; 2.按照指定(多个)字段排序 select * from table_name order id desc,status desc; 3.按照指定字段和规则排序 selec
Stella981 Stella981
1年前
Angular material mat
Icon Icon Name mat-icon code _add\_comment_ add comment icon <mat-icon> add\_comment</mat-icon> _attach\_file_ attach file icon <mat-icon> attach\_file</mat-icon> _attach\
Wesley13 Wesley13
1年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
#### 背景描述 # Time: 2019-01-24T00:08:14.705724+08:00 # User@Host: **[**] @ [**] Id: ** # Schema: sentrymeta Last_errno: 0 Killed: 0 # Query_time: 0.315758 Lock_
helloworld_34035044 helloworld_34035044
4个月前
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。 uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid() 或 uuid(sep)参数说明:sep 布尔值,生成的uuid中是否包含分隔符'',缺省为
helloworld_28799839 helloworld_28799839
2个月前
常用知识整理
# Javascript ## 判断对象是否为空 ```js Object.keys(myObject).length === 0 ``` ## 经常使用的三元运算 > 我们经常遇到处理表格列状态字段如 `status` 的时候可以用到 ``` vue