//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);