-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[Debezium] Fix the server timeZone bug of debeziumDeserialization when using dataSream API(#317) #1366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[Debezium] Fix the server timeZone bug of debeziumDeserialization when using dataSream API(#317) #1366
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cef031b
[Bug] [Debezium] Fix the server timeZone bug of debeziumDeserialization
312f13a
[Debezium]add debeziumDeserializationSchema which contains primary ke…
2e8ca2d
Merge branch 'ververica:master' into master
youyangkou b9148b4
Merge branch 'ververica:master' into master
youyangkou 191851c
Merge branch 'ververica:master' into master
youyangkou 728ff4b
Merge branch 'ververica:master' into master
youyangkou b31cde2
Merge branch 'ververica:master' into master
youyangkou 1bdbaaa
Merge branch 'ververica:master' into master
youyangkou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...-debezium/src/main/java/com/ververica/cdc/debezium/PairDebeziumDeserializationSchema.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package com.ververica.cdc.debezium; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.ververica.cdc.debezium.utils.SourceRecordUtil; | ||
| import javafx.util.Pair; | ||
| import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
| import org.apache.flink.util.Collector; | ||
| import org.apache.kafka.connect.json.JsonConverter; | ||
| import org.apache.kafka.connect.json.JsonConverterConfig; | ||
| import org.apache.kafka.connect.source.SourceRecord; | ||
| import org.apache.kafka.connect.storage.ConverterConfig; | ||
| import org.apache.kafka.connect.storage.ConverterType; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
|
|
||
| public class PairDebeziumDeserializationSchema implements DebeziumDeserializationSchema<Pair> { | ||
|
|
||
| private transient ObjectMapper objectMapper; | ||
| private transient JsonConverter valueConverter; | ||
| private transient JsonConverter keyConverter; | ||
| private transient boolean initialized; | ||
| private final int timeZoneOffset; | ||
|
|
||
| public PairDebeziumDeserializationSchema() { | ||
| this.timeZoneOffset = 8; | ||
| } | ||
|
|
||
| public PairDebeziumDeserializationSchema(int timeZoneOffset) { | ||
| this.timeZoneOffset = timeZoneOffset; | ||
| } | ||
|
|
||
| @Override | ||
| public void deserialize(SourceRecord record, Collector<Pair> collector) throws Exception { | ||
| if (!initialized) { | ||
| initialize(); | ||
| } | ||
|
|
||
| SourceRecord sourceRecord = SourceRecordUtil.correctTimeZoneSourceRecord(record, timeZoneOffset); | ||
|
|
||
| byte[] value = valueConverter.fromConnectData( | ||
| sourceRecord.topic(), sourceRecord.valueSchema(), sourceRecord.value()); | ||
|
|
||
| if (sourceRecord.key() == null) { | ||
| collector.collect(new Pair(null, objectMapper.readTree(value))); | ||
| } else { | ||
| byte[] key = | ||
| keyConverter.fromConnectData( | ||
| sourceRecord.topic(), sourceRecord.keySchema(), sourceRecord.key()); | ||
| collector.collect(new Pair(objectMapper.readTree(key), objectMapper.readTree(value))); | ||
| } | ||
| } | ||
|
|
||
| private void initialize() { | ||
| initialized = true; | ||
| objectMapper = new ObjectMapper(); | ||
|
|
||
| valueConverter = new JsonConverter(); | ||
| Map<String, Object> valueConfigs = new HashMap<>(2); | ||
| valueConfigs.put(ConverterConfig.TYPE_CONFIG, ConverterType.VALUE.getName()); | ||
| valueConfigs.put(JsonConverterConfig.SCHEMAS_ENABLE_CONFIG, false); | ||
| valueConverter.configure(valueConfigs); | ||
|
|
||
| keyConverter = new JsonConverter(); | ||
| Map<String, Object> keyConfigs = new HashMap<>(2); | ||
| keyConfigs.put(ConverterConfig.TYPE_CONFIG, ConverterType.KEY.getName()); | ||
| keyConfigs.put(JsonConverterConfig.SCHEMAS_ENABLE_CONFIG, false); | ||
| keyConverter.configure(keyConfigs); | ||
| } | ||
|
|
||
| @Override | ||
| public TypeInformation<Pair> getProducedType() { | ||
| return TypeInformation.of(Pair.class); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...k-connector-debezium/src/main/java/com/ververica/cdc/debezium/utils/SourceRecordUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package com.ververica.cdc.debezium.utils; | ||
|
|
||
| import io.debezium.data.Envelope; | ||
| import io.debezium.time.Timestamp; | ||
| import io.debezium.time.ZonedTimestamp; | ||
| import org.apache.kafka.connect.data.Field; | ||
| import org.apache.kafka.connect.data.Schema; | ||
| import org.apache.kafka.connect.data.Struct; | ||
| import org.apache.kafka.connect.source.SourceRecord; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.time.ZoneOffset; | ||
|
|
||
| /** | ||
| * Utilities to {@link SourceRecord}. | ||
| */ | ||
| public class SourceRecordUtil { | ||
|
|
||
| private SourceRecordUtil() { | ||
| } | ||
|
|
||
| /** | ||
| * Return {@link SourceRecord} which correct the record time zone | ||
| */ | ||
| public static SourceRecord correctTimeZoneSourceRecord(SourceRecord record, int timeZoneOffset) { | ||
| Envelope.Operation op = Envelope.operationFor(record); | ||
| Struct value = (Struct) record.value(); | ||
| Schema valueSchema = record.valueSchema(); | ||
|
|
||
| if (op == Envelope.Operation.CREATE || op == Envelope.Operation.READ) { | ||
| Struct after = extractAfterStruct(value, valueSchema, timeZoneOffset); | ||
| value.put(Envelope.FieldName.AFTER, after); | ||
| } else if (op == Envelope.Operation.DELETE) { | ||
| Struct before = extractBeforeStruct(value, valueSchema, timeZoneOffset); | ||
| value.put(Envelope.FieldName.BEFORE, before); | ||
| } else { | ||
| Struct before = extractBeforeStruct(value, valueSchema, timeZoneOffset); | ||
| value.put(Envelope.FieldName.BEFORE, before); | ||
| Struct after = extractAfterStruct(value, valueSchema, timeZoneOffset); | ||
| value.put(Envelope.FieldName.AFTER, after); | ||
| } | ||
|
|
||
| SourceRecord sourceRecord = new SourceRecord(record.sourcePartition(), record.sourceOffset(), record.topic(), record.kafkaPartition(), record.keySchema(), record.key(), valueSchema, value); | ||
| return sourceRecord; | ||
| } | ||
|
|
||
| private static Struct extractBeforeStruct(Struct value, Schema valueSchema, int timeZoneOffset) { | ||
| Schema beforeSchema = valueSchema.field(Envelope.FieldName.BEFORE).schema(); | ||
| Struct beforeStruct = value.getStruct(Envelope.FieldName.BEFORE); | ||
| for (Field field : beforeSchema.fields()) { | ||
| beforeStruct = convertTimestamp(beforeStruct, field, timeZoneOffset); | ||
| } | ||
| return beforeStruct; | ||
| } | ||
|
|
||
| private static Struct extractAfterStruct(Struct value, Schema valueSchema, int timeZoneOffset) { | ||
| Schema afterSchema = valueSchema.field(Envelope.FieldName.AFTER).schema(); | ||
| Struct afterStruct = value.getStruct(Envelope.FieldName.AFTER); | ||
| for (Field field : afterSchema.fields()) { | ||
| afterStruct = convertTimestamp(afterStruct, field, timeZoneOffset); | ||
| } | ||
| return afterStruct; | ||
| } | ||
|
|
||
| private static Struct convertTimestamp(Struct struct, Field field, int timeZoneOffset) { | ||
| if (struct.get(field) != null) { | ||
| //DATETIME TYPE | ||
| if (Timestamp.SCHEMA_NAME.equals(field.schema().name())) { | ||
| struct.put(field, (Long) struct.get(field) - ZoneOffset.ofHours(timeZoneOffset).getTotalSeconds() * 1000); | ||
| } | ||
| //TIMESTAMP TYPE | ||
| if (ZonedTimestamp.SCHEMA_NAME.equals(field.schema().name())) { | ||
| if (struct.get(field) instanceof String) { | ||
| LocalDateTime localDateTime = TemporalConversions.toLocalDateTime(struct.get(field), ZoneOffset.UTC); | ||
| String timeStamp = localDateTime.toInstant(ZoneOffset.ofHours(-timeZoneOffset)).toString(); | ||
| struct.put(field, timeStamp); | ||
| } | ||
| } | ||
| } | ||
| return struct; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @youyangkou for the contribution, but the PR looks like work in a wrong way.
Firstly, the timeZoneOffset
8works is that you happen to be in GMT+8 timezone, for other timezone it doesn't work.Secondly, Not all source need consider the timezone issue, for example, pg doesn't exist the timezone issue, and even in MySQL, only the record from changelog need consider the timezone offset.