Skip to content
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

[Enhancement] Add an option to disable wrapping json to json array. #344

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -231,18 +231,18 @@ private boolean tryHttpConnection(String host) {
}
}

private byte[] joinRows(List<byte[]> rows, int totalBytes) throws IOException {
if (StarRocksSinkOptions.StreamLoadFormat.CSV.equals(sinkOptions.getStreamLoadFormat())) {
byte[] lineDelimiter = StarRocksDelimiterParser.parse(sinkOptions.getSinkStreamLoadProperties().get("row_delimiter"), "\n").getBytes(StandardCharsets.UTF_8);
ByteBuffer bos = ByteBuffer.allocate(totalBytes + rows.size() * lineDelimiter.length);
for (byte[] row : rows) {
bos.put(row);
bos.put(lineDelimiter);
}
return bos.array();
private byte[] joinCsvRows(List<byte[]> rows, int totalBytes) {
byte[] lineDelimiter = StarRocksDelimiterParser.parse(sinkOptions.getSinkStreamLoadProperties().get("row_delimiter"), "\n").getBytes(StandardCharsets.UTF_8);
ByteBuffer bos = ByteBuffer.allocate(totalBytes + rows.size() * lineDelimiter.length);
for (byte[] row : rows) {
bos.put(row);
bos.put(lineDelimiter);
}
return bos.array();
}

if (StarRocksSinkOptions.StreamLoadFormat.JSON.equals(sinkOptions.getStreamLoadFormat())) {
private byte[] joinJsonRows(List<byte[]> rows, int totalBytes) {
if (sinkOptions.isWrapJsonAsArray()) {
ByteBuffer bos = ByteBuffer.allocate(totalBytes + (rows.isEmpty() ? 2 : rows.size() + 1));
bos.put("[".getBytes(StandardCharsets.UTF_8));
byte[] jsonDelimiter = ",".getBytes(StandardCharsets.UTF_8);
Expand All @@ -256,8 +256,24 @@ private byte[] joinRows(List<byte[]> rows, int totalBytes) throws IOException {
}
bos.put("]".getBytes(StandardCharsets.UTF_8));
return bos.array();
} else {
ByteBuffer bos = ByteBuffer.allocate(totalBytes);
for (byte[] row : rows) {
bos.put(row);
}
return bos.array();
}
}

private byte[] joinRows(List<byte[]> rows, int totalBytes) {
switch (sinkOptions.getStreamLoadFormat()) {
case CSV:
return joinCsvRows(rows, totalBytes);
case JSON:
return joinJsonRows(rows, totalBytes);
default:
throw new RuntimeException("Failed to join rows data, unsupported `format` from stream load properties:");
}
throw new RuntimeException("Failed to join rows data, unsupported `format` from stream load properties:");
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public Set<ConfigOption<?>> optionalOptions() {
optionalOptions.add(StarRocksSinkOptions.SINK_ABORT_LINGERING_TXNS);
optionalOptions.add(StarRocksSinkOptions.SINK_ABORT_CHECK_NUM_TXNS);
optionalOptions.add(StarRocksSinkOptions.SINK_USE_NEW_SINK_API);
optionalOptions.add(StarRocksSinkOptions.SINK_WRAP_JSON_AS_ARRAY);
return optionalOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public enum StreamLoadFormat {

public static final ConfigOption<Integer> SINK_PARALLELISM = FactoryUtil.SINK_PARALLELISM;

public static final ConfigOption<Boolean> SINK_WRAP_JSON_AS_ARRAY = ConfigOptions.key("sink.wrap-json-as-array")
.booleanType()
.defaultValue(true)
.withDescription("Whether wrap data as array or not.");

// Sink semantic
private static final Set<String> SINK_SEMANTIC_ENUMS = Arrays.stream(StarRocksSinkSemantic.values()).map(s -> s.getName()).collect(Collectors.toSet());
// wild stream load properties' prefix
Expand Down Expand Up @@ -377,6 +382,10 @@ public boolean isUseUnifiedSinkApi() {
return tableOptions.get(SINK_USE_NEW_SINK_API);
}

public boolean isWrapJsonAsArray() {
return tableOptions.get(SINK_WRAP_JSON_AS_ARRAY);
}

private void validateStreamLoadUrl() {
tableOptions.getOptional(LOAD_URL).ifPresent(urlList -> {
for (String host : urlList) {
Expand Down