Repository navigation
Expand file tree
/
Copy pathExternalTableDefinition.java
More file actions
899 lines (803 loc) · 37.7 KB
/
Copy pathExternalTableDefinition.java
File metadata and controls
899 lines (803 loc) · 37.7 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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/*
* 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.core.ApiFunction;
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.cloud.StringEnumType;
import com.google.cloud.StringEnumValue;
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 = ExternalTableDefinition::fromExternalDataConfiguration;
static final Function<ExternalTableDefinition, ExternalDataConfiguration>
TO_EXTERNAL_DATA_FUNCTION = ExternalTableDefinition::toExternalDataConfigurationPb;
private static final long serialVersionUID = -5951580238459622025L;
public static final class SourceColumnMatch extends StringEnumValue {
private static final long serialVersionUID = 818920627219751207L;
private static final ApiFunction<String, SourceColumnMatch> CONSTRUCTOR =
SourceColumnMatch::new;
private static final StringEnumType<SourceColumnMatch> type =
new StringEnumType<SourceColumnMatch>(SourceColumnMatch.class, CONSTRUCTOR);
public static final SourceColumnMatch POSITION = type.createAndRegister("POSITION");
public static final SourceColumnMatch NAME = type.createAndRegister("NAME");
private SourceColumnMatch(String constant) {
super(constant);
}
/**
* Get the SourceColumnMatch for the given String constant, and throw an exception if the
* constant is not recognized.
*/
public static SourceColumnMatch valueOfStrict(String constant) {
return type.valueOfStrict(constant);
}
/** Get the SourceColumnMatch for the given String constant, and allow unrecognized values. */
public static SourceColumnMatch valueOf(String constant) {
return type.valueOf(constant);
}
/** Return the known values for SourceColumnMatch. */
public static SourceColumnMatch[] values() {
return type.values();
}
}
@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 setFileSetSpecTypeInner(String spec);
abstract Builder setSourceUrisImmut(ImmutableList<String> sourceUris);
/**
* Defines how to interpret files denoted by URIs. By default the files are assumed to be data
* files (this can be specified explicitly via FILE_SET_SPEC_TYPE_FILE_SYSTEM_MATCH). A second
* option is "FILE_SET_SPEC_TYPE_NEW_LINE_DELIMITED_MANIFEST" which interprets each file as a
* manifest file, where each line is a reference to a file.
*/
public Builder setFileSetSpecType(String fileSetSpecType) {
return setFileSetSpecTypeInner(fileSetSpecType);
}
/**
* 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);
}
/**
* Defines the list of possible SQL data types to which the source decimal values are converted.
* This list and the precision and the scale parameters of the decimal field determine the
* target type. In the order of NUMERIC, BIGNUMERIC, and STRING, a type is picked if it is in
* the specified list and if it supports the precision and the scale. STRING supports all
* precision and scale values.
*
* @param decimalTargetTypes decimalTargetType or {@code null} for none
*/
public abstract Builder setDecimalTargetTypes(List<String> decimalTargetTypes);
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);
/**
* [Optional, Trusted Tester] connectionId for external data source. The value may be {@code
* null}.
*/
public abstract Builder setConnectionId(String connectionId);
/**
* [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);
}
;
/**
* When creating an external table, the user can provide a reference file with the table schema.
* This is enabled for the following formats: AVRO, PARQUET, ORC.
*
* @param referenceFileSchemaUri or {@code null} for none
*/
public abstract Builder setReferenceFileSchemaUri(String referenceFileSchemaUri);
abstract Builder setHivePartitioningOptionsInner(
HivePartitioningOptions hivePartitioningOptions);
public Builder setObjectMetadata(String objectMetadata) {
return setObjectMetadataInner(objectMetadata);
}
abstract Builder setObjectMetadataInner(String objectMetadata);
/**
* [Optional] Metadata Cache Mode for the table. Set this to enable caching of metadata from
* external data source.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#metadatacachemode">
* MetadataCacheMode</a>
*/
public Builder setMetadataCacheMode(String metadataCacheMode) {
return setMetadataCacheModeInner(metadataCacheMode);
}
abstract Builder setMetadataCacheModeInner(String metadataCacheMode);
/**
* [Optional] Metadata Cache Mode for the table. Set this to enable caching of metadata from
* external data source.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#resource:-table">
* MaxStaleness</a>
*/
public Builder setMaxStaleness(String maxStaleness) {
return setMaxStalenessInner(maxStaleness);
}
abstract Builder setMaxStalenessInner(String maxStaleness);
/**
* Time zone used when parsing timestamp values that do not have specific time zone information
* (e.g. 2024-04-20 12:34:56). The expected format is a IANA timezone string (e.g.
* America/Los_Angeles).
*/
public abstract Builder setTimeZone(String timeZone);
/** Format used to parse DATE values. Supports C-style and SQL-style values. */
public abstract Builder setDateFormat(String dateFormat);
/** Format used to parse DATETIME values. Supports C-style and SQL-style values. */
public abstract Builder setDatetimeFormat(String datetimeFormat);
/** Format used to parse TIME values. Supports C-style and SQL-style values. */
public abstract Builder setTimeFormat(String timeFormat);
/** Format used to parse TIMESTAMP values. Supports C-style and SQL-style values. */
public abstract Builder setTimestampFormat(String timestampFormat);
/**
* Controls the strategy used to match loaded columns to the schema. If not set, a sensible
* default is chosen based on how the schema is provided. If autodetect is used, then columns
* are matched by name. Otherwise, columns are matched by position. This is done to keep the
* behavior backward-compatible. Acceptable values are: POSITION - matches by position. This
* assumes that the columns are ordered the same way as the schema. NAME - matches by name. This
* reads the header row as column names and reorders columns to match the field names in the
* schema.
*/
public abstract Builder setSourceColumnMatch(SourceColumnMatch sourceColumnMatch);
/**
* A list of strings represented as SQL NULL value in a CSV file. null_marker and null_markers
* can't be set at the same time. If null_marker is set, null_markers has to be not set. If
* null_markers is set, null_marker has to be not set. If both null_marker and null_markers are
* set at the same time, a user error would be thrown. Any strings listed in null_markers,
* including empty string would be interpreted as SQL NULL. This applies to all column types.
*/
public abstract Builder setNullMarkers(List<String> nullMarkers);
/** 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 the connection ID used to connect to external data source.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration">
* ConnectionId</a>
*/
@Nullable
public abstract String getConnectionId();
/**
* 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 String getFileSetSpecType() {
return getFileSetSpecTypeInner();
}
@Nullable
abstract String getFileSetSpecTypeInner();
@Nullable
public abstract ImmutableList<String> getSourceUrisImmut();
/**
* Returns the object metadata.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/v2/tables#externalDataConfiguration">
* ObjectMetadata</a>
*/
@Nullable
public String getObjectMetadata() {
return getObjectMetadataInner();
}
@Nullable
abstract String getObjectMetadataInner();
/**
* Returns the metadata cache mode.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#metadatacachemode">
* MetadataCacheMode</a>
*/
@Nullable
public String getMetadataCacheMode() {
return getMetadataCacheModeInner();
}
@Nullable
abstract String getMetadataCacheModeInner();
/**
* Returns the maximum staleness of data that could be returned when the table is queried.
* Staleness encoded as a string encoding of sql IntervalValue type.
*
* @see <a
* href="hhttps://cloud.google.com/bigquery/docs/reference/rest/v2/tables#resource:-table">
* MaxStaleness</a>
*/
@Nullable
public String getMaxStaleness() {
return getMaxStalenessInner();
}
@Nullable
abstract String getMaxStalenessInner();
/**
* 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();
@Nullable
public abstract ImmutableList<String> getDecimalTargetTypes();
/**
* [Experimental] Returns whether automatic detection of schema and format options should be
* performed.
*/
@Nullable
public abstract Boolean getAutodetect();
@Nullable
public abstract String getReferenceFileSchemaUri();
/**
* [Experimental] Returns the HivePartitioningOptions when the data layout follows Hive
* partitioning convention
*/
@SuppressWarnings("unchecked")
@Nullable
public HivePartitioningOptions getHivePartitioningOptions() {
return getHivePartitioningOptionsInner();
}
/**
* Returns the time zone used when parsing timestamp values that don't have specific time zone
* information.
*/
@Nullable
public abstract String getTimeZone();
/** Returns the format used to parse DATE values. */
@Nullable
public abstract String getDateFormat();
/** Returns the format used to parse DATETIME values. */
@Nullable
public abstract String getDatetimeFormat();
/** Returns the format used to parse TIME values. */
@Nullable
public abstract String getTimeFormat();
/** Returns the format used to parse TIMESTAMP values. */
@Nullable
public abstract String getTimestampFormat();
/** Returns the strategy used to match loaded columns to the schema, either POSITION or NAME. */
@Nullable
public abstract SourceColumnMatch getSourceColumnMatch();
/** Returns a list of strings represented as SQL NULL value in a CSV file. */
@Nullable
public abstract List<String> getNullMarkers();
@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());
if (getMaxStaleness() != null) {
tablePb.setMaxStaleness(getMaxStaleness());
}
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 (getConnectionId() != null) {
externalConfigurationPb.setConnectionId(getConnectionId());
}
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 (getDecimalTargetTypes() != null) {
externalConfigurationPb.setDecimalTargetTypes(getDecimalTargetTypes());
}
if (getFormatOptions() != null && FormatOptions.PARQUET.equals(getFormatOptions().getType())) {
externalConfigurationPb.setParquetOptions(((ParquetOptions) getFormatOptions()).toPb());
}
if (getFormatOptions() != null && FormatOptions.AVRO.equals(getFormatOptions().getType())) {
externalConfigurationPb.setAvroOptions(((AvroOptions) getFormatOptions()).toPb());
}
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 (getReferenceFileSchemaUri() != null) {
externalConfigurationPb.setReferenceFileSchemaUri(getReferenceFileSchemaUri());
}
if (getHivePartitioningOptions() != null) {
externalConfigurationPb.setHivePartitioningOptions(getHivePartitioningOptions().toPb());
}
if (getFileSetSpecType() != null) {
externalConfigurationPb.setFileSetSpecType(getFileSetSpecType());
}
if (getObjectMetadata() != null) {
externalConfigurationPb.setObjectMetadata(getObjectMetadata());
}
if (getMetadataCacheMode() != null) {
externalConfigurationPb.setMetadataCacheMode(getMetadataCacheMode());
}
if (getTimeZone() != null) {
externalConfigurationPb.setTimeZone(getTimeZone());
}
if (getDateFormat() != null) {
externalConfigurationPb.setDateFormat(getDateFormat());
}
if (getDatetimeFormat() != null) {
externalConfigurationPb.setDatetimeFormat(getDatetimeFormat());
}
if (getTimeFormat() != null) {
externalConfigurationPb.setTimeFormat(getTimeFormat());
}
if (getTimestampFormat() != null) {
externalConfigurationPb.setTimestampFormat(getTimestampFormat());
}
if (getSourceColumnMatch() != null) {
externalConfigurationPb
.getCsvOptions()
.setSourceColumnMatch(getSourceColumnMatch().toString());
}
if (getNullMarkers() != null) {
externalConfigurationPb.getCsvOptions().setNullMarkers(getNullMarkers());
}
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 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.
* @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) {
checkArgument(!isNullOrEmpty(sourceUri), "Provided sourceUri is null or empty");
return newBuilder().setSourceUris(ImmutableList.of(sourceUri));
}
/**
* 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.getDecimalTargetTypes() != null) {
builder.setDecimalTargetTypes(
ImmutableList.copyOf(externalDataConfiguration.getDecimalTargetTypes()));
}
if (externalDataConfiguration.getSourceFormat() != null) {
builder.setFormatOptions(FormatOptions.of(externalDataConfiguration.getSourceFormat()));
}
builder.setCompression(externalDataConfiguration.getCompression());
if (externalDataConfiguration.getConnectionId() != null) {
builder.setConnectionId(externalDataConfiguration.getConnectionId());
}
builder.setIgnoreUnknownValues(externalDataConfiguration.getIgnoreUnknownValues());
if (externalDataConfiguration.getAvroOptions() != null) {
builder.setFormatOptions(AvroOptions.fromPb(externalDataConfiguration.getAvroOptions()));
}
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.getParquetOptions() != null) {
builder.setFormatOptions(
ParquetOptions.fromPb(externalDataConfiguration.getParquetOptions()));
}
builder.setMaxBadRecords(externalDataConfiguration.getMaxBadRecords());
builder.setAutodetect(externalDataConfiguration.getAutodetect());
if (externalDataConfiguration.getHivePartitioningOptions() != null) {
builder.setHivePartitioningOptions(
HivePartitioningOptions.fromPb(externalDataConfiguration.getHivePartitioningOptions()));
}
if (externalDataConfiguration.getReferenceFileSchemaUri() != null) {
builder.setReferenceFileSchemaUri(externalDataConfiguration.getReferenceFileSchemaUri());
}
if (externalDataConfiguration.getFileSetSpecType() != null) {
builder.setFileSetSpecType(externalDataConfiguration.getFileSetSpecType());
}
if (externalDataConfiguration.getObjectMetadata() != null) {
builder.setObjectMetadata(externalDataConfiguration.getObjectMetadata());
}
if (externalDataConfiguration.getMetadataCacheMode() != null) {
builder.setMetadataCacheMode(externalDataConfiguration.getMetadataCacheMode());
}
if (tablePb.getMaxStaleness() != null) {
builder.setMaxStaleness(tablePb.getMaxStaleness());
}
if (externalDataConfiguration.getTimeZone() != null) {
builder.setTimeZone(externalDataConfiguration.getTimeZone());
}
if (externalDataConfiguration.getDateFormat() != null) {
builder.setDateFormat(externalDataConfiguration.getDateFormat());
}
if (externalDataConfiguration.getDatetimeFormat() != null) {
builder.setDatetimeFormat(externalDataConfiguration.getDatetimeFormat());
}
if (externalDataConfiguration.getTimeFormat() != null) {
builder.setTimeFormat(externalDataConfiguration.getTimeFormat());
}
if (externalDataConfiguration.getTimestampFormat() != null) {
builder.setTimestampFormat(externalDataConfiguration.getTimestampFormat());
}
if (externalDataConfiguration.getCsvOptions() != null) {
if (externalDataConfiguration.getCsvOptions().getSourceColumnMatch() != null) {
builder.setSourceColumnMatch(
SourceColumnMatch.valueOf(
externalDataConfiguration.getCsvOptions().getSourceColumnMatch()));
}
if (externalDataConfiguration.getCsvOptions().getNullMarkers() != null) {
builder.setNullMarkers(externalDataConfiguration.getCsvOptions().getNullMarkers());
}
}
}
return builder.build();
}
static ExternalTableDefinition fromExternalDataConfiguration(
ExternalDataConfiguration externalDataConfiguration) {
Builder builder = newBuilder();
if (externalDataConfiguration.getSourceUris() != null) {
builder.setSourceUris(externalDataConfiguration.getSourceUris());
}
if (externalDataConfiguration.getDecimalTargetTypes() != null) {
builder.setDecimalTargetTypes(externalDataConfiguration.getDecimalTargetTypes());
}
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.getConnectionId() != null) {
builder.setConnectionId(externalDataConfiguration.getConnectionId());
}
if (externalDataConfiguration.getIgnoreUnknownValues() != null) {
builder.setIgnoreUnknownValues(externalDataConfiguration.getIgnoreUnknownValues());
}
if (externalDataConfiguration.getAvroOptions() != null) {
builder.setFormatOptions(AvroOptions.fromPb(externalDataConfiguration.getAvroOptions()));
}
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.getParquetOptions() != null) {
builder.setFormatOptions(
ParquetOptions.fromPb(externalDataConfiguration.getParquetOptions()));
}
if (externalDataConfiguration.getMaxBadRecords() != null) {
builder.setMaxBadRecords(externalDataConfiguration.getMaxBadRecords());
}
if (externalDataConfiguration.getAutodetect() != null) {
builder.setAutodetect(externalDataConfiguration.getAutodetect());
}
if (externalDataConfiguration.getReferenceFileSchemaUri() != null) {
builder.setReferenceFileSchemaUri(externalDataConfiguration.getReferenceFileSchemaUri());
}
if (externalDataConfiguration.getHivePartitioningOptions() != null) {
builder.setHivePartitioningOptions(
HivePartitioningOptions.fromPb(externalDataConfiguration.getHivePartitioningOptions()));
}
if (externalDataConfiguration.getFileSetSpecType() != null) {
builder.setFileSetSpecType(externalDataConfiguration.getFileSetSpecType());
}
if (externalDataConfiguration.getObjectMetadata() != null) {
builder.setObjectMetadata(externalDataConfiguration.getObjectMetadata());
}
if (externalDataConfiguration.getMetadataCacheMode() != null) {
builder.setMetadataCacheMode(externalDataConfiguration.getMetadataCacheMode());
}
if (externalDataConfiguration.getTimeZone() != null) {
builder.setTimeZone(externalDataConfiguration.getTimeZone());
}
if (externalDataConfiguration.getDateFormat() != null) {
builder.setDateFormat(externalDataConfiguration.getDateFormat());
}
if (externalDataConfiguration.getDatetimeFormat() != null) {
builder.setDatetimeFormat(externalDataConfiguration.getDatetimeFormat());
}
if (externalDataConfiguration.getTimeFormat() != null) {
builder.setTimeFormat(externalDataConfiguration.getTimeFormat());
}
if (externalDataConfiguration.getTimestampFormat() != null) {
builder.setTimestampFormat(externalDataConfiguration.getTimestampFormat());
}
if (externalDataConfiguration.getCsvOptions() != null) {
if (externalDataConfiguration.getCsvOptions().getSourceColumnMatch() != null) {
builder.setSourceColumnMatch(
SourceColumnMatch.valueOf(
externalDataConfiguration.getCsvOptions().getSourceColumnMatch()));
}
if (externalDataConfiguration.getCsvOptions().getNullMarkers() != null) {
builder.setNullMarkers(externalDataConfiguration.getCsvOptions().getNullMarkers());
}
}
return builder.build();
}
}