-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[FLINK-18590][json] Support json array explode to multi messages #26473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
package org.apache.flink.formats.json; | ||
|
||
import org.apache.flink.api.common.functions.util.ListCollector; | ||
import org.apache.flink.api.common.serialization.DeserializationSchema; | ||
import org.apache.flink.connector.testutils.formats.DummyInitializationContext; | ||
import org.apache.flink.core.testutils.FlinkAssertions; | ||
|
@@ -34,6 +35,7 @@ | |
import org.apache.flink.testutils.junit.extensions.parameterized.ParameterizedTestExtension; | ||
import org.apache.flink.testutils.junit.extensions.parameterized.Parameters; | ||
import org.apache.flink.types.Row; | ||
import org.apache.flink.util.Collector; | ||
import org.apache.flink.util.jackson.JacksonMapperFactory; | ||
|
||
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper; | ||
|
@@ -51,6 +53,7 @@ | |
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.time.ZoneOffset; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
|
@@ -224,6 +227,75 @@ void testSerDe() throws Exception { | |
assertThat(serializedJson).containsExactly(actualBytes); | ||
} | ||
|
||
@Test | ||
public void testEmptyJsonArrayDeserialization() throws Exception { | ||
DataType dataType = ROW(FIELD("f1", INT()), FIELD("f2", BOOLEAN()), FIELD("f3", STRING())); | ||
RowType rowType = (RowType) dataType.getLogicalType(); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
ArrayNode arrayNode = objectMapper.createArrayNode(); | ||
|
||
DeserializationSchema<RowData> deserializationSchema = | ||
createDeserializationSchema( | ||
isJsonParser, rowType, false, false, TimestampFormat.ISO_8601); | ||
|
||
open(deserializationSchema); | ||
|
||
List<RowData> result = new ArrayList<>(); | ||
Collector<RowData> collector = new ListCollector<>(result); | ||
deserializationSchema.deserialize(objectMapper.writeValueAsBytes(arrayNode), collector); | ||
assertThat(result).isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testJsonArrayToMultiRecords() throws Exception { | ||
DataType dataType = ROW(FIELD("f1", INT()), FIELD("f2", BOOLEAN()), FIELD("f3", STRING())); | ||
RowType rowType = (RowType) dataType.getLogicalType(); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
ObjectNode element1 = objectMapper.createObjectNode(); | ||
element1.put("f1", 1); | ||
element1.put("f2", true); | ||
element1.put("f3", "str"); | ||
|
||
ObjectNode element2 = objectMapper.createObjectNode(); | ||
element2.put("f1", 10); | ||
element2.put("f2", false); | ||
element2.put("f3", "newStr"); | ||
|
||
ArrayNode arrayNode = objectMapper.createArrayNode(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we test arrays of arrays of arrays and arrays of objects of arrays There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am curious with arrays of arrays, do we create multiple records only at the top level or should we have an option to expand the nested arrays to multiple records also. Should this be a format configuration option ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume that it's more complicated structure and not supported yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I have added related description in doc. |
||
arrayNode.add(element1); | ||
arrayNode.add(element2); | ||
|
||
DeserializationSchema<RowData> deserializationSchema = | ||
createDeserializationSchema( | ||
isJsonParser, rowType, false, false, TimestampFormat.ISO_8601); | ||
|
||
open(deserializationSchema); | ||
|
||
// test serialization | ||
JsonRowDataSerializationSchema serializationSchema = | ||
new JsonRowDataSerializationSchema( | ||
rowType, | ||
TimestampFormat.ISO_8601, | ||
JsonFormatOptions.MapNullKeyMode.LITERAL, | ||
"null", | ||
true, | ||
false); | ||
open(serializationSchema); | ||
|
||
List<RowData> result = new ArrayList<>(); | ||
Collector<RowData> collector = new ListCollector<>(result); | ||
deserializationSchema.deserialize(objectMapper.writeValueAsBytes(arrayNode), collector); | ||
assertThat(result).hasSize(2); | ||
lianghongjia6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
byte[] result1 = serializationSchema.serialize(result.get(0)); | ||
byte[] result2 = serializationSchema.serialize(result.get(1)); | ||
assertThat(result1).isEqualTo(objectMapper.writeValueAsBytes(element1)); | ||
assertThat(result2).isEqualTo(objectMapper.writeValueAsBytes(element2)); | ||
} | ||
|
||
/** | ||
* Tests the deserialization slow path, e.g. convert into string and use {@link | ||
* Double#parseDouble(String)}. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please fire a new implement jira to remove this method in json module and add comments here just follow #13081 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I had create a new issue, and add comments