danny0405 commented on code in PR #10943:
URL: https://github.com/apache/hudi/pull/10943#discussion_r1547081375
##########
hudi-common/src/main/java/org/apache/hudi/avro/HoodieAvroUtils.java:
##########
@@ -209,10 +210,18 @@ public static byte[] avroToJson(GenericRecord record,
boolean pretty) throws IOE
private static ByteArrayOutputStream avroToJsonHelper(GenericRecord record,
boolean pretty) throws IOException {
DatumWriter<Object> writer = new GenericDatumWriter<>(record.getSchema());
ByteArrayOutputStream out = new ByteArrayOutputStream();
- JsonEncoder jsonEncoder =
EncoderFactory.get().jsonEncoder(record.getSchema(), out, pretty);
- writer.write(record, jsonEncoder);
- jsonEncoder.flush();
- return out;
+ try {
+ JsonEncoder jsonEncoder =
EncoderFactory.get().jsonEncoder(record.getSchema(), out, pretty);
+ writer.write(record, jsonEncoder);
+ jsonEncoder.flush();
+ return out;
+ } catch (ClassCastException | NullPointerException ex) {
+ // NullPointerException will be thrown in cases where the field values
are missing
+ // ClassCastException will be thrown in cases where the field values do
not match the schema type
+ // Fallback to using `toString` which also returns json but without a
pretty-print option
+ out.write(record.toString().getBytes(StandardCharsets.UTF_8));
Review Comment:
> think I've convinced myself there should just be a new method like
"safeToJson" that does not throw an exception that we use in the error
table/writer cases since those are not as critical to Hudi.
+1
--
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]