Skip to content

Commit 79001cf

Browse files
committed
Replace format with FormatOptions in ExternalDataConfiguration
1 parent 5f6357e commit 79001cf

8 files changed

Lines changed: 200 additions & 88 deletions

File tree

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/CsvOptions.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818

1919
import com.google.common.base.MoreObjects;
2020

21-
import java.io.Serializable;
2221
import java.nio.charset.Charset;
2322
import java.util.Objects;
2423

2524
/**
26-
* Google BigQuery CSV options. This class wraps some properties of CSV files used by BigQuery to
27-
* parse external data.
25+
* Google BigQuery options for CSV format. This class wraps some properties of CSV files used by
26+
* BigQuery to parse external data.
2827
*/
29-
public class CsvOptions implements Serializable {
28+
public class CsvOptions extends FormatOptions {
3029

3130
private static final long serialVersionUID = 2193570529308612708L;
3231

@@ -132,6 +131,7 @@ public CsvOptions build() {
132131
}
133132

134133
private CsvOptions(Builder builder) {
134+
super(FormatOptions.CSV);
135135
this.allowJaggedRows = builder.allowJaggedRows;
136136
this.allowQuotedNewLines = builder.allowQuotedNewLines;
137137
this.encoding = builder.encoding;
@@ -226,24 +226,12 @@ public boolean equals(Object obj) {
226226
com.google.api.services.bigquery.model.CsvOptions toPb() {
227227
com.google.api.services.bigquery.model.CsvOptions csvOptions =
228228
new com.google.api.services.bigquery.model.CsvOptions();
229-
if (allowJaggedRows != null) {
230-
csvOptions.setAllowJaggedRows(allowJaggedRows);
231-
}
232-
if (allowQuotedNewLines != null) {
233-
csvOptions.setAllowQuotedNewlines(allowQuotedNewLines);
234-
}
235-
if (encoding != null) {
236-
csvOptions.setEncoding(encoding);
237-
}
238-
if (fieldDelimiter != null) {
239-
csvOptions.setFieldDelimiter(fieldDelimiter);
240-
}
241-
if (quote != null) {
242-
csvOptions.setQuote(quote);
243-
}
244-
if (skipLeadingRows != null) {
245-
csvOptions.setSkipLeadingRows(skipLeadingRows);
246-
}
229+
csvOptions.setAllowJaggedRows(allowJaggedRows);
230+
csvOptions.setAllowQuotedNewlines(allowQuotedNewLines);
231+
csvOptions.setEncoding(encoding);
232+
csvOptions.setFieldDelimiter(fieldDelimiter);
233+
csvOptions.setQuote(quote);
234+
csvOptions.setSkipLeadingRows(skipLeadingRows);
247235
return csvOptions;
248236
}
249237

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalDataConfiguration.java

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,27 @@ public com.google.api.services.bigquery.model.ExternalDataConfiguration apply(
6363

6464
private final List<String> sourceUris;
6565
private final Schema schema;
66-
private final String sourceFormat;
66+
private final FormatOptions formatOptions;
6767
private final Integer maxBadRecords;
6868
private final Boolean ignoreUnknownValues;
6969
private final String compression;
70-
private final CsvOptions csvOptions;
7170

7271
public static final class Builder {
7372

7473
private List<String> sourceUris;
7574
private Schema schema;
76-
private String sourceFormat;
75+
private FormatOptions formatOptions;
7776
private Integer maxBadRecords;
7877
private Boolean ignoreUnknownValues;
7978
private String compression;
80-
private CsvOptions csvOptions;
8179

8280
private Builder() {}
8381

8482
/**
85-
* Sets the fully-qualified URIs that point to your data in Google Cloud Storage. Each URI can
86-
* contain one '*' wildcard character that must come after the bucket's name. Size limits
87-
* related to load jobs apply to external data sources, plus an additional limit of 10 GB
88-
* maximum size across all URIs.
83+
* Sets the fully-qualified URIs that point to your data in Google Cloud Storage (e.g.
84+
* gs://bucket/path). Each URI can contain one '*' wildcard character that must come after the
85+
* bucket's name. Size limits related to load jobs apply to external data sources, plus an
86+
* additional limit of 10 GB maximum size across all URIs.
8987
*
9088
* @see <a href="https://cloud.google.com/bigquery/loading-data-into-bigquery#quota">Quota</a>
9189
*/
@@ -103,15 +101,14 @@ public Builder schema(Schema schema) {
103101
}
104102

105103
/**
106-
* Sets the source format of the external data. Supported values are {@code CSV} for CSV files,
107-
* and {@code NEWLINE_DELIMITED_JSON} for newline-delimited JSON. If not set, files are assumed
108-
* to be in CSV format.
104+
* Sets the source format, and possibly some parsing options, of the external data. Supported
105+
* formats are {@code CSV} and {@code NEWLINE_DELIMITED_JSON}.
109106
*
110107
* <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
111108
* Source Format</a>
112109
*/
113-
public Builder sourceFormat(String sourceFormat) {
114-
this.sourceFormat = checkNotNull(sourceFormat);
110+
public Builder formatOptions(FormatOptions formatOptions) {
111+
this.formatOptions = checkNotNull(formatOptions);
115112
return this;
116113
}
117114

@@ -129,9 +126,8 @@ public Builder maxBadRecords(Integer maxBadRecords) {
129126
* Sets whether BigQuery should allow extra values that are not represented in the table schema.
130127
* If true, the extra values are ignored. If false, records with extra columns are treated as
131128
* bad records, and if there are too many bad records, an invalid error is returned in the job
132-
* result. The default value is false. The value set with
133-
* {@link #sourceFormat(String)} property determines what
134-
* BigQuery treats as an extra value.
129+
* result. The default value is false. The value set with {@link #formatOptions(FormatOptions)}
130+
* property determines what BigQuery treats as an extra value.
135131
*
136132
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.ignoreUnknownValues">
137133
* Ignore Unknown Values</a>
@@ -152,15 +148,6 @@ public Builder compression(String compression) {
152148
return this;
153149
}
154150

155-
/**
156-
* Sets additional properties to be used to parse CSV data (used when
157-
* {@link #sourceFormat(String)} is set to CSV).
158-
*/
159-
public Builder csvOptions(CsvOptions csvOptions) {
160-
this.csvOptions = csvOptions;
161-
return this;
162-
}
163-
164151
/**
165152
* Creates an {@code ExternalDataConfiguration} object.
166153
*/
@@ -174,9 +161,8 @@ public ExternalDataConfiguration build() {
174161
this.ignoreUnknownValues = builder.ignoreUnknownValues;
175162
this.maxBadRecords = builder.maxBadRecords;
176163
this.schema = builder.schema;
177-
this.sourceFormat = builder.sourceFormat;
164+
this.formatOptions = builder.formatOptions;
178165
this.sourceUris = builder.sourceUris;
179-
this.csvOptions = builder.csvOptions;
180166
}
181167

182168
/**
@@ -193,8 +179,8 @@ public String compression() {
193179
* Returns whether BigQuery should allow extra values that are not represented in the table
194180
* schema. If true, the extra values are ignored. If false, records with extra columns are treated
195181
* as bad records, and if there are too many bad records, an invalid error is returned in the job
196-
* result. The default value is false. The value of {@link #sourceFormat()} determines what
197-
* BigQuery treats as an extra value.
182+
* result. The default value is false. The value of {@link #format()} determines what BigQuery
183+
* treats as an extra value.
198184
*
199185
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.ignoreUnknownValues">
200186
* Ignore Unknown Values</a>
@@ -219,13 +205,13 @@ public Schema schema() {
219205
}
220206

221207
/**
222-
* Sets the source format of the external data.
208+
* Returns the source format of the external data.
223209
*
224210
* <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
225211
* Source Format</a>
226212
*/
227-
public String sourceFormat() {
228-
return sourceFormat;
213+
public String format() {
214+
return formatOptions.type();
229215
}
230216

231217
/**
@@ -241,11 +227,11 @@ public List<String> sourceUris() {
241227
}
242228

243229
/**
244-
* Returns additional properties used to parse CSV data (used when {@link #sourceFormat()} is set
245-
* to CSV).
230+
* Returns additional properties used to parse CSV data (used when {@link #format()} is set to
231+
* CSV). Returns {@code null} if not set.
246232
*/
247233
public CsvOptions csvOptions() {
248-
return csvOptions;
234+
return formatOptions instanceof CsvOptions ? (CsvOptions) formatOptions : null;
249235
}
250236

251237
/**
@@ -257,28 +243,26 @@ public Builder toBuilder() {
257243
.ignoreUnknownValues(ignoreUnknownValues)
258244
.maxBadRecords(maxBadRecords)
259245
.schema(schema)
260-
.sourceFormat(sourceFormat)
261-
.sourceUris(sourceUris)
262-
.csvOptions(csvOptions);
246+
.formatOptions(formatOptions)
247+
.sourceUris(sourceUris);
263248
}
264249

265250
@Override
266251
public String toString() {
267252
return MoreObjects.toStringHelper(this)
268253
.add("sourceUris", sourceUris)
269-
.add("sourceFormat", sourceFormat)
254+
.add("formatOptions", formatOptions)
270255
.add("schema", schema)
271256
.add("compression", compression)
272257
.add("ignoreUnknownValues", ignoreUnknownValues)
273258
.add("maxBadRecords", maxBadRecords)
274-
.add("csvOptions", csvOptions)
275259
.toString();
276260
}
277261

278262
@Override
279263
public int hashCode() {
280-
return Objects.hash(compression, ignoreUnknownValues, maxBadRecords, schema, sourceFormat,
281-
sourceUris, csvOptions);
264+
return Objects.hash(compression, ignoreUnknownValues, maxBadRecords, schema, formatOptions,
265+
sourceUris);
282266
}
283267

284268
@Override
@@ -302,14 +286,14 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toPb() {
302286
if (schema != null) {
303287
externalConfigurationPb.setSchema(schema.toPb());
304288
}
305-
if (sourceFormat != null) {
306-
externalConfigurationPb.setSourceFormat(sourceFormat);
289+
if (formatOptions != null) {
290+
externalConfigurationPb.setSourceFormat(formatOptions.type());
307291
}
308292
if (sourceUris != null) {
309293
externalConfigurationPb.setSourceUris(sourceUris);
310294
}
311-
if (csvOptions != null) {
312-
externalConfigurationPb.setCsvOptions(csvOptions.toPb());
295+
if (csvOptions() != null) {
296+
externalConfigurationPb.setCsvOptions(csvOptions().toPb());
313297
}
314298
return externalConfigurationPb;
315299
}
@@ -329,8 +313,8 @@ com.google.api.services.bigquery.model.ExternalDataConfiguration toPb() {
329313
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
330314
* Source Format</a>
331315
*/
332-
public static Builder builder(List<String> sourceUris, Schema schema, String format) {
333-
return new Builder().sourceUris(sourceUris).schema(schema).sourceFormat(format);
316+
public static Builder builder(List<String> sourceUris, Schema schema, FormatOptions format) {
317+
return new Builder().sourceUris(sourceUris).schema(schema).formatOptions(format);
334318
}
335319

336320
/**
@@ -347,11 +331,11 @@ public static Builder builder(List<String> sourceUris, Schema schema, String for
347331
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
348332
* Source Format</a>
349333
*/
350-
public static Builder builder(String sourceUri, Schema schema, String format) {
334+
public static Builder builder(String sourceUri, Schema schema, FormatOptions format) {
351335
return new Builder()
352336
.sourceUris(ImmutableList.of(sourceUri))
353337
.schema(schema)
354-
.sourceFormat(format);
338+
.formatOptions(format);
355339
}
356340

357341
/**
@@ -369,8 +353,8 @@ public static Builder builder(String sourceUri, Schema schema, String format) {
369353
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
370354
* Source Format</a>
371355
*/
372-
public static ExternalDataConfiguration of(
373-
List<String> sourceUris, Schema schema, String format) {
356+
public static ExternalDataConfiguration of(List<String> sourceUris, Schema schema,
357+
FormatOptions format) {
374358
return builder(sourceUris, schema, format).build();
375359
}
376360

@@ -388,7 +372,8 @@ public static ExternalDataConfiguration of(
388372
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
389373
* Source Format</a>
390374
*/
391-
public static ExternalDataConfiguration of(String sourceUri, Schema schema, String format) {
375+
public static ExternalDataConfiguration of(String sourceUri, Schema schema,
376+
FormatOptions format) {
392377
return builder(sourceUri, schema, format).build();
393378
}
394379

@@ -402,7 +387,7 @@ static ExternalDataConfiguration fromPb(
402387
builder.schema(Schema.fromPb(externalDataConfiguration.getSchema()));
403388
}
404389
if (externalDataConfiguration.getSourceFormat() != null) {
405-
builder.sourceFormat(externalDataConfiguration.getSourceFormat());
390+
builder.formatOptions(FormatOptions.of(externalDataConfiguration.getSourceFormat()));
406391
}
407392
if (externalDataConfiguration.getCompression() != null) {
408393
builder.compression(externalDataConfiguration.getCompression());
@@ -411,7 +396,7 @@ static ExternalDataConfiguration fromPb(
411396
builder.ignoreUnknownValues(externalDataConfiguration.getIgnoreUnknownValues());
412397
}
413398
if (externalDataConfiguration.getCsvOptions() != null) {
414-
builder.csvOptions(CsvOptions.fromPb(externalDataConfiguration.getCsvOptions()));
399+
builder.formatOptions(CsvOptions.fromPb(externalDataConfiguration.getCsvOptions()));
415400
}
416401
if (externalDataConfiguration.getMaxBadRecords() != null) {
417402
builder.maxBadRecords(externalDataConfiguration.getMaxBadRecords());

0 commit comments

Comments
 (0)