|
| 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 CSV, passed to {@link DataFrame#writeCsv(String, |
| 27 | + * CsvWriteOptions)}. |
| 28 | + * |
| 29 | + * <p>Mirrors a subset of DataFusion's {@code DataFrameWriteOptions} and the writer-side {@code |
| 30 | + * CsvOptions}. 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 writeCsv} 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>Compression reuses {@link FileCompressionType} -- both the read and write sides accept the |
| 39 | + * same codec set ({@code UNCOMPRESSED}, {@code GZIP}, {@code BZIP2}, {@code XZ}, {@code ZSTD}). |
| 40 | + */ |
| 41 | +public final class CsvWriteOptions { |
| 42 | + |
| 43 | + private Boolean singleFileOutput; |
| 44 | + private final List<String> partitionCols = new ArrayList<>(); |
| 45 | + private Boolean hasHeader; |
| 46 | + private Byte delimiter; |
| 47 | + private Byte quote; |
| 48 | + private Byte escape; |
| 49 | + private String nullValue; |
| 50 | + private FileCompressionType fileCompressionType; |
| 51 | + |
| 52 | + /** |
| 53 | + * When {@code true}, write to a single file at the supplied path. When left unset (the default) |
| 54 | + * and no partition columns are configured, the path is treated as a directory and DataFusion |
| 55 | + * writes one or more part-files. |
| 56 | + */ |
| 57 | + public CsvWriteOptions singleFileOutput(boolean v) { |
| 58 | + this.singleFileOutput = v; |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Hive-style partition columns. Each column listed here is removed from the data rows and encoded |
| 64 | + * into the directory layout (one subdirectory per distinct value). Mutually exclusive with {@link |
| 65 | + * #singleFileOutput(boolean)} -- DataFusion rejects the combination at write time. |
| 66 | + */ |
| 67 | + public CsvWriteOptions partitionCols(String... cols) { |
| 68 | + this.partitionCols.clear(); |
| 69 | + for (String c : cols) { |
| 70 | + this.partitionCols.add(c); |
| 71 | + } |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + /** Whether to write a header row. Defaults to DataFusion's setting (typically {@code true}). */ |
| 76 | + public CsvWriteOptions hasHeader(boolean v) { |
| 77 | + this.hasHeader = v; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + /** Field delimiter byte. Defaults to {@code ','}. */ |
| 82 | + public CsvWriteOptions delimiter(byte b) { |
| 83 | + this.delimiter = b; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + /** Quote character byte. Defaults to {@code '"'}. */ |
| 88 | + public CsvWriteOptions quote(byte b) { |
| 89 | + this.quote = b; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + /** Escape character byte. Defaults to none. */ |
| 94 | + public CsvWriteOptions escape(byte b) { |
| 95 | + this.escape = b; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + /** String to write for SQL NULL values. Defaults to the empty string. */ |
| 100 | + public CsvWriteOptions nullValue(String s) { |
| 101 | + this.nullValue = s; |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /** Output compression codec. Defaults to uncompressed. */ |
| 106 | + public CsvWriteOptions fileCompressionType(FileCompressionType t) { |
| 107 | + this.fileCompressionType = t; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + byte[] toBytes() { |
| 112 | + org.apache.datafusion.protobuf.CsvWriteOptionsProto.Builder b = |
| 113 | + org.apache.datafusion.protobuf.CsvWriteOptionsProto.newBuilder(); |
| 114 | + if (singleFileOutput != null) { |
| 115 | + b.setSingleFileOutput(singleFileOutput); |
| 116 | + } |
| 117 | + b.addAllPartitionCols(partitionCols); |
| 118 | + if (hasHeader != null) { |
| 119 | + b.setHasHeader(hasHeader); |
| 120 | + } |
| 121 | + if (delimiter != null) { |
| 122 | + b.setDelimiter(delimiter & 0xFF); |
| 123 | + } |
| 124 | + if (quote != null) { |
| 125 | + b.setQuote(quote & 0xFF); |
| 126 | + } |
| 127 | + if (escape != null) { |
| 128 | + b.setEscape(escape & 0xFF); |
| 129 | + } |
| 130 | + if (nullValue != null) { |
| 131 | + b.setNullValue(nullValue); |
| 132 | + } |
| 133 | + if (fileCompressionType != null) { |
| 134 | + b.setFileCompressionType(FileCompressionTypes.toProto(fileCompressionType)); |
| 135 | + } |
| 136 | + return b.build().toByteArray(); |
| 137 | + } |
| 138 | +} |
0 commit comments