-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix] Fix Debezium format cannot parse date/time/timestamp
- Loading branch information
Showing
12 changed files
with
577 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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
370 changes: 329 additions & 41 deletions
370
...ector-kafka-e2e/src/test/java/org/apache/seatunnel/e2e/connector/kafka/KafkaFormatIT.java
Large diffs are not rendered by default.
Oops, something went wrong.
14 changes: 3 additions & 11 deletions
14
...tunnel-connector-v2-e2e/connector-kafka-e2e/src/test/resources/debezium/debezium_data.txt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
143 changes: 143 additions & 0 deletions
143
...at-json/src/main/java/org/apache/seatunnel/format/json/debezium/DebeziumRowConverter.java
This file contains 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,143 @@ | ||
/* | ||
* 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 org.apache.seatunnel.format.json.debezium; | ||
|
||
import org.apache.seatunnel.api.table.type.ArrayType; | ||
import org.apache.seatunnel.api.table.type.DecimalType; | ||
import org.apache.seatunnel.api.table.type.MapType; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelDataType; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRow; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRowType; | ||
import org.apache.seatunnel.api.table.type.SqlType; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DebeziumRowConverter implements Serializable { | ||
|
||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
private final SeaTunnelRowType rowType; | ||
public DebeziumRowConverter(SeaTunnelRowType rowType) { | ||
this.rowType = rowType; | ||
} | ||
|
||
public SeaTunnelRow serializeValue(String json) throws JsonProcessingException { | ||
JsonNode node = OBJECT_MAPPER.readTree(json); | ||
return (SeaTunnelRow) getValue(rowType, node); | ||
} | ||
|
||
private Object getValue(SeaTunnelDataType<?> dataType, JsonNode value) { | ||
SqlType sqlType = dataType.getSqlType(); | ||
if (value == null) { | ||
return null; | ||
} | ||
switch (sqlType) { | ||
case BOOLEAN: | ||
return value.booleanValue(); | ||
case TINYINT: | ||
return (byte) value.intValue(); | ||
case SMALLINT: | ||
return (short) value.intValue(); | ||
case INT: | ||
return value.intValue(); | ||
case BIGINT: | ||
return value.longValue(); | ||
case FLOAT: | ||
return value.floatValue(); | ||
case DOUBLE: | ||
return value.doubleValue(); | ||
case DECIMAL: | ||
if (value.isNumber()) { | ||
return value.decimalValue(); | ||
} | ||
if (value.isBinary() || value.isTextual()) { | ||
try { | ||
return new BigDecimal( | ||
new BigInteger(value.binaryValue()), | ||
((DecimalType) dataType).getScale()); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Invalid bytes for Decimal field", e); | ||
} | ||
} | ||
case STRING: | ||
return value.textValue(); | ||
case BYTES: | ||
try { | ||
return value.binaryValue(); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Invalid bytes field", e); | ||
} | ||
case DATE: | ||
return LocalDate.ofEpochDay(value.intValue()); | ||
case TIME: | ||
return LocalTime.ofNanoOfDay(value.longValue() * 1000L); | ||
case TIMESTAMP: | ||
try { | ||
long timestamp = Long.parseLong(value.toString()); | ||
return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneOffset.UTC); | ||
} catch (NumberFormatException e) { | ||
return LocalDateTime.parse( | ||
value.textValue(), | ||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")); | ||
} | ||
case ARRAY: | ||
List<Object> arrayValue = new ArrayList<>(); | ||
for (JsonNode o : value) { | ||
arrayValue.add(getValue(((ArrayType) dataType).getElementType(), o)); | ||
} | ||
return arrayValue; | ||
case MAP: | ||
Map<Object, Object> mapValue = new LinkedHashMap<>(); | ||
for (Iterator<Map.Entry<String, JsonNode>> it = value.fields(); it.hasNext(); ) { | ||
Map.Entry<String, JsonNode> entry = it.next(); | ||
mapValue.put( | ||
entry.getKey(), | ||
getValue(((MapType) dataType).getValueType(), entry.getValue())); | ||
} | ||
return mapValue; | ||
case ROW: | ||
SeaTunnelRowType rowType = (SeaTunnelRowType) dataType; | ||
SeaTunnelRow row = new SeaTunnelRow(rowType.getTotalFields()); | ||
for (int i = 0; i < rowType.getTotalFields(); i++) { | ||
row.setField( | ||
i, | ||
getValue(rowType.getFieldType(i), value.get(rowType.getFieldName(i)))); | ||
} | ||
return row; | ||
default: | ||
throw new UnsupportedOperationException("Unsupported type: " + sqlType); | ||
} | ||
} | ||
} |