1_gbl

//5.创建MySQL CDC Source

DebeziumSourceFunction<String> sourceFunction = MySQLSource.<String>builder()

        .hostname("hadoop102")

        .port(3306)

        .username("root")

        .password("000000")

        .databaseList("gmall2021-realtime")

        .tableList("gmall2021-realtime.table_process")

        .deserializer(new DebeziumDeserializationSchema<String>() {

            //反序列化方法

            @Override

            public void deserialize(SourceRecord sourceRecord, Collector<String> collector) throws Exception {

                //库名&表名

                String topic = sourceRecord.topic();

                String[] split = topic.split("\\.");

                String db = split[1];

                String table = split[2];

                //获取数据

                Struct value = (Struct) sourceRecord.value();

                Struct after = value.getStruct("after");

                JSONObject data = new JSONObject();

                if (after != null) {

                    Schema schema = after.schema();

                    for (Field field : schema.fields()) {

                        data.put(field.name(), after.get(field.name()));

                    }

                }

                //获取操作类型

                Envelope.Operation operation = Envelope.operationFor(sourceRecord);

                //创建JSON用于存放最终的结果

                JSONObject result = new JSONObject();

                result.put("database", db);

                result.put("table", table);

                result.put("type", operation.toString().toLowerCase());

                result.put("data", data);

                collector.collect(result.toJSONString());

            }

            //定义数据类型

            @Override

            public TypeInformation<String> getProducedType() {

                return TypeInformation.of(String.class);

            }

        })

        .build();

//6.读取MySQL数据

DataStreamSource<String> tableProcessDS = env.addSource(sourceFunction);

//7.将配置信息流作为广播流

MapStateDescriptor<String, TableProcess> mapStateDescriptor = new MapStateDescriptor<>("table-process-state", String.class, TableProcess.class);

BroadcastStream<String> broadcastStream = tableProcessDS.broadcast(mapStateDescriptor);

//8.将主流和广播流进行链接

BroadcastConnectedStream<JSONObject, String> connectedStream = filterDS.connect(broadcastStream);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值