forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvOptions.java
More file actions
273 lines (242 loc) · 9.11 KB
/
CsvOptions.java
File metadata and controls
273 lines (242 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery;
import com.google.common.base.MoreObjects;
import java.nio.charset.Charset;
import java.util.Objects;
/**
* Google BigQuery options for CSV format. This class wraps some properties of CSV files used by
* BigQuery to parse external data.
*/
public final class CsvOptions extends FormatOptions {
private static final long serialVersionUID = 2193570529308612708L;
private final Boolean allowJaggedRows;
private final Boolean allowQuotedNewLines;
private final String encoding;
private final String fieldDelimiter;
private final String quote;
private final Integer skipLeadingRows;
public static final class Builder {
private Boolean allowJaggedRows;
private Boolean allowQuotedNewLines;
private String encoding;
private String fieldDelimiter;
private String quote;
private Integer skipLeadingRows;
private Builder() {}
/**
* Set whether BigQuery should accept rows that are missing trailing optional columns. If
* {@code true}, BigQuery treats missing trailing columns as null values. If {@code false},
* records with missing trailing columns are treated as bad records, and if there are too many
* bad records, an invalid error is returned in the job result. By default, rows with missing
* trailing columns are considered bad records.
*/
public Builder allowJaggedRows(Boolean allowJaggedRows) {
this.allowJaggedRows = allowJaggedRows;
return this;
}
/**
* Sets whether BigQuery should allow quoted data sections that contain newline characters in a
* CSV file. By default quoted newline are not allowed.
*/
public Builder allowQuotedNewLines(Boolean allowQuotedNewLines) {
this.allowQuotedNewLines = allowQuotedNewLines;
return this;
}
/**
* Sets the character encoding of the data. The supported values are UTF-8 or ISO-8859-1. The
* default value is UTF-8. BigQuery decodes the data after the raw, binary data has been split
* using the values set in {@link #quote(String)} and {@link #fieldDelimiter(String)}.
*/
public Builder encoding(String encoding) {
this.encoding = encoding;
return this;
}
/**
* Sets the character encoding of the data. The supported values are UTF-8 or ISO-8859-1. The
* default value is UTF-8. BigQuery decodes the data after the raw, binary data has been split
* using the values set in {@link #quote(String)} and {@link #fieldDelimiter(String)}.
*/
public Builder encoding(Charset encoding) {
this.encoding = encoding.name();
return this;
}
/**
* Sets the separator for fields in a CSV file. BigQuery converts the string to ISO-8859-1
* encoding, and then uses the first byte of the encoded string to split the data in its raw,
* binary state. BigQuery also supports the escape sequence "\t" to specify a tab separator.
* The default value is a comma (',').
*/
public Builder fieldDelimiter(String fieldDelimiter) {
this.fieldDelimiter = fieldDelimiter;
return this;
}
/**
* Sets the value that is used to quote data sections in a CSV file. BigQuery converts the
* string to ISO-8859-1 encoding, and then uses the first byte of the encoded string to split
* the data in its raw, binary state. The default value is a double-quote ('"'). If your data
* does not contain quoted sections, set the property value to an empty string. If your data
* contains quoted newline characters, you must also set {@link #allowQuotedNewLines(Boolean)}
* property to {@code true}.
*/
public Builder quote(String quote) {
this.quote = quote;
return this;
}
/**
* Sets the number of rows at the top of a CSV file that BigQuery will skip when reading the
* data. The default value is 0. This property is useful if you have header rows in the file
* that should be skipped.
*/
public Builder skipLeadingRows(Integer skipLeadingRows) {
this.skipLeadingRows = skipLeadingRows;
return this;
}
/**
* Creates a {@code CsvOptions} object.
*/
public CsvOptions build() {
return new CsvOptions(this);
}
}
private CsvOptions(Builder builder) {
super(FormatOptions.CSV);
this.allowJaggedRows = builder.allowJaggedRows;
this.allowQuotedNewLines = builder.allowQuotedNewLines;
this.encoding = builder.encoding;
this.fieldDelimiter = builder.fieldDelimiter;
this.quote = builder.quote;
this.skipLeadingRows = builder.skipLeadingRows;
}
/**
* Returns whether BigQuery should accept rows that are missing trailing optional columns. If
* {@code true}, BigQuery treats missing trailing columns as null values. If {@code false},
* records with missing trailing columns are treated as bad records, and if the number of bad
* records exceeds {@link ExternalTableDefinition#maxBadRecords()}, an invalid error is returned
* in the job result.
*/
public Boolean allowJaggedRows() {
return allowJaggedRows;
}
/**
* Returns whether BigQuery should allow quoted data sections that contain newline characters in a
* CSV file.
*/
public Boolean allowQuotedNewLines() {
return allowQuotedNewLines;
}
/**
* Returns the character encoding of the data. The supported values are UTF-8 or ISO-8859-1. If
* not set, UTF-8 is used. BigQuery decodes the data after the raw, binary data has been split
* using the values set in {@link #quote()} and {@link #fieldDelimiter()}.
*/
public String encoding() {
return encoding;
}
/**
* Returns the separator for fields in a CSV file.
*/
public String fieldDelimiter() {
return fieldDelimiter;
}
/**
* Returns the value that is used to quote data sections in a CSV file.
*/
public String quote() {
return quote;
}
/**
* Returns the number of rows at the top of a CSV file that BigQuery will skip when reading the
* data.
*/
public Integer skipLeadingRows() {
return skipLeadingRows;
}
/**
* Returns a builder for the {@code CsvOptions} object.
*/
public Builder toBuilder() {
return new Builder()
.allowJaggedRows(allowJaggedRows)
.allowQuotedNewLines(allowQuotedNewLines)
.encoding(encoding)
.fieldDelimiter(fieldDelimiter)
.quote(quote)
.skipLeadingRows(skipLeadingRows);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("type", type())
.add("allowJaggedRows", allowJaggedRows)
.add("allowQuotedNewLines", allowQuotedNewLines)
.add("encoding", encoding)
.add("fieldDelimiter", fieldDelimiter)
.add("quote", quote)
.add("skipLeadingRows", skipLeadingRows)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(type(), allowJaggedRows, allowQuotedNewLines, encoding, fieldDelimiter,
quote, skipLeadingRows);
}
@Override
public boolean equals(Object obj) {
return obj == this
|| obj instanceof CsvOptions
&& Objects.equals(toPb(), ((CsvOptions) obj).toPb());
}
com.google.api.services.bigquery.model.CsvOptions toPb() {
com.google.api.services.bigquery.model.CsvOptions csvOptions =
new com.google.api.services.bigquery.model.CsvOptions();
csvOptions.setAllowJaggedRows(allowJaggedRows);
csvOptions.setAllowQuotedNewlines(allowQuotedNewLines);
csvOptions.setEncoding(encoding);
csvOptions.setFieldDelimiter(fieldDelimiter);
csvOptions.setQuote(quote);
csvOptions.setSkipLeadingRows(skipLeadingRows);
return csvOptions;
}
/**
* Returns a builder for a CsvOptions object.
*/
public static Builder builder() {
return new Builder();
}
static CsvOptions fromPb(com.google.api.services.bigquery.model.CsvOptions csvOptions) {
Builder builder = builder();
if (csvOptions.getAllowJaggedRows() != null) {
builder.allowJaggedRows(csvOptions.getAllowJaggedRows());
}
if (csvOptions.getAllowQuotedNewlines() != null) {
builder.allowQuotedNewLines(csvOptions.getAllowQuotedNewlines());
}
if (csvOptions.getEncoding() != null) {
builder.encoding(csvOptions.getEncoding());
}
if (csvOptions.getFieldDelimiter() != null) {
builder.fieldDelimiter(csvOptions.getFieldDelimiter());
}
if (csvOptions.getQuote() != null) {
builder.quote(csvOptions.getQuote());
}
if (csvOptions.getSkipLeadingRows() != null) {
builder.skipLeadingRows(csvOptions.getSkipLeadingRows());
}
return builder.build();
}
}