hailin0 commented on code in PR #3981: URL: https://github.com/apache/incubator-seatunnel/pull/3981#discussion_r1126095803
########## seatunnel-formats/seatunnel-format-json/src/main/java/org/apache/seatunnel/format/json/debezium/DebeziumJsonDeserializationSchema.java: ########## @@ -0,0 +1,159 @@ +/* + * 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.serialization.DeserializationSchema; +import org.apache.seatunnel.api.source.Collector; +import org.apache.seatunnel.api.table.type.RowKind; +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.common.exception.CommonErrorCode; +import org.apache.seatunnel.format.json.JsonDeserializationSchema; +import org.apache.seatunnel.format.json.exception.SeaTunnelJsonFormatException; + +import java.io.IOException; + +import static org.apache.seatunnel.api.table.type.BasicType.STRING_TYPE; + +public class DebeziumJsonDeserializationSchema implements DeserializationSchema<SeaTunnelRow> { + private static final long serialVersionUID = 1L; + + private static final String OP_READ = "r"; // snapshot read + private static final String OP_CREATE = "c"; // insert + private static final String OP_UPDATE = "u"; // update + private static final String OP_DELETE = "d"; // delete + + private static final String REPLICA_IDENTITY_EXCEPTION = + "The \"before\" field of %s message is null, " + + "if you are using Debezium Postgres Connector, " + + "please check the Postgres table has been set REPLICA IDENTITY to FULL level."; + + private final SeaTunnelRowType rowType; + + private final JsonDeserializationSchema jsonDeserializer; + + /** + * Flag indicating whether the Debezium JSON data contains schema part or not. When Debezium + * Kafka Connect enables "value.converter.schemas.enable", the JSON will contain "schema" + * information, but we just ignore "schema" and extract data from "payload". + */ + private final boolean schemaInclude; + + private final boolean ignoreParseErrors; + + public DebeziumJsonDeserializationSchema( + SeaTunnelRowType rowType, boolean schemaInclude, boolean ignoreParseErrors) { + this.rowType = rowType; + this.schemaInclude = schemaInclude; + this.ignoreParseErrors = ignoreParseErrors; + this.jsonDeserializer = + new JsonDeserializationSchema( + false, ignoreParseErrors, createJsonRowType(rowType, schemaInclude)); + } + + @Override + public SeaTunnelRow deserialize(byte[] message) throws IOException { + throw new UnsupportedOperationException( + "Please invoke DeserializationSchema#deserialize(byte[], Collector<SeaTunnelRow>) instead."); + } + + @Override + public void deserialize(byte[] message, Collector<SeaTunnelRow> out) throws IOException { + if (message == null || message.length == 0) { + // skip tombstone messages + return; + } + + try { + SeaTunnelRow row = jsonDeserializer.deserialize(message); Review Comment: reference https://github.com/apache/incubator-seatunnel/blob/dev/seatunnel-formats/seatunnel-format-json/src/main/java/org/apache/seatunnel/format/json/canal/CanalJsonDeserializationSchema.java#L116 https://github.com/apache/incubator-seatunnel/blob/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-kafka-e2e/src/test/java/org/apache/seatunnel/e2e/connector/kafka/CanalToKafkaIT.java#L265 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
