Hisoka-X commented on code in PR #5919:
URL: https://github.com/apache/seatunnel/pull/5919#discussion_r1410191100
##########
seatunnel-formats/seatunnel-format-json/src/main/java/org/apache/seatunnel/format/json/canal/CanalJsonDeserializationSchema.java:
##########
@@ -113,93 +114,93 @@ public SeaTunnelDataType<SeaTunnelRow> getProducedType() {
return this.physicalRowType;
}
- @Override
- public void deserialize(byte[] message, Collector<SeaTunnelRow> out) {
- if (message == null) {
- return;
- }
- ObjectNode jsonNode = (ObjectNode) convertBytes(message);
- assert jsonNode != null;
- deserialize(jsonNode, out);
- }
-
- public void deserialize(ObjectNode jsonNode, Collector<SeaTunnelRow> out) {
- if (database != null
- &&
!databasePattern.matcher(jsonNode.get(FIELD_DATABASE).asText()).matches()) {
- return;
- }
- if (table != null &&
!tablePattern.matcher(jsonNode.get(FIELD_TABLE).asText()).matches()) {
- return;
- }
- JsonNode dataNode = jsonNode.get(FIELD_DATA);
- String type = jsonNode.get(FIELD_TYPE).asText();
- // When a null value is encountered, an exception needs to be thrown
for easy sensing
- if (dataNode == null || dataNode.isNull()) {
- // We'll skip the query or create or alter event data
- if (OP_QUERY.equals(type) || OP_CREATE.equals(type) ||
OP_ALTER.equals(type)) {
+ public void deserialize(ObjectNode jsonNode, Collector<SeaTunnelRow> out)
throws IOException {
+ try {
+ if (database != null
+ &&
!databasePattern.matcher(jsonNode.get(FIELD_DATABASE).asText()).matches()) {
return;
}
- throw new SeaTunnelJsonFormatException(
- CommonErrorCodeDeprecated.JSON_OPERATION_FAILED,
- format("Null data value \"%s\" Cannot send downstream",
jsonNode));
- }
- if (OP_INSERT.equals(type)) {
- for (int i = 0; i < dataNode.size(); i++) {
- SeaTunnelRow row = convertJsonNode(dataNode.get(i));
- out.collect(row);
+ if (table != null
+ &&
!tablePattern.matcher(jsonNode.get(FIELD_TABLE).asText()).matches()) {
+ return;
}
- } else if (OP_UPDATE.equals(type)) {
- final ArrayNode oldNode = (ArrayNode) jsonNode.get(FIELD_OLD);
- for (int i = 0; i < dataNode.size(); i++) {
- SeaTunnelRow after = convertJsonNode(dataNode.get(i));
- SeaTunnelRow before = convertJsonNode(oldNode.get(i));
- for (int f = 0; f < fieldCount; f++) {
- assert before != null;
- if (before.isNullAt(f) && oldNode.findValue(fieldNames[f])
== null) {
- // fields in "old" (before) means the fields are
changed
- // fields not in "old" (before) means the fields are
not changed
- // so we just copy the not changed fields into before
- assert after != null;
- before.setField(f, after.getField(f));
- }
+
+ JsonNode dataNode = jsonNode.get(FIELD_DATA);
+ String type = jsonNode.get(FIELD_TYPE).asText();
+ // When a null value is encountered, an exception needs to be
thrown for easy sensing
+ if (dataNode == null || dataNode.isNull()) {
+ // We'll skip the query or create or alter event data
+ if (OP_QUERY.equals(type) || OP_CREATE.equals(type) ||
OP_ALTER.equals(type)) {
+ return;
}
- assert before != null;
- before.setRowKind(RowKind.UPDATE_BEFORE);
- assert after != null;
- after.setRowKind(RowKind.UPDATE_AFTER);
- out.collect(before);
- out.collect(after);
+ throw new IllegalStateException(
+ format("Null data value '%s' Cannot send downstream",
jsonNode));
}
- } else if (OP_DELETE.equals(type)) {
- for (int i = 0; i < dataNode.size(); i++) {
- SeaTunnelRow row = convertJsonNode(dataNode.get(i));
- assert row != null;
- row.setRowKind(RowKind.DELETE);
- out.collect(row);
+ if (OP_INSERT.equals(type)) {
+ for (int i = 0; i < dataNode.size(); i++) {
+ SeaTunnelRow row = convertJsonNode(dataNode.get(i));
+ out.collect(row);
+ }
+ } else if (OP_UPDATE.equals(type)) {
+ final ArrayNode oldNode = (ArrayNode) jsonNode.get(FIELD_OLD);
+ for (int i = 0; i < dataNode.size(); i++) {
+ SeaTunnelRow after = convertJsonNode(dataNode.get(i));
+ SeaTunnelRow before = convertJsonNode(oldNode.get(i));
+ for (int f = 0; f < fieldCount; f++) {
+ if (before.isNullAt(f) &&
oldNode.findValue(fieldNames[f]) == null) {
+ // fields in "old" (before) means the fields are
changed
+ // fields not in "old" (before) means the fields
are not changed
+ // so we just copy the not changed fields into
before
+ before.setField(f, after.getField(f));
+ }
+ }
+ before.setRowKind(RowKind.UPDATE_BEFORE);
+ after.setRowKind(RowKind.UPDATE_AFTER);
+ out.collect(before);
+ out.collect(after);
+ }
+ } else if (OP_DELETE.equals(type)) {
+ for (int i = 0; i < dataNode.size(); i++) {
+ SeaTunnelRow row = convertJsonNode(dataNode.get(i));
+ row.setRowKind(RowKind.DELETE);
+ out.collect(row);
+ }
+ } else {
+ throw new IllegalStateException(format("Unknown operation type
'%s'.", type));
}
- } else {
+ } catch (Throwable t) {
if (!ignoreParseErrors) {
- throw new SeaTunnelJsonFormatException(
- CommonErrorCodeDeprecated.UNSUPPORTED_DATA_TYPE,
- format(
- "Unknown \"type\" value \"%s\". The Canal JSON
message is '%s'",
- type, jsonNode.asText()));
+ Throwable cause = CommonError.jsonOperationError(FORMAT,
jsonNode.toString(), t);
+ throw new IOException(cause);
Review Comment:
We should ensure that our error messages are clear and effective, and there
is no need to create an additional layer of packaging. At the same time, in
most cases, the outer layer should not catch SeaTunnelRuntimeException. If it
is an IOException, it may be caught.
--
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]