Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class JsonOptions {
ConfigOptions.key("json.line-delimiter")
.stringType()
.defaultValue("\n")
.withFallbackKeys("lineSep")
.withDescription("The line delimiter for JSON format");

/** Enum for handling null keys in JSON maps. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,65 @@ public void testWithCustomLineDelimiters() throws IOException {
}
}

@Test
public void testJsonLineDelimiterFallbackKey() throws IOException {
RowType rowType = DataTypes.ROW(DataTypes.INT().notNull(), DataTypes.STRING());

Options options = new Options();
options.set("lineSep", "|");

assertThat(options.get(JsonOptions.LINE_DELIMITER)).isEqualTo("|");

// the writer uses the delimiter given by the fallback key
String json =
writeToJson(
options,
rowType,
GenericRow.of(1, BinaryString.fromString("Alice")),
"test_line_sep");
assertThat(json).endsWith("|").doesNotContain("\n");

// the reader splits a '|' delimited file into rows
Path testFile = new Path(parent, "test_line_sep_read_" + UUID.randomUUID() + ".json");
try (PositionOutputStream out = fileIO.newOutputStream(testFile, false)) {
out.write(
"{\"f0\":1,\"f1\":\"Alice\"}|{\"f0\":2,\"f1\":\"Bob\"}|"
.getBytes(StandardCharsets.UTF_8));
}
FileFormat format =
new JsonFileFormat(new FileFormatFactory.FormatContext(options, 1024, 1024));
List<InternalRow> result = new ArrayList<>();
try (RecordReader<InternalRow> reader =
format.createReaderFactory(rowType, rowType, new ArrayList<>())
.createReader(
new FormatReaderContext(
fileIO,
testFile,
fileIO.getFileSize(testFile),
null,
null))) {
InternalRowSerializer serializer = new InternalRowSerializer(rowType);
reader.forEachRemaining(row -> result.add(serializer.copy(row)));
}
assertThat(result).hasSize(2);
assertThat(result.get(0).getInt(0)).isEqualTo(1);
assertThat(result.get(0).getString(1).toString()).isEqualTo("Alice");
assertThat(result.get(1).getInt(0)).isEqualTo(2);
assertThat(result.get(1).getString(1).toString()).isEqualTo("Bob");

List<InternalRow> roundTrip =
writeThenRead(
options,
rowType,
Arrays.asList(
GenericRow.of(1, BinaryString.fromString("Alice")),
GenericRow.of(2, BinaryString.fromString("Bob"))),
"test_line_sep_round_trip");
assertThat(roundTrip).hasSize(2);
assertThat(roundTrip.get(0).getString(1).toString()).isEqualTo("Alice");
assertThat(roundTrip.get(1).getString(1).toString()).isEqualTo("Bob");
}

@Test
public void testVectorTypeReadWrite() throws IOException {
RowType rowType = DataTypes.ROW(DataTypes.INT(), DataTypes.VECTOR(3, DataTypes.FLOAT()));
Expand Down
Loading