MonsterChenzhuo commented on code in PR #1880:
URL: https://github.com/apache/incubator-paimon/pull/1880#discussion_r1304232336
##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/action/cdc/kafka/formats/canal/CanalRecordParser.java:
##########
@@ -199,69 +179,67 @@ protected String extractString(String key) {
}
private boolean isDdl() {
- return root.get("isDdl") != null && root.get("isDdl").asBoolean();
+ JsonNode isDdlNode = root.get(IS_DDL_FIELD);
+ if (isDdlNode == null) {
+ throw new IllegalArgumentException("Expected 'isDdl' field in the
JSON but not found.");
+ }
+ return isDdlNode.asBoolean();
}
private List<String> extractPrimaryKeys() {
- List<String> primaryKeys = new ArrayList<>();
- ArrayNode pkNames = (ArrayNode) root.get(FIELD_PRIMARY_KEYS);
- pkNames.iterator().forEachRemaining(pk ->
primaryKeys.add(toFieldName(pk.asText())));
- return primaryKeys;
+ ArrayNode pkNames = JsonSerdeUtil.getNodeAs(root, FIELD_PRIMARY_KEYS,
ArrayNode.class);
+ return StreamSupport.stream(pkNames.spliterator(), false)
+ .map(pk -> toFieldName(pk.asText()))
+ .collect(Collectors.toList());
}
private LinkedHashMap<String, String> extractFieldTypesFromMySqlType() {
+ JsonNode schema = JsonSerdeUtil.getNodeAs(root, FIELD_MYSQL_TYPE,
JsonNode.class);
Review Comment:
1.Direct Retrieval Method:
```java
JsonNode tableNode = root.get(FIELD_TABLE);
```
This is a straightforward method that directly retrieves the child node
named FIELD_TABLE from the root node. If the child node doesn't exist,
tableNode will be null.
2.Generic Method:
```java
public static <T extends JsonNode> T getNodeAs(
JsonNode root, String fieldName, Class<T> clazz) {
...
}
```
This method is more complex and aims to ensure that the retrieved child node
is not only present but also of the expected type. This is achieved through the
following steps:
Using Optional to handle potential null values.
Using the map method to retrieve the child node from the root.
Using the filter method to ensure the child node is of the expected type.
Using the map method to cast the child node to the expected type.
If the child node is absent or not of the expected type, an exception is
thrown using orElseThrow.
Differences:
Simplicity: The direct retrieval method is simpler but doesn't perform type
checks.
Type Safety: The generic method offers type safety, ensuring the retrieved
node is of the expected type.
Exception Handling: The generic method throws an exception if the node is
absent or not of the expected type, whereas the direct retrieval method returns
null if the node is absent.
In summary, the generic method offers stronger type safety and error
handling but is also more complex. If you frequently perform such operations in
your application and want to reduce potential errors, using the generic method
might be a good choice.
--
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]