Unverified Commit 029d6b33 authored by Billy Xing's avatar Billy Xing Committed by GitHub

Add new sink connector 'datastream-doris-ext' for CDCSOURCE (#1242)

* Add new sink connector 'datastream-doris-ext', add 'sink.additional-columns' for CDCSOURCE doris sink, support meta column and fix value column. meta: op_ts,database_name,schema_name,table_name; fix value column type: BOOLEAN,INT,TINYINT,BIGINT,DECIMAL,FLOAT,DATE,TIMESTAMP,CHAR,VARCHAR,STRING. eg. 'sink.connector' = 'datastream-doris-ext', 'sink.timezone' = 'Asia/Shanghai', 'sink.additional-columns' = 'database_name:meta@database_name,table_name:meta@table_name,schema_name:meta@schema_name,op_ts:meta@op_ts,SiteCode:VARCHAR@SOME_FIX_TEXT', * Add new sink connector 'datastream-doris-ext' for dlink-client-1.15 Co-authored-by: 's avatarbillyxing <billyxing@ciicsh.com>
parent 9c315b5c
......@@ -105,7 +105,8 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
Properties properties = new Properties();
Map<String, String> sink = config.getSink();
for (Map.Entry<String, String> entry : sink.entrySet()) {
if (Asserts.isNotNullString(entry.getKey()) && entry.getKey().startsWith("properties") && Asserts.isNotNullString(entry.getValue())) {
if (Asserts.isNotNullString(entry.getKey()) && entry.getKey().startsWith("properties")
&& Asserts.isNotNullString(entry.getValue())) {
properties.setProperty(entry.getKey().replace("properties.", ""), entry.getValue());
}
}
......@@ -114,6 +115,7 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
protected SingleOutputStreamOperator<Map> deserialize(DataStreamSource<String> dataStreamSource) {
return dataStreamSource.map(new MapFunction<String, Map>() {
@Override
public Map map(String value) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
......@@ -129,6 +131,7 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
final String tableName = table.getName();
final String schemaName = table.getSchema();
return mapOperator.filter(new FilterFunction<Map>() {
@Override
public boolean filter(Map value) throws Exception {
LinkedHashMap source = (LinkedHashMap) value.get("source");
......@@ -153,6 +156,7 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
String schemaTableName) {
return filterOperator
.flatMap(new FlatMapFunction<Map, RowData>() {
@Override
public void flatMap(Map value, Collector<RowData> out) throws Exception {
try {
......@@ -163,7 +167,8 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
igenericRowData.setRowKind(RowKind.INSERT);
Map idata = (Map) value.get("after");
for (int i = 0; i < columnNameList.size(); i++) {
igenericRowData.setField(i, convertValue(idata.get(columnNameList.get(i)), columnTypeList.get(i)));
igenericRowData.setField(i,
convertValue(idata.get(columnNameList.get(i)), columnTypeList.get(i)));
}
out.collect(igenericRowData);
break;
......@@ -172,7 +177,8 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
dgenericRowData.setRowKind(RowKind.DELETE);
Map ddata = (Map) value.get("before");
for (int i = 0; i < columnNameList.size(); i++) {
dgenericRowData.setField(i, convertValue(ddata.get(columnNameList.get(i)), columnTypeList.get(i)));
dgenericRowData.setField(i,
convertValue(ddata.get(columnNameList.get(i)), columnTypeList.get(i)));
}
out.collect(dgenericRowData);
break;
......@@ -181,21 +187,24 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
ubgenericRowData.setRowKind(RowKind.UPDATE_BEFORE);
Map ubdata = (Map) value.get("before");
for (int i = 0; i < columnNameList.size(); i++) {
ubgenericRowData.setField(i, convertValue(ubdata.get(columnNameList.get(i)), columnTypeList.get(i)));
ubgenericRowData.setField(i,
convertValue(ubdata.get(columnNameList.get(i)), columnTypeList.get(i)));
}
out.collect(ubgenericRowData);
GenericRowData uagenericRowData = new GenericRowData(columnNameList.size());
uagenericRowData.setRowKind(RowKind.UPDATE_AFTER);
Map uadata = (Map) value.get("after");
for (int i = 0; i < columnNameList.size(); i++) {
uagenericRowData.setField(i, convertValue(uadata.get(columnNameList.get(i)), columnTypeList.get(i)));
uagenericRowData.setField(i,
convertValue(uadata.get(columnNameList.get(i)), columnTypeList.get(i)));
}
out.collect(uagenericRowData);
break;
default:
}
} catch (Exception e) {
logger.error("SchameTable: {} - Row: {} - Exception: {}", schemaTableName, JSONUtil.toJsonString(value), e);
logger.error("SchameTable: {} - Row: {} - Exception: {}", schemaTableName,
JSONUtil.toJsonString(value), e);
throw e;
}
}
......@@ -235,7 +244,8 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
buildColumn(columnNameList, columnTypeList, table.getColumns());
DataStream<RowData> rowDataDataStream = buildRowData(filterOperator, columnNameList, columnTypeList, table.getSchemaTableName());
DataStream<RowData> rowDataDataStream =
buildRowData(filterOperator, columnNameList, columnTypeList, table.getSchemaTableName());
addSink(env, rowDataDataStream, table, columnNameList, columnTypeList);
}
......@@ -305,11 +315,14 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
return value;
} else if (logicalType instanceof TimestampType) {
if (value instanceof Integer) {
return TimestampData.fromLocalDateTime(Instant.ofEpochMilli(((Integer) value).longValue()).atZone(sinkTimeZone).toLocalDateTime());
return TimestampData.fromLocalDateTime(
Instant.ofEpochMilli(((Integer) value).longValue()).atZone(sinkTimeZone).toLocalDateTime());
} else if (value instanceof Long) {
return TimestampData.fromLocalDateTime(Instant.ofEpochMilli((long) value).atZone(sinkTimeZone).toLocalDateTime());
return TimestampData
.fromLocalDateTime(Instant.ofEpochMilli((long) value).atZone(sinkTimeZone).toLocalDateTime());
} else {
return TimestampData.fromLocalDateTime(Instant.parse(value.toString()).atZone(sinkTimeZone).toLocalDateTime());
return TimestampData
.fromLocalDateTime(Instant.parse(value.toString()).atZone(sinkTimeZone).toLocalDateTime());
}
} else if (logicalType instanceof DecimalType) {
final DecimalType decimalType = ((DecimalType) logicalType);
......@@ -388,4 +401,8 @@ public abstract class AbstractSinkBuilder implements SinkBuilder {
}
return pks;
}
protected ZoneId getSinkTimeZone() {
return this.sinkTimeZone;
}
}
......@@ -20,6 +20,7 @@
package com.dlink.cdc;
import com.dlink.assertion.Asserts;
import com.dlink.cdc.doris.DorisExtendSinkBuilder;
import com.dlink.cdc.doris.DorisSinkBuilder;
import com.dlink.cdc.kafka.KafkaSinkBuilder;
import com.dlink.cdc.kafka.KafkaSinkJsonBuilder;
......@@ -41,7 +42,8 @@ public class SinkBuilderFactory {
new KafkaSinkJsonBuilder(),
new DorisSinkBuilder(),
new StarrocksSinkBuilder(),
new SQLSinkBuilder()
new SQLSinkBuilder(),
new DorisExtendSinkBuilder()
};
public static SinkBuilder buildSinkBuilder(FlinkCDCConfig config) {
......
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.dlink.cdc.doris;
import java.io.Serializable;
import java.util.Map;
/**
* @author BillyXing
* @since 2022/11/10 19:37
*/
public class AdditionalColumnEntry<K, V> implements Map.Entry<K, V>, Serializable {
private static final long serialVersionUID = 45678994131L;
private final K k;
private final V v;
private AdditionalColumnEntry(K k, V v) {
this.k = k;
this.v = v;
}
public static <K, V> AdditionalColumnEntry<K, V> of(K k, V v) {
return new AdditionalColumnEntry<K, V>(k, v);
}
@Override
public K getKey() {
return k;
}
@Override
public V getValue() {
return v;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.dlink.cdc.doris;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.ConfigOptions;
/**
* @author BillyXing
* @since 2022/11/10 19:37
*/
public class DorisExtendSinkOptions extends DorisSinkOptions {
public static final ConfigOption<String> AdditionalColumns = ConfigOptions.key("additional-columns").stringType()
.noDefaultValue()
.withDescription("Additional columns for sink, support meta column and fix value column."
+ "(meta: op_ts,database_name,schema_name,table_name; "
+ "fix value column type:BOOLEAN,INT,TINYINT,BIGINT,DECIMAL,FLOAT,DATE,TIMESTAMP,CHAR,VARCHAR,STRING)");
}
......@@ -20,6 +20,7 @@
package com.dlink.cdc;
import com.dlink.assertion.Asserts;
import com.dlink.cdc.doris.DorisExtendSinkBuilder;
import com.dlink.cdc.doris.DorisSinkBuilder;
import com.dlink.cdc.kafka.KafkaSinkBuilder;
import com.dlink.cdc.kafka.KafkaSinkJsonBuilder;
......@@ -39,7 +40,8 @@ public class SinkBuilderFactory {
new KafkaSinkBuilder(),
new KafkaSinkJsonBuilder(),
new DorisSinkBuilder(),
new SQLSinkBuilder()
new SQLSinkBuilder(),
new DorisExtendSinkBuilder()
};
public static SinkBuilder buildSinkBuilder(FlinkCDCConfig config) {
......
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.dlink.cdc.doris;
import java.io.Serializable;
import java.util.Map;
/**
* @author BillyXing
* @since 2022/11/10 19:37
*/
public class AdditionalColumnEntry<K, V> implements Map.Entry<K, V>, Serializable {
private static final long serialVersionUID = 45678994131L;
private final K k;
private final V v;
private AdditionalColumnEntry(K k, V v) {
this.k = k;
this.v = v;
}
public static <K, V> AdditionalColumnEntry<K, V> of(K k, V v) {
return new AdditionalColumnEntry<K, V>(k, v);
}
@Override
public K getKey() {
return k;
}
@Override
public V getValue() {
return v;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.dlink.cdc.doris;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.ConfigOptions;
/**
* @author BillyXing
* @since 2022/11/10 19:37
*/
public class DorisExtendSinkOptions extends DorisSinkOptions {
public static final ConfigOption<String> AdditionalColumns = ConfigOptions.key("additional-columns").stringType()
.noDefaultValue()
.withDescription("Additional columns for sink, support meta column and fix value column."
+ "(meta: op_ts,database_name,schema_name,table_name; "
+ "fix value column type:BOOLEAN,INT,TINYINT,BIGINT,DECIMAL,FLOAT,DATE,TIMESTAMP,CHAR,VARCHAR,STRING)");
}
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