forked from googleapis/java-bigquery
/
BigQuery.java
1676 lines (1510 loc) · 54.6 KB
/
BigQuery.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
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015 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 com.google.api.core.InternalApi;
import com.google.api.gax.paging.Page;
import com.google.cloud.FieldSelector;
import com.google.cloud.FieldSelector.Helper;
import com.google.cloud.Policy;
import com.google.cloud.RetryOption;
import com.google.cloud.Service;
import com.google.cloud.bigquery.spi.v2.BigQueryRpc;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* An interface for Google Cloud BigQuery.
*
* @see <a href="https://cloud.google.com/bigquery/what-is-bigquery">Google Cloud BigQuery</a>
*/
public interface BigQuery extends Service<BigQueryOptions> {
/**
* Fields of a BigQuery Dataset resource.
*
* @see <a href= "https://cloud.google.com/bigquery/docs/reference/v2/datasets#resource">Dataset
* Resource</a>
*/
enum DatasetField implements FieldSelector {
ACCESS("access"),
CREATION_TIME("creationTime"),
DATASET_REFERENCE("datasetReference"),
DEFAULT_TABLE_EXPIRATION_MS("defaultTableExpirationMsS"),
DESCRIPTION("description"),
ETAG("etag"),
FRIENDLY_NAME("friendlyName"),
ID("id"),
LABELS("labels"),
LAST_MODIFIED_TIME("lastModifiedTime"),
LOCATION("location"),
SELF_LINK("selfLink");
static final List<? extends FieldSelector> REQUIRED_FIELDS =
ImmutableList.of(DATASET_REFERENCE);
private final String selector;
DatasetField(String selector) {
this.selector = selector;
}
@Override
public String getSelector() {
return selector;
}
}
/**
* Fields of a BigQuery Table resource.
*
* @see <a href= "https://cloud.google.com/bigquery/docs/reference/v2/tables#resource">Table
* Resource</a>
*/
enum TableField implements FieldSelector {
CREATION_TIME("creationTime"),
DESCRIPTION("description"),
ETAG("etag"),
EXPIRATION_TIME("expirationTime"),
EXTERNAL_DATA_CONFIGURATION("externalDataConfiguration"),
FRIENDLY_NAME("friendlyName"),
ID("id"),
LABELS("labels"),
LAST_MODIFIED_TIME("lastModifiedTime"),
LOCATION("location"),
NUM_BYTES("numBytes"),
NUM_LONG_TERM_BYTES("numLongTermBytes"),
NUM_ROWS("numRows"),
SCHEMA("schema"),
SELF_LINK("selfLink"),
STREAMING_BUFFER("streamingBuffer"),
TABLE_REFERENCE("tableReference"),
TIME_PARTITIONING("timePartitioning"),
RANGE_PARTITIONING("rangePartitioning"),
TYPE("type"),
VIEW("view");
static final List<? extends FieldSelector> REQUIRED_FIELDS =
ImmutableList.of(TABLE_REFERENCE, TYPE);
private final String selector;
TableField(String selector) {
this.selector = selector;
}
@Override
public String getSelector() {
return selector;
}
}
/**
* Fields of a BigQuery Model resource.
*
* @see <a href= "https://cloud.google.com/bigquery/docs/reference/v2/models#resource">Model
* Resource</a>
*/
enum ModelField implements FieldSelector {
CREATION_TIME("creationTime"),
DESCRIPTION("description"),
ETAG("etag"),
EXPIRATION_TIME("expirationTime"),
FRIENDLY_NAME("friendlyName"),
LABELS("labels"),
LAST_MODIFIED_TIME("lastModifiedTime"),
LOCATION("location"),
MODEL_REFERENCE("modelReference"),
TRAINING_RUNS("trainingRuns"),
LABEL_COLUMNS("labelColumns"),
FEATURE_COLUMNS("featureColumns"),
TYPE("modelType");
static final List<? extends FieldSelector> REQUIRED_FIELDS = ImmutableList.of(MODEL_REFERENCE);
private final String selector;
ModelField(String selector) {
this.selector = selector;
}
@Override
public String getSelector() {
return selector;
}
}
/**
* Fields of a BigQuery Routine resource.
*
* @see <a href= "https://cloud.google.com/bigquery/docs/reference/v2/routines#resource">Routine
* Resource</a>
*/
enum RoutineField implements FieldSelector {
ARGUMENTS("arguments"),
CREATION_TIME("creationTime"),
DEFINITION_BODY("definitionBody"),
ETAG("etag"),
IMPORTED_LIBRARIES("importedLibraries"),
LANGUAGE("language"),
LAST_MODIFIED_TIME("lastModifiedTime"),
RETURN_TYPE("returnType"),
ROUTINE_REFERENCE("routineReference"),
ROUTINE_TYPE("routineType");
static final List<? extends FieldSelector> REQUIRED_FIELDS =
ImmutableList.of(ROUTINE_REFERENCE);
private final String selector;
RoutineField(String selector) {
this.selector = selector;
}
@Override
public String getSelector() {
return selector;
}
}
/**
* Fields of a BigQuery Job resource.
*
* @see <a href= "https://cloud.google.com/bigquery/docs/reference/v2/jobs#resource">Job Resource
* </a>
*/
enum JobField implements FieldSelector {
CONFIGURATION("configuration"),
ETAG("etag"),
ID("id"),
JOB_REFERENCE("jobReference"),
SELF_LINK("selfLink"),
STATISTICS("statistics"),
STATUS("status"),
USER_EMAIL("user_email");
static final List<? extends FieldSelector> REQUIRED_FIELDS =
ImmutableList.of(JOB_REFERENCE, CONFIGURATION);
private final String selector;
JobField(String selector) {
this.selector = selector;
}
@Override
public String getSelector() {
return selector;
}
}
/** Class for specifying dataset list options. */
class DatasetListOption extends Option {
private static final long serialVersionUID = 8660294969063340498L;
private DatasetListOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/**
* Returns an option to specify a label filter. See
* https://cloud.google.com/bigquery/docs/adding-using-labels#filtering_datasets_using_labels
*/
public static DatasetListOption labelFilter(String labelFilter) {
return new DatasetListOption(BigQueryRpc.Option.LABEL_FILTER, labelFilter);
}
/** Returns an option to specify the maximum number of datasets returned per page. */
public static DatasetListOption pageSize(long pageSize) {
return new DatasetListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
}
/** Returns an option to specify the page token from which to start listing datasets. */
public static DatasetListOption pageToken(String pageToken) {
return new DatasetListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken);
}
/** Returns an options to list all datasets, even hidden ones. */
public static DatasetListOption all() {
return new DatasetListOption(BigQueryRpc.Option.ALL_DATASETS, true);
}
}
/** Class for specifying dataset get, create and update options. */
class DatasetOption extends Option {
private static final long serialVersionUID = 1674133909259913250L;
private DatasetOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/**
* Returns an option to specify the dataset's fields to be returned by the RPC call. If this
* option is not provided all dataset's fields are returned. {@code DatasetOption.fields} can be
* used to specify only the fields of interest. {@link Dataset#getDatasetId()} is always
* returned, even if not specified.
*/
public static DatasetOption fields(DatasetField... fields) {
return new DatasetOption(
BigQueryRpc.Option.FIELDS, Helper.selector(DatasetField.REQUIRED_FIELDS, fields));
}
}
/** Class for specifying dataset delete options. */
class DatasetDeleteOption extends Option {
private static final long serialVersionUID = -7166083569900951337L;
private DatasetDeleteOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/**
* Returns an option to delete a dataset even if non-empty. If not provided, attempting to
* delete a non-empty dataset will result in a {@link BigQueryException} being thrown.
*/
public static DatasetDeleteOption deleteContents() {
return new DatasetDeleteOption(BigQueryRpc.Option.DELETE_CONTENTS, true);
}
}
/** Class for specifying table list options. */
class ModelListOption extends Option {
private static final long serialVersionUID = 8660294969063322498L;
private ModelListOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/** Returns an option to specify the maximum number of models returned per page. */
public static ModelListOption pageSize(long pageSize) {
checkArgument(pageSize >= 0);
return new ModelListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
}
/** Returns an option to specify the page token from which to start listing models. */
public static ModelListOption pageToken(String pageToken) {
return new ModelListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken);
}
}
/** Class for specifying routine list options. */
class RoutineListOption extends Option {
private static final long serialVersionUID = 8660294969063312498L;
private RoutineListOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/** Returns an option to specify the maximum number of routines returned per page. */
public static RoutineListOption pageSize(long pageSize) {
checkArgument(pageSize >= 0);
return new RoutineListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
}
/** Returns an option to specify the page token from which to start listing routines. */
public static RoutineListOption pageToken(String pageToken) {
return new RoutineListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken);
}
}
/** Class for specifying table list options. */
class TableListOption extends Option {
private static final long serialVersionUID = 8660294969063340498L;
private TableListOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/** Returns an option to specify the maximum number of tables returned per page. */
public static TableListOption pageSize(long pageSize) {
checkArgument(pageSize >= 0);
return new TableListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
}
/** Returns an option to specify the page token from which to start listing tables. */
public static TableListOption pageToken(String pageToken) {
return new TableListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken);
}
}
/** Class for specifying table get, create and update options. */
class TableOption extends Option {
private static final long serialVersionUID = -1723870134095936772L;
private TableOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/**
* Returns an option to specify the table's fields to be returned by the RPC call. If this
* option is not provided all table's fields are returned. {@code TableOption.fields} can be
* used to specify only the fields of interest. {@link Table#getTableId()} and type (which is
* part of {@link Table#getDefinition()}) are always returned, even if not specified.
*/
public static TableOption fields(TableField... fields) {
return new TableOption(
BigQueryRpc.Option.FIELDS, Helper.selector(TableField.REQUIRED_FIELDS, fields));
}
}
/* Class for specifying IAM options. */
class IAMOption extends Option {
private static final long serialVersionUID = 8607992885371024269L;
private IAMOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
public static IAMOption requestedPolicyVersion(long version) {
return new IAMOption(BigQueryRpc.Option.REQUESTED_POLICY_VERSION, version);
}
}
/** Class for specifying model get, create and update options. */
class ModelOption extends Option {
private static final long serialVersionUID = -1723870134095226772L;
private ModelOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/**
* Returns an option to specify the model's fields to be returned by the RPC call. If this
* option is not provided all model's fields are returned. {@code ModelOption.fields} can be
* used to specify only the fields of interest.
*/
public static ModelOption fields(ModelField... fields) {
return new ModelOption(
BigQueryRpc.Option.FIELDS, Helper.selector(ModelField.REQUIRED_FIELDS, fields));
}
}
/** Class for specifying table get, create and update options. */
class RoutineOption extends Option {
private static final long serialVersionUID = -1723870122095226772L;
private RoutineOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/**
* Returns an option to specify the routines's fields to be returned by the RPC call. If this
* option is not provided all model's fields are returned. {@code RoutineOption.fields} can be
* used to specify only the fields of interest.
*/
public static RoutineOption fields(RoutineField... fields) {
return new RoutineOption(
BigQueryRpc.Option.FIELDS, Helper.selector(RoutineField.REQUIRED_FIELDS, fields));
}
}
/** Class for specifying table data list options. */
class TableDataListOption extends Option {
private static final long serialVersionUID = 8488823381738864434L;
private TableDataListOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/** Returns an option to specify the maximum number of rows returned per page. */
public static TableDataListOption pageSize(long pageSize) {
checkArgument(pageSize >= 0);
return new TableDataListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
}
/** Returns an option to specify the page token from which to start listing table data. */
public static TableDataListOption pageToken(String pageToken) {
return new TableDataListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken);
}
/**
* Returns an option that sets the zero-based index of the row from which to start listing table
* data.
*/
public static TableDataListOption startIndex(long index) {
checkArgument(index >= 0);
return new TableDataListOption(BigQueryRpc.Option.START_INDEX, index);
}
}
/** Class for specifying job list options. */
class JobListOption extends Option {
private static final long serialVersionUID = -8207122131226481423L;
private JobListOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/** Returns an option to list all jobs, even the ones issued by other users. */
public static JobListOption allUsers() {
return new JobListOption(BigQueryRpc.Option.ALL_USERS, true);
}
/** Returns an option to list only jobs that match the provided state filters. */
public static JobListOption stateFilter(JobStatus.State... stateFilters) {
List<String> stringFilters =
Lists.transform(
ImmutableList.copyOf(stateFilters),
new Function<JobStatus.State, String>() {
@Override
public String apply(JobStatus.State state) {
return state.name().toLowerCase();
}
});
return new JobListOption(BigQueryRpc.Option.STATE_FILTER, stringFilters);
}
/** Returns an option to filter out jobs before the given minimum creation time. */
public static JobListOption minCreationTime(long minCreationTime) {
return new JobListOption(BigQueryRpc.Option.MIN_CREATION_TIME, minCreationTime);
}
/** Returns an option to filter out jobs after the given maximum creation time. */
public static JobListOption maxCreationTime(long maxCreationTime) {
return new JobListOption(BigQueryRpc.Option.MAX_CREATION_TIME, maxCreationTime);
}
/** Returns an option to specify the maximum number of jobs returned per page. */
public static JobListOption pageSize(long pageSize) {
checkArgument(pageSize >= 0);
return new JobListOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
}
/** Returns an option to specify the page token from which to start listing jobs. */
public static JobListOption pageToken(String pageToken) {
return new JobListOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken);
}
/** Returns an option to list only child job from specify parent job id. */
public static JobListOption parentJobId(String parentJobId) {
return new JobListOption(BigQueryRpc.Option.PARENT_JOB_ID, parentJobId);
}
/**
* Returns an option to specify the job's fields to be returned by the RPC call. If this option
* is not provided all job's fields are returned. {@code JobOption.fields()} can be used to
* specify only the fields of interest. {@link Job#getJobId()}, {@link JobStatus#getState()},
* {@link JobStatus#getError()} as well as type-specific configuration (e.g. {@link
* QueryJobConfiguration#getQuery()} for Query Jobs) are always returned, even if not specified.
* {@link JobField#SELF_LINK} and {@link JobField#ETAG} can not be selected when listing jobs.
*/
public static JobListOption fields(JobField... fields) {
return new JobListOption(
BigQueryRpc.Option.FIELDS,
Helper.listSelector("jobs", JobField.REQUIRED_FIELDS, fields, "state", "errorResult"));
}
}
/** Class for specifying table get and create options. */
class JobOption extends Option {
private static final long serialVersionUID = -3111736712316353665L;
private JobOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/**
* Returns an option to specify the job's fields to be returned by the RPC call. If this option
* is not provided all job's fields are returned. {@code JobOption.fields()} can be used to
* specify only the fields of interest. {@link Job#getJobId()} as well as type-specific
* configuration (e.g. {@link QueryJobConfiguration#getQuery()} for Query Jobs) are always
* returned, even if not specified.
*/
public static JobOption fields(JobField... fields) {
return new JobOption(
BigQueryRpc.Option.FIELDS, Helper.selector(JobField.REQUIRED_FIELDS, fields));
}
}
/** Class for specifying query results options. */
class QueryResultsOption extends Option {
private static final long serialVersionUID = 3788898503226985525L;
private QueryResultsOption(BigQueryRpc.Option option, Object value) {
super(option, value);
}
/** Returns an option to specify the maximum number of rows returned per page. */
public static QueryResultsOption pageSize(long pageSize) {
checkArgument(pageSize >= 0);
return new QueryResultsOption(BigQueryRpc.Option.MAX_RESULTS, pageSize);
}
/** Returns an option to specify the page token from which to start getting query results. */
public static QueryResultsOption pageToken(String pageToken) {
return new QueryResultsOption(BigQueryRpc.Option.PAGE_TOKEN, pageToken);
}
/**
* Returns an option that sets the zero-based index of the row from which to start getting query
* results.
*/
public static QueryResultsOption startIndex(long startIndex) {
checkArgument(startIndex >= 0);
return new QueryResultsOption(BigQueryRpc.Option.START_INDEX, startIndex);
}
/**
* Returns an option that sets how long to wait for the query to complete, in milliseconds,
* before returning. Default is 10 seconds.
*/
public static QueryResultsOption maxWaitTime(long maxWaitTime) {
checkArgument(maxWaitTime >= 0);
return new QueryResultsOption(BigQueryRpc.Option.TIMEOUT, maxWaitTime);
}
}
class QueryOption implements Serializable {
private static final long serialVersionUID = 6206193419355824689L;
private final Object option;
private QueryOption(Object option) {
this.option = option;
}
public QueryResultsOption getQueryResultsOption() {
return option instanceof QueryResultsOption ? (QueryResultsOption) option : null;
}
public RetryOption getRetryOption() {
return option instanceof RetryOption ? (RetryOption) option : null;
}
static QueryResultsOption[] filterQueryResultsOptions(QueryOption... options) {
List<QueryResultsOption> queryResultOptions = new ArrayList<>(options.length);
for (QueryOption opt : options) {
if (opt.getQueryResultsOption() != null) {
queryResultOptions.add(opt.getQueryResultsOption());
}
}
return queryResultOptions.toArray(new QueryResultsOption[queryResultOptions.size()]);
}
static RetryOption[] filterRetryOptions(QueryOption... options) {
List<RetryOption> retryOptions = new ArrayList<>(options.length);
for (QueryOption opt : options) {
if (opt.getRetryOption() != null) {
retryOptions.add(opt.getRetryOption());
}
}
return retryOptions.toArray(new RetryOption[retryOptions.size()]);
}
public static QueryOption of(QueryResultsOption resultsOption) {
return new QueryOption(resultsOption);
}
public static QueryOption of(RetryOption waitOption) {
return new QueryOption(waitOption);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
QueryOption that = (QueryOption) o;
return option != null ? option.equals(that.option) : that.option == null;
}
@Override
public int hashCode() {
return option != null ? option.hashCode() : 0;
}
}
/**
* Creates a new dataset.
*
* <p>Example of creating a dataset.
*
* <pre>
* {
* @code
* String datasetName = "my_dataset_name";
* Dataset dataset = null;
* DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();
* try {
* // the dataset was created
* dataset = bigquery.create(datasetInfo);
* } catch (BigQueryException e) {
* // the dataset was not created
* }
* }
* </pre>
*
* @throws BigQueryException upon failure
*/
Dataset create(DatasetInfo datasetInfo, DatasetOption... options);
/**
* Creates a new table.
*
* <p>Example of creating a table.
*
* <pre>
* {
* @code
* String datasetName = "my_dataset_name";
* String tableName = "my_table_name";
* String fieldName = "string_field";
* TableId tableId = TableId.of(datasetName, tableName);
* // Table field definition
* Field field = Field.of(fieldName, LegacySQLTypeName.STRING);
* // Table schema definition
* Schema schema = Schema.of(field);
* TableDefinition tableDefinition = StandardTableDefinition.of(schema);
* TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
* Table table = bigquery.create(tableInfo);
* }
* </pre>
*
* @throws BigQueryException upon failure
*/
Table create(TableInfo tableInfo, TableOption... options);
/**
* Creates a new routine.
*
* @throws BigQueryException upon failure
*/
Routine create(RoutineInfo routineInfo, RoutineOption... options);
/**
* Creates a new job.
*
* <p>Example of loading a newline-delimited-json file with textual fields from GCS to a table.
*
* <pre>
* {
* @code
* String datasetName = "my_dataset_name";
* String tableName = "my_table_name";
* String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.json";
* TableId tableId = TableId.of(datasetName, tableName);
* // Table field definition
* Field[] fields = new Field[] { Field.of("name", LegacySQLTypeName.STRING),
* Field.of("post_abbr", LegacySQLTypeName.STRING) };
* // Table schema definition
* Schema schema = Schema.of(fields);
* LoadJobConfiguration configuration = LoadJobConfiguration.builder(tableId, sourceUri)
* .setFormatOptions(FormatOptions.json()).setCreateDisposition(CreateDisposition.CREATE_IF_NEEDED)
* .setSchema(schema).build();
* // Load the table
* Job loadJob = bigquery.create(JobInfo.of(configuration));
* loadJob = loadJob.waitFor();
* // Check the table
* System.out.println("State: " + loadJob.getStatus().getState());
* return ((StandardTableDefinition) bigquery.getTable(tableId).getDefinition()).getNumRows();
* }
* </pre>
*
* <p>Example of creating a query job.
*
* <pre>
* {
* @code
* String query = "SELECT field FROM my_dataset_name.my_table_name";
* Job job = null;
* JobConfiguration jobConfiguration = QueryJobConfiguration.of(query);
* JobInfo jobInfo = JobInfo.of(jobConfiguration);
* try {
* job = bigquery.create(jobInfo);
* } catch (BigQueryException e) {
* // the job was not created
* }
* }
* </pre>
*
* @throws BigQueryException upon failure
*/
Job create(JobInfo jobInfo, JobOption... options);
/**
* Returns the requested dataset or {@code null} if not found.
*
* <p>Example of getting a dataset.
*
* <pre>
* {
* @code
* String datasetName = "my_dataset";
* Dataset dataset = bigquery.getDataset(datasetName);
* }
* </pre>
*
* @throws BigQueryException upon failure
*/
Dataset getDataset(String datasetId, DatasetOption... options);
/**
* Returns the requested dataset or {@code null} if not found.
*
* <p>Example of getting a dataset.
*
* <pre>
* {
* @code
* String projectId = "my_project_id";
* String datasetName = "my_dataset_name";
* DatasetId datasetId = DatasetId.of(projectId, datasetName);
* Dataset dataset = bigquery.getDataset(datasetId);
* }
* </pre>
*
* @throws BigQueryException upon failure
*/
Dataset getDataset(DatasetId datasetId, DatasetOption... options);
/**
* Lists the project's datasets. This method returns partial information on each dataset: ({@link
* Dataset#getDatasetId()}, {@link Dataset#getFriendlyName()} and {@link
* Dataset#getGeneratedId()}). To get complete information use either {@link #getDataset(String,
* DatasetOption...)} or {@link #getDataset(DatasetId, DatasetOption...)}.
*
* <p>Example of listing datasets, specifying the page size.
*
* <pre>
* {
* @code
* // List datasets in the default project
* Page<Dataset> datasets = bigquery.listDatasets(DatasetListOption.pageSize(100));
* for (Dataset dataset : datasets.iterateAll()) {
* // do something with the dataset
* }
* }
* </pre>
*
* @throws BigQueryException upon failure
*/
Page<Dataset> listDatasets(DatasetListOption... options);
/**
* Lists the datasets in the provided project. This method returns partial information on each
* dataset: ({@link Dataset#getDatasetId()}, {@link Dataset#getFriendlyName()} and {@link
* Dataset#getGeneratedId()}). To get complete information use either {@link #getDataset(String,
* DatasetOption...)} or {@link #getDataset(DatasetId, DatasetOption...)}.
*
* <p>Example of listing datasets in a project, specifying the page size.
*
* <pre>
* {
* @code
* String projectId = "my_project_id";
* // List datasets in a specified project
* Page<Dataset> datasets = bigquery.listDatasets(projectId, DatasetListOption.pageSize(100));
* for (Dataset dataset : datasets.iterateAll()) {
* // do something with the dataset
* }
* }
* </pre>
*
* @throws BigQueryException upon failure
*/
Page<Dataset> listDatasets(String projectId, DatasetListOption... options);
/**
* Deletes the requested dataset.
*
* <p>Example of deleting a dataset from its id, even if non-empty.
*
* <pre>
* {
* @code
* String datasetName = "my_dataset_name";
* boolean deleted = bigquery.delete(datasetName, DatasetDeleteOption.deleteContents());
* if (deleted) {
* // the dataset was deleted
* } else {
* // the dataset was not found
* }
* }
* </pre>
*
* @return {@code true} if dataset was deleted, {@code false} if it was not found
* @throws BigQueryException upon failure
*/
boolean delete(String datasetId, DatasetDeleteOption... options);
/**
* Deletes the requested dataset.
*
* <p>Example of deleting a dataset, even if non-empty.
*
* <pre>
* {
* @code
* String projectId = "my_project_id";
* String datasetName = "my_dataset_name";
* DatasetId datasetId = DatasetId.of(projectId, datasetName);
* boolean deleted = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
* if (deleted) {
* // the dataset was deleted
* } else {
* // the dataset was not found
* }
* }
* </pre>
*
* @return {@code true} if dataset was deleted, {@code false} if it was not found
* @throws BigQueryException upon failure
*/
boolean delete(DatasetId datasetId, DatasetDeleteOption... options);
/**
* Deletes the requested table.
*
* @deprecated Now that BigQuery datasets contain multiple resource types, this invocation is
* ambiguous. Please use more strongly typed version of {@code #delete} that leverages an
* non-ambiguous resource type identifier such as {@code TableId}.
* @return {@code true} if table was deleted, {@code false} if it was not found
* @throws BigQueryException upon failure
*/
@Deprecated
boolean delete(String datasetId, String tableId);
/**
* Deletes the requested table.
*
* <p>Example of deleting a table.
*
* <pre>
* {
* @code
* String projectId = "my_project_id";
* String datasetName = "my_dataset_name";
* String tableName = "my_table_name";
* TableId tableId = TableId.of(projectId, datasetName, tableName);
* boolean deleted = bigquery.delete(tableId);
* if (deleted) {
* // the table was deleted
* } else {
* // the table was not found
* }
* }
* </pre>
*
* @return {@code true} if table was deleted, {@code false} if it was not found
* @throws BigQueryException upon failure
*/
boolean delete(TableId tableId);
/**
* Deletes the requested model.
*
* <p>Example of deleting a model.
*
* <pre>
* {
* @code
* String projectId = "my_project_id";
* String datasetName = "my_dataset_name";
* String tableName = "my_model_name";
* ModelId modelId = ModelId.of(projectId, datasetName, modelName);
* boolean deleted = bigquery.delete(modelId);
* if (deleted) {
* // the model was deleted
* } else {
* // the model was not found
* }
* }
* </pre>
*
* @return {@code true} if model was deleted, {@code false} if it was not found
* @throws BigQueryException upon failure
*/
boolean delete(ModelId modelId);
/**
* Deletes the requested routine.
*
* <p>
* Example of deleting a routine.
*
* <pre>
* {@code
* String projectId = "my_project_id";
* String datasetId = "my_dataset_id";
* String routineId = "my_routine_id";
* RoutineId routineId = RoutineId.of(projectId, datasetId, routineId);
* boolean deleted = bigquery.delete(routineId);
* if (deleted) {
* // the routine was deleted
* } else {
* // the routine was not found
* }
* </pre>
*
* @return {@code true} if routine was deleted, {@code false} if it was not
* found
*
* @throws BigQueryException upon failure
*/
boolean delete(RoutineId routineId);
/**
* Deletes the requested job.
*
* @return {@code true} if job was deleted, {@code false} if it was not found
* @throws BigQueryException upon failure
*/
boolean delete(JobId jobId);
/**
* Updates dataset information.
*
* <p>Example of updating a dataset by changing its description.
* <!--SNIPPET
* bigquery_update_table_description-->
*
* <pre>