|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.datafusion; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +/** |
| 26 | + * Configuration knobs for writing JSON, passed to {@link DataFrame#writeJson(String, |
| 27 | + * JsonWriteOptions)}. |
| 28 | + * |
| 29 | + * <p>Mirrors a subset of DataFusion's {@code DataFrameWriteOptions} and the writer-side {@code |
| 30 | + * JsonOptions}. All setters return {@code this} for fluent chaining. Defaults: every field {@code |
| 31 | + * null} or empty (meaning the DataFusion default is used). |
| 32 | + * |
| 33 | + * <p>Path semantics: when {@link #singleFileOutput(boolean)} is {@code true}, the path passed to |
| 34 | + * {@code writeJson} is the literal output filename. When left unset (the default) and there are no |
| 35 | + * partition columns, the path is treated as a directory that DataFusion populates with one or more |
| 36 | + * part-files. |
| 37 | + * |
| 38 | + * <p>The output is always newline-delimited JSON (NDJSON). DataFusion's JSON writer does not emit |
| 39 | + * the bracketed array form, so there is no toggle for it here. |
| 40 | + * |
| 41 | + * <p>Compression reuses {@link FileCompressionType} -- the same codec set ({@code UNCOMPRESSED}, |
| 42 | + * {@code GZIP}, {@code BZIP2}, {@code XZ}, {@code ZSTD}) the read side and the CSV writer accept. |
| 43 | + */ |
| 44 | +public final class JsonWriteOptions { |
| 45 | + |
| 46 | + private Boolean singleFileOutput; |
| 47 | + private final List<String> partitionCols = new ArrayList<>(); |
| 48 | + private FileCompressionType fileCompressionType; |
| 49 | + |
| 50 | + /** |
| 51 | + * When {@code true}, write to a single file at the supplied path. When left unset (the default) |
| 52 | + * and no partition columns are configured, the path is treated as a directory and DataFusion |
| 53 | + * writes one or more part-files. |
| 54 | + */ |
| 55 | + public JsonWriteOptions singleFileOutput(boolean v) { |
| 56 | + this.singleFileOutput = v; |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Hive-style partition columns. Each column listed here is removed from the data rows and encoded |
| 62 | + * into the directory layout (one subdirectory per distinct value). Mutually exclusive with {@link |
| 63 | + * #singleFileOutput(boolean)} -- DataFusion rejects the combination at write time. |
| 64 | + */ |
| 65 | + public JsonWriteOptions partitionCols(String... cols) { |
| 66 | + this.partitionCols.clear(); |
| 67 | + for (String c : cols) { |
| 68 | + this.partitionCols.add(c); |
| 69 | + } |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + /** Output compression codec. Defaults to uncompressed. */ |
| 74 | + public JsonWriteOptions fileCompressionType(FileCompressionType t) { |
| 75 | + this.fileCompressionType = t; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + byte[] toBytes() { |
| 80 | + org.apache.datafusion.protobuf.JsonWriteOptionsProto.Builder b = |
| 81 | + org.apache.datafusion.protobuf.JsonWriteOptionsProto.newBuilder(); |
| 82 | + if (singleFileOutput != null) { |
| 83 | + b.setSingleFileOutput(singleFileOutput); |
| 84 | + } |
| 85 | + b.addAllPartitionCols(partitionCols); |
| 86 | + if (fileCompressionType != null) { |
| 87 | + b.setFileCompressionType(FileCompressionTypes.toProto(fileCompressionType)); |
| 88 | + } |
| 89 | + return b.build().toByteArray(); |
| 90 | + } |
| 91 | +} |
0 commit comments