You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/05/15 08:46:01 UTC

[GitHub] [flink] wuchong commented on a change in pull request #12152: [FLINK-17149][debezium][canal] Introduce Debezium and Canal changelog format

wuchong commented on a change in pull request #12152:
URL: https://github.com/apache/flink/pull/12152#discussion_r425656042



##########
File path: flink-formats/flink-json/src/main/java/org/apache/flink/formats/json/canal/CanalJsonDeserializationSchema.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.flink.formats.json.canal;
+
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.formats.json.JsonRowDataDeserializationSchema;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.types.RowKind;
+import org.apache.flink.util.Collector;
+
+import java.io.IOException;
+import java.util.Objects;
+
+import static java.lang.String.format;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType;
+
+/**
+ * Deserialization schema from Canal JSON to Flink Table/SQL internal data structure {@link RowData}.
+ * The deserialization schema knows Debezium's schema definition and can extract the database data
+ * and convert into {@link RowData} with {@link RowKind}.
+ *
+ * <p>Deserializes a <code>byte[]</code> message as a JSON object and reads
+ * the specified fields.
+ *
+ * <p>Failures during deserialization are forwarded as wrapped IOExceptions.
+ *
+ * @see <a href="https://github.com/alibaba/canal">Alibaba Canal</a>
+ */
+public class CanalJsonDeserializationSchema implements DeserializationSchema<RowData> {
+	private static final long serialVersionUID = 1L;
+
+	private static final String OP_INSERT = "INSERT";
+	private static final String OP_UPDATE = "UPDATE";
+	private static final String OP_DELETE = "DELETE";
+
+	/** The deserializer to deserialize Debezium JSON data. */
+	private final JsonRowDataDeserializationSchema jsonDeserializer;
+
+	/** TypeInformation of the produced {@link RowData}. **/
+	private final TypeInformation<RowData> resultTypeInfo;
+
+	/** Flag indicating whether to ignore invalid fields/rows (default: throw an exception). */
+	private final boolean ignoreParseErrors;
+
+	/** Number of fields. */
+	private final int fieldCount;
+
+	public CanalJsonDeserializationSchema(
+			RowType rowType,
+			TypeInformation<RowData> resultTypeInfo,
+			boolean ignoreParseErrors) {
+		this.resultTypeInfo = resultTypeInfo;
+		this.ignoreParseErrors = ignoreParseErrors;
+		this.fieldCount = rowType.getFieldCount();
+		this.jsonDeserializer = new JsonRowDataDeserializationSchema(
+			createJsonRowType(fromLogicalToDataType(rowType)),
+			// the result type is never used, so it's fine to pass in Canal's result type
+			resultTypeInfo,
+			false, // ignoreParseErrors already contains the functionality of failOnMissingField
+			ignoreParseErrors);
+
+	}
+
+	@Override
+	public RowData deserialize(byte[] message) throws IOException {
+		throw new RuntimeException(
+			"Please invoke DeserializationSchema#deserialize(byte[], Collector<RowData>) instead.");
+	}
+
+	@Override
+	public void deserialize(byte[] message, Collector<RowData> out) throws IOException {
+		RowData row = jsonDeserializer.deserialize(message);
+		String type = row.getString(2).toString(); // "type" field
+		if (OP_INSERT.equals(type)) {
+			// "data" field is an array of row, contains inserted rows

Review comment:
       It's not easy to rewrite in switch case, because we can't allocate local variable with the same naem. 

##########
File path: flink-formats/flink-json/src/main/java/org/apache/flink/formats/json/canal/CanalJsonDeserializationSchema.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.flink.formats.json.canal;
+
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.formats.json.JsonRowDataDeserializationSchema;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.types.RowKind;
+import org.apache.flink.util.Collector;
+
+import java.io.IOException;
+import java.util.Objects;
+
+import static java.lang.String.format;
+import static org.apache.flink.table.types.utils.TypeConversions.fromLogicalToDataType;
+
+/**
+ * Deserialization schema from Canal JSON to Flink Table/SQL internal data structure {@link RowData}.
+ * The deserialization schema knows Debezium's schema definition and can extract the database data
+ * and convert into {@link RowData} with {@link RowKind}.
+ *
+ * <p>Deserializes a <code>byte[]</code> message as a JSON object and reads
+ * the specified fields.
+ *
+ * <p>Failures during deserialization are forwarded as wrapped IOExceptions.
+ *
+ * @see <a href="https://github.com/alibaba/canal">Alibaba Canal</a>
+ */
+public class CanalJsonDeserializationSchema implements DeserializationSchema<RowData> {
+	private static final long serialVersionUID = 1L;
+
+	private static final String OP_INSERT = "INSERT";
+	private static final String OP_UPDATE = "UPDATE";
+	private static final String OP_DELETE = "DELETE";
+
+	/** The deserializer to deserialize Debezium JSON data. */
+	private final JsonRowDataDeserializationSchema jsonDeserializer;
+
+	/** TypeInformation of the produced {@link RowData}. **/
+	private final TypeInformation<RowData> resultTypeInfo;
+
+	/** Flag indicating whether to ignore invalid fields/rows (default: throw an exception). */
+	private final boolean ignoreParseErrors;
+
+	/** Number of fields. */
+	private final int fieldCount;
+
+	public CanalJsonDeserializationSchema(
+			RowType rowType,
+			TypeInformation<RowData> resultTypeInfo,
+			boolean ignoreParseErrors) {
+		this.resultTypeInfo = resultTypeInfo;
+		this.ignoreParseErrors = ignoreParseErrors;
+		this.fieldCount = rowType.getFieldCount();
+		this.jsonDeserializer = new JsonRowDataDeserializationSchema(
+			createJsonRowType(fromLogicalToDataType(rowType)),
+			// the result type is never used, so it's fine to pass in Canal's result type
+			resultTypeInfo,
+			false, // ignoreParseErrors already contains the functionality of failOnMissingField
+			ignoreParseErrors);
+
+	}
+
+	@Override
+	public RowData deserialize(byte[] message) throws IOException {
+		throw new RuntimeException(
+			"Please invoke DeserializationSchema#deserialize(byte[], Collector<RowData>) instead.");
+	}
+
+	@Override
+	public void deserialize(byte[] message, Collector<RowData> out) throws IOException {
+		RowData row = jsonDeserializer.deserialize(message);
+		String type = row.getString(2).toString(); // "type" field
+		if (OP_INSERT.equals(type)) {
+			// "data" field is an array of row, contains inserted rows

Review comment:
       It's not easy to rewrite in switch case, because we can't allocate local variable with the same name. 




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org