Unverified Commit adcafb26 authored by wuzhenhua's avatar wuzhenhua Committed by GitHub

优化 cdc, filter to process (#1722)

parent 7ef628b9
...@@ -26,15 +26,15 @@ import org.dinky.model.FlinkCDCConfig; ...@@ -26,15 +26,15 @@ import org.dinky.model.FlinkCDCConfig;
import org.dinky.model.Schema; import org.dinky.model.Schema;
import org.dinky.model.Table; import org.dinky.model.Table;
import org.dinky.utils.JSONUtil; import org.dinky.utils.JSONUtil;
import org.dinky.utils.LogUtil;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction; import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper; import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource; import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator; import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.table.data.DecimalData; import org.apache.flink.table.data.DecimalData;
import org.apache.flink.table.data.GenericRowData; import org.apache.flink.table.data.GenericRowData;
import org.apache.flink.table.data.RowData; import org.apache.flink.table.data.RowData;
...@@ -63,10 +63,12 @@ import java.sql.Timestamp; ...@@ -63,10 +63,12 @@ import java.sql.Timestamp;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -111,41 +113,46 @@ public abstract class AbstractSinkBuilder implements SinkBuilder { ...@@ -111,41 +113,46 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
return properties; return properties;
} }
protected SingleOutputStreamOperator<Map> deserialize( protected DataStream<Map> deserialize(DataStreamSource<String> dataStreamSource) {
DataStreamSource<String> dataStreamSource) {
return dataStreamSource.map( return dataStreamSource.map(
new MapFunction<String, Map>() { value -> {
@Override ObjectMapper objectMapper = new ObjectMapper();
public Map map(String value) throws Exception { return objectMapper.readValue(value, Map.class);
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(value, Map.class);
}
}); });
} }
protected SingleOutputStreamOperator<Map> shunt( protected DataStream<Map> shunt(
SingleOutputStreamOperator<Map> mapOperator, Table table, String schemaFieldName) { SingleOutputStreamOperator<Map> processOperator, OutputTag<Map> tag) {
final String tableName = table.getName(); return processOperator.getSideOutput(tag);
final String schemaName = table.getSchema(); }
return mapOperator.filter(
new FilterFunction<Map>() { protected SingleOutputStreamOperator<Map> tag(
DataStream<Map> mapOperator,
Map<String, Table> tableMap,
String schemaFieldName,
Map<Table, OutputTag<Map>> tagMap) {
return mapOperator.process(
new ProcessFunction<Map, Map>() {
@Override @Override
public boolean filter(Map value) throws Exception { public void processElement(Map map, Context ctx, Collector<Map> out) {
LinkedHashMap source = (LinkedHashMap) value.get("source"); Map source = (Map) map.get("source");
return tableName.equals(source.get("table").toString()) try {
&& schemaName.equals(source.get(schemaFieldName).toString()); String schemaTableName =
source.get(schemaFieldName).toString()
+ "."
+ source.get("table").toString();
Table table = tableMap.get(schemaTableName);
OutputTag<Map> outputTag = tagMap.get(table);
ctx.output(outputTag, map);
} catch (Exception e) {
out.collect(map);
}
} }
}); });
} }
protected DataStream<Map> shunt(
SingleOutputStreamOperator<Map> processOperator, Table table, OutputTag<Map> tag) {
return processOperator.getSideOutput(tag);
}
protected DataStream<RowData> buildRowData( protected DataStream<RowData> buildRowData(
SingleOutputStreamOperator<Map> filterOperator, DataStream<Map> filterOperator,
List<String> columnNameList, List<String> columnNameList,
List<LogicalType> columnTypeList, List<LogicalType> columnTypeList,
String schemaTableName) { String schemaTableName) {
...@@ -241,27 +248,68 @@ public abstract class AbstractSinkBuilder implements SinkBuilder { ...@@ -241,27 +248,68 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
final String schemaFieldName = config.getSchemaFieldName(); final String schemaFieldName = config.getSchemaFieldName();
if (Asserts.isNotNullCollection(schemaList)) { if (Asserts.isNotNullCollection(schemaList)) {
SingleOutputStreamOperator<Map> mapOperator = deserialize(dataStreamSource); Map<String, Table> tableMap =
for (Schema schema : schemaList) { schemaList.stream()
for (Table table : schema.getTables()) { .map(Schema::getTables)
SingleOutputStreamOperator<Map> filterOperator = .flatMap(List::stream)
shunt(mapOperator, table, schemaFieldName); .collect(
Collectors.toMap(
List<String> columnNameList = new ArrayList<>(); Table::getSchemaTableName, Function.identity()));
List<LogicalType> columnTypeList = new ArrayList<>();
Map<Table, OutputTag<Map>> tagMap =
buildColumn(columnNameList, columnTypeList, table.getColumns()); schemaList.stream()
.map(Schema::getTables)
DataStream<RowData> rowDataDataStream = .flatMap(List::stream)
buildRowData( .collect(
filterOperator, Collectors.toMap(
columnNameList, Function.identity(),
columnTypeList, table ->
table.getSchemaTableName()); new OutputTag<Map>(
getSinkTableName(table)) {}));
Map<Table, List<String>> columnNameMap = new HashMap<>();
Map<Table, List<LogicalType>> columnTypeMap = new HashMap<>();
schemaList.stream()
.map(Schema::getTables)
.flatMap(List::stream)
.forEach(
table -> {
List<String> columnNameList = new ArrayList<>();
List<LogicalType> columnTypeList = new ArrayList<>();
buildColumn(columnNameList, columnTypeList, table.getColumns());
columnNameMap.put(table, columnNameList);
columnTypeMap.put(table, columnTypeList);
});
DataStream<Map> mapOperator = deserialize(dataStreamSource);
SingleOutputStreamOperator<Map> tagOperator =
tag(mapOperator, tableMap, schemaFieldName, tagMap);
tagMap.forEach(
(table, tag) -> {
final String schemaTableName = table.getSchemaTableName();
List<String> columnNameList = columnNameMap.get(table);
List<LogicalType> columnTypeList = columnTypeMap.get(table);
addSink(env, rowDataDataStream, table, columnNameList, columnTypeList); try {
} DataStream<Map> filterOperator = shunt(tagOperator, tag);
} logger.info("Build {} shunt successful...", schemaTableName);
DataStream<RowData> rowDataDataStream =
buildRowData(
filterOperator,
columnNameList,
columnTypeList,
schemaTableName)
.rebalance();
logger.info("Build {} flatMap successful...", schemaTableName);
addSink(env, rowDataDataStream, table, columnNameList, columnTypeList);
logger.info("Build {} sink successful...", schemaTableName);
} catch (Exception e) {
logger.error("Build " + schemaTableName + " cdc sync failed...");
logger.error(LogUtil.getError(e));
}
});
} }
return dataStreamSource; return dataStreamSource;
} }
......
...@@ -36,12 +36,10 @@ import org.apache.flink.api.common.functions.FlatMapFunction; ...@@ -36,12 +36,10 @@ import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.TypeInformation; import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.dag.Transformation; import org.apache.flink.api.dag.Transformation;
import org.apache.flink.api.java.typeutils.RowTypeInfo; import org.apache.flink.api.java.typeutils.RowTypeInfo;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource; import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator; import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.table.data.RowData; import org.apache.flink.table.data.RowData;
import org.apache.flink.table.operations.ModifyOperation; import org.apache.flink.table.operations.ModifyOperation;
import org.apache.flink.table.operations.Operation; import org.apache.flink.table.operations.Operation;
...@@ -63,9 +61,10 @@ import java.time.Instant; ...@@ -63,9 +61,10 @@ import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
...@@ -245,59 +244,56 @@ public class SQLSinkBuilder extends AbstractSinkBuilder implements Serializable ...@@ -245,59 +244,56 @@ public class SQLSinkBuilder extends AbstractSinkBuilder implements Serializable
} }
final List<Schema> schemaList = config.getSchemaList(); final List<Schema> schemaList = config.getSchemaList();
if (Asserts.isNotNullCollection(schemaList)) { if (Asserts.isNotNullCollection(schemaList)) {
Map<String, Table> tableMap =
schemaList.stream()
.map(Schema::getTables)
.flatMap(List::stream)
.collect(
Collectors.toMap(
Table::getSchemaTableName, Function.identity()));
logger.info("Build deserialize successful..."); Map<Table, OutputTag<Map>> tagMap =
Map<Table, OutputTag<Map>> tagMap = new HashMap<>(); schemaList.stream()
Map<String, Table> tableMap = new HashMap<>(); .map(Schema::getTables)
for (Schema schema : schemaList) { .flatMap(List::stream)
for (Table table : schema.getTables()) { .collect(
String sinkTableName = getSinkTableName(table); Collectors.toMap(
OutputTag<Map> outputTag = new OutputTag<Map>(sinkTableName) {}; Function.identity(),
tagMap.put(table, outputTag); table ->
new OutputTag<Map>(
tableMap.put(table.getSchemaTableName(), table); getSinkTableName(table)) {}));
}
}
final String schemaFieldName = config.getSchemaFieldName();
ObjectMapper objectMapper = new ObjectMapper();
SingleOutputStreamOperator<Map> mapOperator =
dataStreamSource
.map(x -> objectMapper.readValue(x, Map.class))
.returns(Map.class);
SingleOutputStreamOperator<Map> processOperator = Map<Table, List<String>> columnNameMap = new HashMap<>();
mapOperator.process( Map<Table, List<LogicalType>> columnTypeMap = new HashMap<>();
new ProcessFunction<Map, Map>() { schemaList.stream()
.map(Schema::getTables)
.flatMap(List::stream)
.forEach(
table -> {
List<String> columnNameList = new ArrayList<>();
List<LogicalType> columnTypeList = new ArrayList<>();
buildColumn(columnNameList, columnTypeList, table.getColumns());
@Override columnNameMap.put(table, columnNameList);
public void processElement( columnTypeMap.put(table, columnTypeList);
Map map,
ProcessFunction<Map, Map>.Context ctx,
Collector<Map> out)
throws Exception {
LinkedHashMap source = (LinkedHashMap) map.get("source");
try {
Table table =
tableMap.get(
source.get(schemaFieldName).toString()
+ "."
+ source.get("table").toString());
OutputTag<Map> outputTag = tagMap.get(table);
ctx.output(outputTag, map);
} catch (Exception e) {
out.collect(map);
}
}
}); });
final String schemaFieldName = config.getSchemaFieldName();
DataStream<Map> mapOperator = deserialize(dataStreamSource);
logger.info("Build deserialize successful...");
SingleOutputStreamOperator<Map> tagOperator =
tag(mapOperator, tableMap, schemaFieldName, tagMap);
logger.info("Build tag successful...");
tagMap.forEach( tagMap.forEach(
(table, tag) -> { (table, tag) -> {
final String schemaTableName = table.getSchemaTableName(); final String schemaTableName = table.getSchemaTableName();
List<String> columnNameList = columnNameMap.get(table);
List<LogicalType> columnTypeList = columnTypeMap.get(table);
try { try {
DataStream<Map> filterOperator = shunt(processOperator, table, tag); DataStream<Map> filterOperator = shunt(tagOperator, tag);
logger.info("Build " + schemaTableName + " shunt successful..."); logger.info("Build {} shunt successful...", schemaTableName);
List<String> columnNameList = new ArrayList<>();
List<LogicalType> columnTypeList = new ArrayList<>();
buildColumn(columnNameList, columnTypeList, table.getColumns());
DataStream<Row> rowDataDataStream = DataStream<Row> rowDataDataStream =
buildRow( buildRow(
filterOperator, filterOperator,
...@@ -305,13 +301,13 @@ public class SQLSinkBuilder extends AbstractSinkBuilder implements Serializable ...@@ -305,13 +301,13 @@ public class SQLSinkBuilder extends AbstractSinkBuilder implements Serializable
columnTypeList, columnTypeList,
schemaTableName) schemaTableName)
.rebalance(); .rebalance();
logger.info("Build " + schemaTableName + " flatMap successful..."); logger.info("Build {} flatMap successful...", schemaTableName);
logger.info("Start build " + schemaTableName + " sink...");
addTableSink( addTableSink(
customTableEnvironment, customTableEnvironment,
rowDataDataStream, rowDataDataStream,
table, table,
columnNameList); columnNameList);
logger.info("Build {} sink successful..., schemaTableName");
} catch (Exception e) { } catch (Exception e) {
logger.error("Build " + schemaTableName + " cdc sync failed..."); logger.error("Build " + schemaTableName + " cdc sync failed...");
logger.error(LogUtil.getError(e)); logger.error(LogUtil.getError(e));
......
...@@ -29,6 +29,7 @@ import org.dinky.model.FlinkCDCConfig; ...@@ -29,6 +29,7 @@ import org.dinky.model.FlinkCDCConfig;
import org.dinky.model.Schema; import org.dinky.model.Schema;
import org.dinky.model.Table; import org.dinky.model.Table;
import org.dinky.utils.JSONUtil; import org.dinky.utils.JSONUtil;
import org.dinky.utils.LogUtil;
import org.apache.flink.api.common.functions.FlatMapFunction; import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.TypeInformation; import org.apache.flink.api.common.typeinfo.TypeInformation;
...@@ -49,15 +50,19 @@ import org.apache.flink.table.types.logical.VarBinaryType; ...@@ -49,15 +50,19 @@ import org.apache.flink.table.types.logical.VarBinaryType;
import org.apache.flink.table.types.utils.TypeConversions; import org.apache.flink.table.types.utils.TypeConversions;
import org.apache.flink.types.RowKind; import org.apache.flink.types.RowKind;
import org.apache.flink.util.Collector; import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
...@@ -95,7 +100,7 @@ public class StarrocksSinkBuilder extends AbstractSinkBuilder implements SinkBui ...@@ -95,7 +100,7 @@ public class StarrocksSinkBuilder extends AbstractSinkBuilder implements SinkBui
DataStreamSource<String> dataStreamSource) { DataStreamSource<String> dataStreamSource) {
final List<Schema> schemaList = config.getSchemaList(); final List<Schema> schemaList = config.getSchemaList();
if (Asserts.isNotNullCollection(schemaList)) { if (Asserts.isNotNullCollection(schemaList)) {
SingleOutputStreamOperator<Map> mapOperator = deserialize(dataStreamSource); DataStream<Map> mapOperator = deserialize(dataStreamSource);
String mergeFlag = config.getSink().get("sink.merge.table"); String mergeFlag = config.getSink().get("sink.merge.table");
if ("true".equalsIgnoreCase(mergeFlag)) { if ("true".equalsIgnoreCase(mergeFlag)) {
// 取第一个table // 取第一个table
...@@ -112,26 +117,71 @@ public class StarrocksSinkBuilder extends AbstractSinkBuilder implements SinkBui ...@@ -112,26 +117,71 @@ public class StarrocksSinkBuilder extends AbstractSinkBuilder implements SinkBui
addSink(env, rowDataDataStream, table, columnNameList, columnTypeList); addSink(env, rowDataDataStream, table, columnNameList, columnTypeList);
} else { } else {
final String schemaFieldName = config.getSchemaFieldName(); final String schemaFieldName = config.getSchemaFieldName();
for (Schema schema : schemaList) { Map<String, Table> tableMap =
for (Table table : schema.getTables()) { schemaList.stream()
SingleOutputStreamOperator<Map> filterOperator = .map(Schema::getTables)
shunt(mapOperator, table, schemaFieldName); .flatMap(List::stream)
.collect(
Collectors.toMap(
Table::getSchemaTableName, Function.identity()));
List<String> columnNameList = new ArrayList<>(); Map<Table, OutputTag<Map>> tagMap =
List<LogicalType> columnTypeList = new ArrayList<>(); schemaList.stream()
.map(Schema::getTables)
.flatMap(List::stream)
.collect(
Collectors.toMap(
Function.identity(),
table ->
new OutputTag<Map>(
getSinkTableName(table)) {}));
buildColumn(columnNameList, columnTypeList, table.getColumns()); Map<Table, List<String>> columnNameMap = new HashMap<>();
Map<Table, List<LogicalType>> columnTypeMap = new HashMap<>();
schemaList.stream()
.map(Schema::getTables)
.flatMap(List::stream)
.forEach(
table -> {
List<String> columnNameList = new ArrayList<>();
List<LogicalType> columnTypeList = new ArrayList<>();
buildColumn(columnNameList, columnTypeList, table.getColumns());
DataStream<RowData> rowDataDataStream = columnNameMap.put(table, columnNameList);
buildRowData( columnTypeMap.put(table, columnTypeList);
filterOperator, });
columnNameList,
columnTypeList,
table.getSchemaTableName());
addSink(env, rowDataDataStream, table, columnNameList, columnTypeList); SingleOutputStreamOperator<Map> tagOperator =
} tag(mapOperator, tableMap, schemaFieldName, tagMap);
} tagMap.forEach(
(table, tag) -> {
final String schemaTableName = table.getSchemaTableName();
List<String> columnNameList = columnNameMap.get(table);
List<LogicalType> columnTypeList = columnTypeMap.get(table);
try {
DataStream<Map> filterOperator = shunt(tagOperator, tag);
logger.info("Build {} shunt successful...", schemaTableName);
DataStream<RowData> rowDataDataStream =
buildRowData(
filterOperator,
columnNameList,
columnTypeList,
schemaTableName)
.rebalance();
logger.info("Build {} flatMap successful...", schemaTableName);
addSink(
env,
rowDataDataStream,
table,
columnNameList,
columnTypeList);
logger.info("Build {} sink successful..., schemaTableName");
} catch (Exception e) {
logger.error("Build " + schemaTableName + " cdc sync failed...");
logger.error(LogUtil.getError(e));
}
});
} }
} }
return dataStreamSource; return dataStreamSource;
...@@ -247,7 +297,7 @@ public class StarrocksSinkBuilder extends AbstractSinkBuilder implements SinkBui ...@@ -247,7 +297,7 @@ public class StarrocksSinkBuilder extends AbstractSinkBuilder implements SinkBui
} }
protected DataStream<RowData> buildRowData( protected DataStream<RowData> buildRowData(
SingleOutputStreamOperator<Map> filterOperator, DataStream<Map> filterOperator,
List<String> columnNameList, List<String> columnNameList,
List<LogicalType> columnTypeList, List<LogicalType> columnTypeList,
String schemaTableName) { String schemaTableName) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment