/
ExternalTableDefinition.java
481 lines (437 loc) · 21.1 KB
/
ExternalTableDefinition.java
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Copyright 2016 Google LLC
*
* 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 static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import com.google.api.services.bigquery.model.ExternalDataConfiguration;
import com.google.api.services.bigquery.model.Table;
import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import java.util.List;
import javax.annotation.Nullable;
/**
* Google BigQuery external table definition. BigQuery's external tables are tables whose data
* reside outside of BigQuery but can be queried as normal BigQuery tables. External tables are
* experimental and might be subject to change or removed.
*
* @see <a href="https://cloud.google.com/bigquery/federated-data-sources">Federated Data Sources
* </a>
*/
@AutoValue
public abstract class ExternalTableDefinition extends TableDefinition {
static final Function<ExternalDataConfiguration, ExternalTableDefinition>
FROM_EXTERNAL_DATA_FUNCTION =
new Function<ExternalDataConfiguration, ExternalTableDefinition>() {
@Override
public ExternalTableDefinition apply(ExternalDataConfiguration pb) {
return ExternalTableDefinition.fromExternalDataConfiguration(pb);
}
};
static final Function<ExternalTableDefinition, ExternalDataConfiguration>
TO_EXTERNAL_DATA_FUNCTION =
new Function<ExternalTableDefinition, ExternalDataConfiguration>() {
@Override
public ExternalDataConfiguration apply(ExternalTableDefinition tableInfo) {
return tableInfo.toExternalDataConfigurationPb();
}
};
private static final long serialVersionUID = -5951580238459622025L;
@AutoValue.Builder
public abstract static class Builder
extends TableDefinition.Builder<ExternalTableDefinition, Builder> {
/**
* Sets the fully-qualified URIs that point to your data in Google Cloud Storage (e.g.
* gs://bucket/path). Each URI can contain one '*' wildcard character that must come after the
* bucket's name. Size limits related to load jobs apply to external data sources, plus an
* additional limit of 10 GB maximum size across all URIs.
*
* <p>For Google Cloud Bigtable URIs: Exactly one URI can be specified and it has be a fully
* specified and valid HTTPS URL for a Google Cloud Bigtable table.
*
* <p>For Google Cloud Datastore backup URIs: Exactly one URI can be specified. Also, the '*'
* wildcard character is not allowed.
*
* @see <a href="https://cloud.google.com/bigquery/loading-data-into-bigquery#quota">Quota</a>
*/
public Builder setSourceUris(List<String> sourceUris) {
return setSourceUrisImmut(ImmutableList.copyOf(sourceUris));
}
abstract Builder setSourceUrisImmut(ImmutableList<String> sourceUris);
/**
* Sets the source format, and possibly some parsing options, of the external data. Supported
* formats are {@code CSV} and {@code NEWLINE_DELIMITED_JSON}.
*
* <p><a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
* Source Format</a>
*/
public Builder setFormatOptions(FormatOptions formatOptions) {
return setFormatOptionsInner(formatOptions);
}
abstract Builder setFormatOptionsInner(FormatOptions formatOptions);
/**
* Sets the maximum number of bad records that BigQuery can ignore when reading data. If the
* number of bad records exceeds this value, an invalid error is returned in the job result. The
* default value is 0, which requires that all records are valid.
*/
public abstract Builder setMaxBadRecords(Integer maxBadRecords);
/**
* Sets whether BigQuery should allow extra values that are not represented in the table schema.
* If true, the extra values are ignored. If false, records with extra columns are treated as
* bad records, and if there are too many bad records, an invalid error is returned in the job
* result. The default value is false. The value set with {@link
* #setFormatOptions(FormatOptions)} property determines what BigQuery treats as an extra value.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.ignoreUnknownValues">
* Ignore Unknown Values</a>
*/
public abstract Builder setIgnoreUnknownValues(Boolean ignoreUnknownValues);
/**
* Sets compression type of the data source. By default no compression is assumed.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.compression">
* Compression</a>
*/
public abstract Builder setCompression(String compression);
/**
* [Experimental] Sets detection of schema and format options automatically. Any option
* specified explicitly will be honored.
*/
public abstract Builder setAutodetect(Boolean autodetect);
public abstract Builder setType(Type type);
/** Sets the table schema. */
public abstract Builder setSchema(Schema schema);
/** Sets the table Hive partitioning options. */
public Builder setHivePartitioningOptions(HivePartitioningOptions hivePartitioningOptions) {
return setHivePartitioningOptionsInner(hivePartitioningOptions);
};
abstract Builder setHivePartitioningOptionsInner(
HivePartitioningOptions hivePartitioningOptions);
/** Creates an {@code ExternalTableDefinition} object. */
@Override
public abstract ExternalTableDefinition build();
}
/**
* Returns the compression type of the data source.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.compression">
* Compression</a>
*/
@Nullable
public abstract String getCompression();
/**
* Returns whether BigQuery should allow extra values that are not represented in the table
* schema. If true, the extra values are ignored. If false, records with extra columns are treated
* as bad records, and if there are too many bad records, an invalid error is returned in the job
* result. The default value is false. The value of {@link #getFormatOptions()} determines what
* BigQuery treats as an extra value.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.ignoreUnknownValues">
* Ignore Unknown Values</a>
*/
@Nullable
public Boolean ignoreUnknownValues() {
return getIgnoreUnknownValues();
};
@Nullable
public abstract Boolean getIgnoreUnknownValues();
/**
* Returns the maximum number of bad records that BigQuery can ignore when reading data. If the
* number of bad records exceeds this value, an invalid error is returned in the job result.
*/
@Nullable
public abstract Integer getMaxBadRecords();
/**
* Returns the fully-qualified URIs that point to your data in Google Cloud Storage. Each URI can
* contain one '*' wildcard character that must come after the bucket's name. Size limits related
* to load jobs apply to external data sources, plus an additional limit of 10 GB maximum size
* across all URIs.
*
* @see <a href="https://cloud.google.com/bigquery/loading-data-into-bigquery#quota">Quota</a>
*/
@Nullable
public List<String> getSourceUris() {
return getSourceUrisImmut();
}
@Nullable
public abstract ImmutableList<String> getSourceUrisImmut();
/**
* Returns the source format, and possibly some parsing options, of the external data. Supported
* formats are {@code CSV} and {@code NEWLINE_DELIMITED_JSON}.
*/
@SuppressWarnings("unchecked")
@Nullable
public <F extends FormatOptions> F getFormatOptions() {
return (F) getFormatOptionsInner();
}
@Nullable
abstract FormatOptions getFormatOptionsInner();
/**
* [Experimental] Returns whether automatic detection of schema and format options should be
* performed.
*/
@Nullable
public abstract Boolean getAutodetect();
/**
* [Experimental] Returns the HivePartitioningOptions when the data layout follows Hive
* partitioning convention
*/
@SuppressWarnings("unchecked")
@Nullable
public HivePartitioningOptions getHivePartitioningOptions() {
return getHivePartitioningOptionsInner();
}
@Nullable
abstract HivePartitioningOptions getHivePartitioningOptionsInner();
/** Returns a builder for the {@code ExternalTableDefinition} object. */
public abstract Builder toBuilder();
@Override
com.google.api.services.bigquery.model.Table toPb() {
Table tablePb = super.toPb();
tablePb.setExternalDataConfiguration(toExternalDataConfigurationPb());
return tablePb;
}
com.google.api.services.bigquery.model.ExternalDataConfiguration toExternalDataConfigurationPb() {
com.google.api.services.bigquery.model.ExternalDataConfiguration externalConfigurationPb =
new com.google.api.services.bigquery.model.ExternalDataConfiguration();
if (getCompression() != null) {
externalConfigurationPb.setCompression(getCompression());
}
if (ignoreUnknownValues() != null) {
externalConfigurationPb.setIgnoreUnknownValues(ignoreUnknownValues());
}
if (getMaxBadRecords() != null) {
externalConfigurationPb.setMaxBadRecords(getMaxBadRecords());
}
if (getSchema() != null) {
externalConfigurationPb.setSchema(getSchema().toPb());
}
if (getFormatOptions() != null) {
externalConfigurationPb.setSourceFormat(getFormatOptions().getType());
}
if (getSourceUris() != null) {
externalConfigurationPb.setSourceUris(getSourceUris());
}
if (getFormatOptions() != null && FormatOptions.CSV.equals(getFormatOptions().getType())) {
externalConfigurationPb.setCsvOptions(((CsvOptions) getFormatOptions()).toPb());
}
if (getFormatOptions() != null
&& FormatOptions.GOOGLE_SHEETS.equals(getFormatOptions().getType())) {
externalConfigurationPb.setGoogleSheetsOptions(
((GoogleSheetsOptions) getFormatOptions()).toPb());
}
if (getFormatOptions() != null && FormatOptions.BIGTABLE.equals(getFormatOptions().getType())) {
externalConfigurationPb.setBigtableOptions(((BigtableOptions) getFormatOptions()).toPb());
}
if (getAutodetect() != null) {
externalConfigurationPb.setAutodetect(getAutodetect());
}
if (getHivePartitioningOptions() != null) {
externalConfigurationPb.setHivePartitioningOptions(getHivePartitioningOptions().toPb());
}
return externalConfigurationPb;
}
static Builder newBuilder() {
return new AutoValue_ExternalTableDefinition.Builder().setType(Type.EXTERNAL);
}
/**
* Creates a builder for an ExternalTableDefinition object.
*
* @param sourceUris the fully-qualified URIs that point to your data in Google Cloud Storage.
* Each URI can contain one '*' wildcard character that must come after the bucket's name.
* Size limits related to load jobs apply to external data sources, plus an additional limit
* of 10 GB maximum size across all URIs.
* @param schema the schema for the external data
* @param format the source format of the external data
* @return a builder for an ExternalTableDefinition object given source URIs, schema and format
* @see <a href="https://cloud.google.com/bigquery/loading-data-into-bigquery#quota">Quota</a>
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
* Source Format</a>
*/
public static Builder newBuilder(List<String> sourceUris, Schema schema, FormatOptions format) {
return newBuilder().setSourceUris(sourceUris).setSchema(schema).setFormatOptions(format);
}
/**
* Creates a builder for an ExternalTableDefinition object.
*
* @param sourceUri a fully-qualified URI that points to your data in Google Cloud Storage. The
* URI can contain one '*' wildcard character that must come after the bucket's name. Size
* limits related to load jobs apply to external data sources.
* @param schema the schema for the external data
* @param format the source format of the external data
* @return a builder for an ExternalTableDefinition object given source URI, schema and format
* @see <a href="https://cloud.google.com/bigquery/loading-data-into-bigquery#quota">Quota</a>
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
* Source Format</a>
*/
public static Builder newBuilder(String sourceUri, Schema schema, FormatOptions format) {
checkArgument(!isNullOrEmpty(sourceUri), "Provided sourceUri is null or empty");
return newBuilder(ImmutableList.of(sourceUri), schema, format);
}
/**
* Creates a builder for an ExternalTableDefinition object.
*
* @param sourceUri the fully-qualified URIs that point to your data in Google Cloud. For Google
* Cloud Bigtable URIs: Exactly one URI can be specified and it has be a fully specified and
* valid HTTPS URL for a Google Cloud Bigtable table. Size limits related to load jobs apply
* to external data sources, plus an additional limit of 10 GB maximum size across all URIs.
* @param format the source format of the external data
* @return a builder for an ExternalTableDefinition object given source URIs and format
* @see <a href="https://cloud.google.com/bigquery/loading-data-into-bigquery#quota">Quota</a>
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
* Source Format</a>
*/
public static Builder newBuilder(String sourceUri, FormatOptions format) {
checkArgument(!isNullOrEmpty(sourceUri), "Provided sourceUri is null or empty");
return newBuilder().setSourceUris(ImmutableList.of(sourceUri)).setFormatOptions(format);
}
/**
* Creates an ExternalTableDefinition object.
*
* @param sourceUris the fully-qualified URIs that point to your data in Google Cloud Storage.
* Each URI can contain one '*' wildcard character that must come after the bucket's name.
* Size limits related to load jobs apply to external data sources, plus an additional limit
* of 10 GB maximum size across all URIs.
* @param schema the schema for the external data
* @param format the source format of the external data
* @return an ExternalTableDefinition object given source URIs, schema and format
* @see <a href="https://cloud.google.com/bigquery/loading-data-into-bigquery#quota">Quota</a>
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
* Source Format</a>
*/
public static ExternalTableDefinition of(
List<String> sourceUris, Schema schema, FormatOptions format) {
return newBuilder(sourceUris, schema, format).build();
}
/**
* Creates an ExternalTableDefinition object.
*
* @param sourceUri a fully-qualified URI that points to your data in Google Cloud Storage. The
* URI can contain one '*' wildcard character that must come after the bucket's name. Size
* limits related to load jobs apply to external data sources.
* @param schema the schema for the external data
* @param format the source format of the external data
* @return an ExternalTableDefinition object given source URIs, schema and format
* @see <a href="https://cloud.google.com/bigquery/loading-data-into-bigquery#quota">Quota</a>
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
* Source Format</a>
*/
public static ExternalTableDefinition of(String sourceUri, Schema schema, FormatOptions format) {
return newBuilder(sourceUri, schema, format).build();
}
/**
* Creates a builder for an ExternalTableDefinition object.
*
* @param sourceUri the fully-qualified URIs that point to your data in Google Cloud. For Google
* Cloud Bigtable URIs: Exactly one URI can be specified and it has be a fully specified and
* valid HTTPS URL for a Google Cloud Bigtable table. Size limits related to load jobs apply
* to external data sources, plus an additional limit of 10 GB maximum size across all URIs.
* @param format the source format of the external data
* @return a builder for an ExternalTableDefinition object given source URIs and format
* @see <a href="https://cloud.google.com/bigquery/loading-data-into-bigquery#quota">Quota</a>
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration.sourceFormat">
* Source Format</a>
*/
public static ExternalTableDefinition of(String sourceUri, FormatOptions format) {
return newBuilder(sourceUri, format).build();
}
@SuppressWarnings("unchecked")
static ExternalTableDefinition fromPb(Table tablePb) {
Builder builder = newBuilder().table(tablePb);
com.google.api.services.bigquery.model.ExternalDataConfiguration externalDataConfiguration =
tablePb.getExternalDataConfiguration();
if (externalDataConfiguration != null) {
if (externalDataConfiguration.getSourceUris() != null) {
builder.setSourceUris(ImmutableList.copyOf(externalDataConfiguration.getSourceUris()));
}
if (externalDataConfiguration.getSourceFormat() != null) {
builder.setFormatOptions(FormatOptions.of(externalDataConfiguration.getSourceFormat()));
}
builder.setCompression(externalDataConfiguration.getCompression());
builder.setIgnoreUnknownValues(externalDataConfiguration.getIgnoreUnknownValues());
if (externalDataConfiguration.getCsvOptions() != null) {
builder.setFormatOptions(CsvOptions.fromPb(externalDataConfiguration.getCsvOptions()));
}
if (externalDataConfiguration.getGoogleSheetsOptions() != null) {
builder.setFormatOptions(
GoogleSheetsOptions.fromPb(externalDataConfiguration.getGoogleSheetsOptions()));
}
if (externalDataConfiguration.getBigtableOptions() != null) {
builder.setFormatOptions(
BigtableOptions.fromPb(externalDataConfiguration.getBigtableOptions()));
}
builder.setMaxBadRecords(externalDataConfiguration.getMaxBadRecords());
builder.setAutodetect(externalDataConfiguration.getAutodetect());
if (externalDataConfiguration.getHivePartitioningOptions() != null) {
builder.setHivePartitioningOptions(
HivePartitioningOptions.fromPb(externalDataConfiguration.getHivePartitioningOptions()));
}
}
return builder.build();
}
static ExternalTableDefinition fromExternalDataConfiguration(
ExternalDataConfiguration externalDataConfiguration) {
Builder builder = newBuilder();
if (externalDataConfiguration.getSourceUris() != null) {
builder.setSourceUris(externalDataConfiguration.getSourceUris());
}
if (externalDataConfiguration.getSchema() != null) {
builder.setSchema(Schema.fromPb(externalDataConfiguration.getSchema()));
}
if (externalDataConfiguration.getSourceFormat() != null) {
builder.setFormatOptions(FormatOptions.of(externalDataConfiguration.getSourceFormat()));
}
if (externalDataConfiguration.getCompression() != null) {
builder.setCompression(externalDataConfiguration.getCompression());
}
if (externalDataConfiguration.getIgnoreUnknownValues() != null) {
builder.setIgnoreUnknownValues(externalDataConfiguration.getIgnoreUnknownValues());
}
if (externalDataConfiguration.getCsvOptions() != null) {
builder.setFormatOptions(CsvOptions.fromPb(externalDataConfiguration.getCsvOptions()));
}
if (externalDataConfiguration.getGoogleSheetsOptions() != null) {
builder.setFormatOptions(
GoogleSheetsOptions.fromPb(externalDataConfiguration.getGoogleSheetsOptions()));
}
if (externalDataConfiguration.getBigtableOptions() != null) {
builder.setFormatOptions(
BigtableOptions.fromPb(externalDataConfiguration.getBigtableOptions()));
}
if (externalDataConfiguration.getMaxBadRecords() != null) {
builder.setMaxBadRecords(externalDataConfiguration.getMaxBadRecords());
}
if (externalDataConfiguration.getAutodetect() != null) {
builder.setAutodetect(externalDataConfiguration.getAutodetect());
}
if (externalDataConfiguration.getHivePartitioningOptions() != null) {
builder.setHivePartitioningOptions(
HivePartitioningOptions.fromPb(externalDataConfiguration.getHivePartitioningOptions()));
}
return builder.build();
}
}