This is an automated email from the ASF dual-hosted git repository.
yihua pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hudi.git
The following commit(s) were added to refs/heads/master by this push:
new f64ff333369 [HUDI-8125] Update handling of non-string inputs for
string fields in MercifulJsonConverter (#11825)
f64ff333369 is described below
commit f64ff333369bb7de3a78afd28263f162262773c8
Author: Tim Brown <[email protected]>
AuthorDate: Wed Sep 4 00:24:56 2024 -0500
[HUDI-8125] Update handling of non-string inputs for string fields in
MercifulJsonConverter (#11825)
---
.../apache/hudi/avro/MercifulJsonConverter.java | 24 ++++++++++++++++------
.../hudi/avro/TestMercifulJsonConverter.java | 19 +++++++++++++++++
2 files changed, 37 insertions(+), 6 deletions(-)
diff --git
a/hudi-common/src/main/java/org/apache/hudi/avro/MercifulJsonConverter.java
b/hudi-common/src/main/java/org/apache/hudi/avro/MercifulJsonConverter.java
index 5219a389662..a6eabc547f4 100644
--- a/hudi-common/src/main/java/org/apache/hudi/avro/MercifulJsonConverter.java
+++ b/hudi-common/src/main/java/org/apache/hudi/avro/MercifulJsonConverter.java
@@ -460,13 +460,25 @@ public class MercifulJsonConverter {
};
}
- protected JsonFieldProcessor generateStringTypeHandler() {
- return new JsonFieldProcessor() {
- @Override
- public Pair<Boolean, Object> convert(Object value, String name, Schema
schema) {
- return Pair.of(true, value.toString());
+ private static JsonFieldProcessor generateStringTypeHandler() {
+ return new StringProcessor();
+ }
+
+ private static class StringProcessor extends JsonFieldProcessor {
+ private static final ObjectMapper STRING_MAPPER = new ObjectMapper();
+
+ @Override
+ public Pair<Boolean, Object> convert(Object value, String name, Schema
schema) {
+ if (value instanceof String) {
+ return Pair.of(true, value);
+ } else {
+ try {
+ return Pair.of(true, STRING_MAPPER.writeValueAsString(value));
+ } catch (IOException ex) {
+ return Pair.of(false, null);
+ }
}
- };
+ }
}
protected JsonFieldProcessor generateBytesTypeHandler() {
diff --git
a/hudi-common/src/test/java/org/apache/hudi/avro/TestMercifulJsonConverter.java
b/hudi-common/src/test/java/org/apache/hudi/avro/TestMercifulJsonConverter.java
index 0c9be20570b..74a611ab245 100644
---
a/hudi-common/src/test/java/org/apache/hudi/avro/TestMercifulJsonConverter.java
+++
b/hudi-common/src/test/java/org/apache/hudi/avro/TestMercifulJsonConverter.java
@@ -32,6 +32,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
import java.io.IOException;
import java.math.BigDecimal;
@@ -71,6 +72,24 @@ public class TestMercifulJsonConverter {
Assertions.assertEquals(rec, CONVERTER.convert(json, simpleSchema));
}
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "{\"first\":\"John\",\"last\":\"Smith\"}",
+ "[{\"first\":\"John\",\"last\":\"Smith\"}]",
+ "{\"first\":\"John\",\"last\":\"Smith\",\"suffix\":3}",
+ })
+ void nestedJsonAsString(String nameInput) throws IOException {
+ Schema simpleSchema = SchemaTestUtil.getSimpleSchema();
+ String json = String.format("{\"name\": %s, \"favorite_number\": 1337,
\"favorite_color\": 10}", nameInput);
+
+ GenericRecord rec = new GenericData.Record(simpleSchema);
+ rec.put("name", nameInput);
+ rec.put("favorite_number", 1337);
+ rec.put("favorite_color", "10");
+
+ Assertions.assertEquals(rec, CONVERTER.convert(json, simpleSchema));
+ }
+
private static final String DECIMAL_AVRO_FILE_INVALID_PATH =
"/decimal-logical-type-invalid.avsc";
private static final String DECIMAL_AVRO_FILE_PATH =
"/decimal-logical-type.avsc";
private static final String DECIMAL_FIXED_AVRO_FILE_PATH =
"/decimal-logical-type-fixed-type.avsc";