forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigQueryExample.java
791 lines (733 loc) · 27.6 KB
/
BigQueryExample.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
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.examples.bigquery;
import com.google.cloud.WriteChannel;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryError;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.CopyJobConfiguration;
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetId;
import com.google.cloud.bigquery.DatasetInfo;
import com.google.cloud.bigquery.ExternalTableDefinition;
import com.google.cloud.bigquery.ExtractJobConfiguration;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.FieldValue;
import com.google.cloud.bigquery.FormatOptions;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobId;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.LoadJobConfiguration;
import com.google.cloud.bigquery.QueryRequest;
import com.google.cloud.bigquery.QueryResponse;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;
import com.google.cloud.bigquery.ViewDefinition;
import com.google.cloud.bigquery.WriteChannelConfiguration;
import com.google.cloud.bigquery.spi.BigQueryRpc.Tuple;
import com.google.common.collect.ImmutableMap;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* An example of using Google BigQuery.
*
* <p>This example demonstrates a simple/typical BigQuery usage.
*
* <p>See the
* <a href="https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-examples/README.md">
* README</a> for compilation instructions. Run this code with
* <pre>{@code target/appassembler/bin/BigQueryExample [<project_id>]
* list datasets |
* list tables <dataset> |
* list jobs |
* list data <dataset> <table> |
* info dataset <dataset> |
* info table <dataset> <table> |
* info job <job> |
* create dataset <dataset> |
* create table <dataset> <table> (<fieldName>:<primitiveType>)+ |
* create view <dataset> <table> <query> |
* create external-table <dataset> <table> <format> (<fieldName>:<primitiveType>)+ <sourceUri> |
* delete dataset <dataset> |
* delete table <dataset> <table> |
* cancel <job> |
* copy <sourceDataset> <sourceTable> <destinationDataset> <destinationTable> |
* load <dataset> <table> <format> <sourceUri>+ |
* extract <dataset> <table> <format> <destinationUri>+ |
* query <query> |
* load-file <dataset> <table> <format> <filePath>}</pre>
*
* <p>The first parameter is an optional {@code project_id} (logged-in project will be used if not
* supplied). Second parameter is a BigQuery operation and can be used to demonstrate its usage. For
* operations that apply to more than one entity (`list`, `create`, `info` and `delete`) the third
* parameter specifies the entity. {@code <primitiveType>} indicates that only primitive types are
* supported by the {@code create table} and {@code create external-table} operations
* ({@code string}, {@code float}, {@code integer}, {@code timestamp}, {@code boolean},
* {@code bytes}). {@code <sourceUri>}, {@code <sourceUris>} and {@code <destinationUris>}
* parameters are URIs to Google Cloud Storage blobs, in the form {@code gs://bucket/path}.
* See each action's run method for the specific BigQuery interaction.
*/
public class BigQueryExample {
private static final int CHUNK_SIZE = 8 * 256 * 1024;
private static final Map<String, BigQueryAction> CREATE_ACTIONS = new HashMap<>();
private static final Map<String, BigQueryAction> INFO_ACTIONS = new HashMap<>();
private static final Map<String, BigQueryAction> LIST_ACTIONS = new HashMap<>();
private static final Map<String, BigQueryAction> DELETE_ACTIONS = new HashMap<>();
private static final Map<String, BigQueryAction> ACTIONS = new HashMap<>();
private abstract static class BigQueryAction<T> {
abstract void run(BigQuery bigquery, T arg) throws Exception;
abstract T parse(String... args) throws Exception;
protected String params() {
return "";
}
}
private static class ParentAction extends BigQueryAction<Tuple<BigQueryAction, Object>> {
private final Map<String, BigQueryAction> subActions;
public ParentAction(Map<String, BigQueryAction> subActions) {
this.subActions = ImmutableMap.copyOf(subActions);
}
@Override
@SuppressWarnings("unchecked")
void run(BigQuery bigquery, Tuple<BigQueryAction, Object> subaction) throws Exception {
subaction.x().run(bigquery, subaction.y());
}
@Override
Tuple<BigQueryAction, Object> parse(String... args) throws Exception {
if (args.length >= 1) {
BigQueryAction action = subActions.get(args[0]);
if (action != null) {
Object actionArguments = action.parse(Arrays.copyOfRange(args, 1, args.length));
return Tuple.of(action, actionArguments);
} else {
throw new IllegalArgumentException("Unrecognized entity '" + args[0] + "'.");
}
}
throw new IllegalArgumentException("Missing required entity.");
}
@Override
public String params() {
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, BigQueryAction> entry : subActions.entrySet()) {
builder.append('\n').append(entry.getKey());
String param = entry.getValue().params();
if (param != null && !param.isEmpty()) {
builder.append(' ').append(param);
}
}
return builder.toString();
}
}
private abstract static class NoArgsAction extends BigQueryAction<Void> {
@Override
Void parse(String... args) throws Exception {
if (args.length == 0) {
return null;
}
throw new IllegalArgumentException("This action takes no arguments.");
}
}
/**
* This class demonstrates how to list BigQuery Datasets.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/datasets/list">Datasets: list
* </a>
*/
private static class ListDatasetsAction extends NoArgsAction {
@Override
public void run(BigQuery bigquery, Void arg) {
Iterator<Dataset> datasetInfoIterator = bigquery.listDatasets().iterateAll();
while (datasetInfoIterator.hasNext()) {
System.out.println(datasetInfoIterator.next());
}
}
}
private abstract static class DatasetAction extends BigQueryAction<DatasetId> {
@Override
DatasetId parse(String... args) throws Exception {
String message;
if (args.length == 1) {
return DatasetId.of(args[0]);
} else if (args.length > 1) {
message = "Too many arguments.";
} else {
message = "Missing required dataset id.";
}
throw new IllegalArgumentException(message);
}
@Override
public String params() {
return "<dataset>";
}
}
/**
* This class demonstrates how to list BigQuery Tables in a Dataset.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables/list">Tables: list</a>
*/
private static class ListTablesAction extends DatasetAction {
@Override
public void run(BigQuery bigquery, DatasetId datasetId) {
Iterator<Table> tableInfoIterator = bigquery.listTables(datasetId).iterateAll();
while (tableInfoIterator.hasNext()) {
System.out.println(tableInfoIterator.next());
}
}
}
/**
* This class demonstrates how to retrieve information on a BigQuery Dataset.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/datasets/get">Datasets: get
* </a>
*/
private static class DatasetInfoAction extends DatasetAction {
@Override
public void run(BigQuery bigquery, DatasetId datasetId) {
System.out.println("Dataset info: " + bigquery.getDataset(datasetId));
}
}
/**
* This class demonstrates how to create a BigQuery Dataset.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/datasets/insert">Datasets:
* insert</a>
*/
private static class CreateDatasetAction extends DatasetAction {
@Override
public void run(BigQuery bigquery, DatasetId datasetId) {
bigquery.create(DatasetInfo.builder(datasetId).build());
System.out.println("Created dataset " + datasetId);
}
}
/**
* This class demonstrates how to delete a BigQuery Dataset.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/datasets/delete">Datasets:
* delete</a>
*/
private static class DeleteDatasetAction extends DatasetAction {
@Override
public void run(BigQuery bigquery, DatasetId datasetId) {
if (bigquery.delete(datasetId)) {
System.out.println("Dataset " + datasetId + " was deleted");
} else {
System.out.println("Dataset " + datasetId + " not found");
}
}
}
private abstract static class TableAction extends BigQueryAction<TableId> {
@Override
TableId parse(String... args) throws Exception {
String message;
if (args.length == 2) {
return TableId.of(args[0], args[1]);
} else if (args.length < 2) {
message = "Missing required dataset and table id.";
} else {
message = "Too many arguments.";
}
throw new IllegalArgumentException(message);
}
@Override
public String params() {
return "<dataset> <table>";
}
}
/**
* This class demonstrates how to retrieve information on a BigQuery Table.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables/get">Tables: get</a>
*/
private static class TableInfoAction extends TableAction {
@Override
public void run(BigQuery bigquery, TableId tableId) {
System.out.println("Table info: " + bigquery.getTable(tableId));
}
}
/**
* This class demonstrates how to delete a BigQuery Table.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables/delete">Tables: delete
* </a>
*/
private static class DeleteTableAction extends TableAction {
@Override
public void run(BigQuery bigquery, TableId tableId) {
if (bigquery.delete(tableId)) {
System.out.println("Table " + tableId + " was deleted");
} else {
System.out.println("Table " + tableId + " not found");
}
}
}
/**
* This class demonstrates how to list the rows in a BigQuery Table.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tabledata/list">Tabledata:
* list</a>
*/
private static class ListTableDataAction extends TableAction {
@Override
public void run(BigQuery bigquery, TableId tableId) {
Iterator<List<FieldValue>> iterator = bigquery.listTableData(tableId).iterateAll();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
private abstract static class JobAction extends BigQueryAction<JobId> {
@Override
JobId parse(String... args) throws Exception {
String message;
if (args.length == 1) {
return JobId.of(args[0]);
} else if (args.length > 1) {
message = "Too many arguments.";
} else {
message = "Missing required query.";
}
throw new IllegalArgumentException(message);
}
@Override
public String params() {
return "<job>";
}
}
/**
* This class demonstrates how to list BigQuery Jobs.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/list">Jobs: list</a>
*/
private static class ListJobsAction extends NoArgsAction {
@Override
public void run(BigQuery bigquery, Void arg) {
Iterator<Job> datasetInfoIterator = bigquery.listJobs().iterateAll();
while (datasetInfoIterator.hasNext()) {
System.out.println(datasetInfoIterator.next());
}
}
}
/**
* This class demonstrates how to retrieve information on a BigQuery Job.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/get">Jobs: get</a>
*/
private static class JobInfoAction extends JobAction {
@Override
public void run(BigQuery bigquery, JobId jobId) {
System.out.println("Job info: " + bigquery.getJob(jobId));
}
}
/**
* This class demonstrates how to cancel a BigQuery Job.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/cancel">Jobs: cancel</a>
*/
private static class CancelJobAction extends JobAction {
@Override
public void run(BigQuery bigquery, JobId jobId) {
if (bigquery.cancel(jobId)) {
System.out.println("Requested cancel for job " + jobId);
} else {
System.out.println("Job " + jobId + " not found");
}
}
}
private abstract static class CreateTableAction extends BigQueryAction<TableInfo> {
@Override
void run(BigQuery bigquery, TableInfo table) throws Exception {
Table createTable = bigquery.create(table);
System.out.println("Created table:");
System.out.println(createTable.toString());
}
static Schema parseSchema(String[] args, int start, int end) {
Schema.Builder builder = Schema.builder();
for (int i = start; i < end; i++) {
String[] fieldsArray = args[i].split(":");
if (fieldsArray.length != 2) {
throw new IllegalArgumentException("Unrecognized field definition '" + args[i] + "'.");
}
String fieldName = fieldsArray[0];
String typeString = fieldsArray[1].toLowerCase();
Field.Type fieldType;
switch (typeString) {
case "string":
fieldType = Field.Type.string();
break;
case "integer":
fieldType = Field.Type.integer();
break;
case "timestamp":
fieldType = Field.Type.timestamp();
break;
case "float":
fieldType = Field.Type.floatingPoint();
break;
case "boolean":
fieldType = Field.Type.bool();
break;
case "bytes":
fieldType = Field.Type.bytes();
break;
default:
throw new IllegalArgumentException("Unrecognized field type '" + typeString + "'.");
}
builder.addField(Field.of(fieldName, fieldType));
}
return builder.build();
}
}
/**
* This class demonstrates how to create a simple BigQuery Table (i.e. a table created from a
* {@link StandardTableDefinition}).
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables/insert">Tables: insert
* </a>
*/
private static class CreateSimpleTableAction extends CreateTableAction {
@Override
TableInfo parse(String... args) throws Exception {
if (args.length >= 3) {
String dataset = args[0];
String table = args[1];
TableId tableId = TableId.of(dataset, table);
return TableInfo.of(tableId, StandardTableDefinition.of(parseSchema(args, 2, args.length)));
}
throw new IllegalArgumentException("Missing required arguments.");
}
@Override
protected String params() {
return "<dataset> <table> (<fieldName>:<primitiveType>)+";
}
}
/**
* This class demonstrates how to create a BigQuery External Table (i.e. a table created from a
* {@link ExternalTableDefinition}).
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables/insert">Tables: insert
* </a>
*/
private static class CreateExternalTableAction extends CreateTableAction {
@Override
TableInfo parse(String... args) throws Exception {
if (args.length >= 5) {
String dataset = args[0];
String table = args[1];
TableId tableId = TableId.of(dataset, table);
ExternalTableDefinition externalTableDefinition =
ExternalTableDefinition.of(args[args.length - 1],
parseSchema(args, 3, args.length - 1), FormatOptions.of(args[2]));
return TableInfo.of(tableId, externalTableDefinition);
}
throw new IllegalArgumentException("Missing required arguments.");
}
@Override
protected String params() {
return "<dataset> <table> <format> (<fieldName>:<primitiveType>)+ <sourceUri>";
}
}
/**
* This class demonstrates how to create a BigQuery View Table (i.e. a table created from a
* {@link ViewDefinition}).
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/tables/insert">Tables: insert
* </a>
*/
private static class CreateViewAction extends CreateTableAction {
@Override
TableInfo parse(String... args) throws Exception {
String message;
if (args.length == 3) {
String dataset = args[0];
String table = args[1];
String query = args[2];
TableId tableId = TableId.of(dataset, table);
return TableInfo.of(tableId, ViewDefinition.of(query));
} else if (args.length < 3) {
message = "Missing required dataset id, table id or query.";
} else {
message = "Too many arguments.";
}
throw new IllegalArgumentException(message);
}
@Override
protected String params() {
return "<dataset> <table> <query>";
}
}
private abstract static class JobRunAction extends BigQueryAction<JobInfo> {
@Override
void run(BigQuery bigquery, JobInfo job) throws Exception {
System.out.println("Creating job");
Job startedJob = bigquery.create(job);
while (!startedJob.isDone()) {
System.out.println("Waiting for job " + startedJob.jobId().job() + " to complete");
Thread.sleep(1000L);
}
startedJob = startedJob.reload();
if (startedJob.status().error() == null) {
System.out.println("Job " + startedJob.jobId().job() + " succeeded");
} else {
System.out.println("Job " + startedJob.jobId().job() + " failed");
System.out.println("Error: " + startedJob.status().error());
}
}
}
/**
* This class demonstrates how to create a BigQuery Load Job and wait for it to complete.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/insert">Jobs: insert</a>
*/
private static class LoadAction extends JobRunAction {
@Override
JobInfo parse(String... args) throws Exception {
if (args.length >= 4) {
String dataset = args[0];
String table = args[1];
String format = args[2];
TableId tableId = TableId.of(dataset, table);
LoadJobConfiguration configuration = LoadJobConfiguration.of(
tableId, Arrays.asList(args).subList(3, args.length), FormatOptions.of(format));
return JobInfo.of(configuration);
}
throw new IllegalArgumentException("Missing required arguments.");
}
@Override
protected String params() {
return "<dataset> <table> <format> <sourceUri>+";
}
}
/**
* This class demonstrates how to create a BigQuery Extract Job and wait for it to complete.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/insert">Jobs: insert</a>
*/
private static class ExtractAction extends JobRunAction {
@Override
JobInfo parse(String... args) throws Exception {
if (args.length >= 4) {
String dataset = args[0];
String table = args[1];
String format = args[2];
TableId tableId = TableId.of(dataset, table);
ExtractJobConfiguration configuration = ExtractJobConfiguration.of(
tableId, Arrays.asList(args).subList(3, args.length), format);
return JobInfo.of(configuration);
}
throw new IllegalArgumentException("Missing required arguments.");
}
@Override
protected String params() {
return "<dataset> <table> <format> <destinationUri>+";
}
}
/**
* This class demonstrates how to create a BigQuery Copy Job and wait for it to complete.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/insert">Jobs: insert</a>
*/
private static class CopyAction extends JobRunAction {
@Override
JobInfo parse(String... args) throws Exception {
String message;
if (args.length == 4) {
TableId sourceTableId = TableId.of(args[0], args[1]);
TableId destinationTableId = TableId.of(args[2], args[3]);
return JobInfo.of(CopyJobConfiguration.of(destinationTableId, sourceTableId));
} else if (args.length < 3) {
message = "Missing required source or destination table.";
} else {
message = "Too many arguments.";
}
throw new IllegalArgumentException(message);
}
@Override
protected String params() {
return "<sourceDataset> <sourceTable> <destinationDataset> <destinationTable>";
}
}
/**
* This class demonstrates how to run a BigQuery SQL Query and wait for associated job to
* complete. Results or errors are shown.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/query">Jobs: query</a>
*/
private static class QueryAction extends BigQueryAction<QueryRequest> {
@Override
void run(BigQuery bigquery, QueryRequest queryRequest) throws Exception {
System.out.println("Running query");
QueryResponse queryResponse = bigquery.query(queryRequest);
while (!queryResponse.jobCompleted()) {
System.out.println("Waiting for query job " + queryResponse.jobId() + " to complete");
Thread.sleep(1000L);
queryResponse = bigquery.getQueryResults(queryResponse.jobId());
}
if (!queryResponse.hasErrors()) {
System.out.println("Query succeeded. Results:");
Iterator<List<FieldValue>> iterator = queryResponse.result().iterateAll();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
} else {
System.out.println("Query completed with errors. Errors:");
for (BigQueryError err : queryResponse.executionErrors()) {
System.out.println(err);
}
}
}
@Override
QueryRequest parse(String... args) throws Exception {
String message;
if (args.length == 1) {
return QueryRequest.of(args[0]);
} else if (args.length > 1) {
message = "Too many arguments.";
} else {
message = "Missing required query.";
}
throw new IllegalArgumentException(message);
}
@Override
protected String params() {
return "<query>";
}
}
/**
* This class demonstrates how to load data into a BigQuery Table from a local file.
*
* @see <a href="https://cloud.google.com/bigquery/loading-data-post-request#resumable">Resumable
* Upload</a>
*/
private static class LoadFileAction
extends BigQueryAction<Tuple<WriteChannelConfiguration, String>> {
@Override
void run(BigQuery bigquery, Tuple<WriteChannelConfiguration, String> configuration)
throws Exception {
System.out.println("Running insert");
try (FileChannel fileChannel = FileChannel.open(Paths.get(configuration.y()))) {
WriteChannel writeChannel = bigquery.writer(configuration.x());
long position = 0;
long written = fileChannel.transferTo(position, CHUNK_SIZE, writeChannel);
while (written > 0) {
position += written;
written = fileChannel.transferTo(position, CHUNK_SIZE, writeChannel);
}
writeChannel.close();
}
}
@Override
Tuple<WriteChannelConfiguration, String> parse(String... args) throws Exception {
if (args.length == 4) {
String dataset = args[0];
String table = args[1];
String format = args[2];
TableId tableId = TableId.of(dataset, table);
WriteChannelConfiguration configuration =
WriteChannelConfiguration.of(tableId, FormatOptions.of(format));
return Tuple.of(configuration, args[3]);
}
throw new IllegalArgumentException("Missing required arguments.");
}
@Override
protected String params() {
return "<dataset> <table> <format> <filePath>";
}
}
static {
CREATE_ACTIONS.put("dataset", new CreateDatasetAction());
CREATE_ACTIONS.put("table", new CreateSimpleTableAction());
CREATE_ACTIONS.put("view", new CreateViewAction());
CREATE_ACTIONS.put("external-table", new CreateExternalTableAction());
INFO_ACTIONS.put("dataset", new DatasetInfoAction());
INFO_ACTIONS.put("table", new TableInfoAction());
INFO_ACTIONS.put("job", new JobInfoAction());
LIST_ACTIONS.put("datasets", new ListDatasetsAction());
LIST_ACTIONS.put("tables", new ListTablesAction());
LIST_ACTIONS.put("jobs", new ListJobsAction());
LIST_ACTIONS.put("data", new ListTableDataAction());
DELETE_ACTIONS.put("dataset", new DeleteDatasetAction());
DELETE_ACTIONS.put("table", new DeleteTableAction());
ACTIONS.put("create", new ParentAction(CREATE_ACTIONS));
ACTIONS.put("info", new ParentAction(INFO_ACTIONS));
ACTIONS.put("list", new ParentAction(LIST_ACTIONS));
ACTIONS.put("delete", new ParentAction(DELETE_ACTIONS));
ACTIONS.put("cancel", new CancelJobAction());
ACTIONS.put("load", new LoadAction());
ACTIONS.put("extract", new ExtractAction());
ACTIONS.put("copy", new CopyAction());
ACTIONS.put("query", new QueryAction());
ACTIONS.put("load-file", new LoadFileAction());
}
private static void printUsage() {
StringBuilder actionAndParams = new StringBuilder();
for (Map.Entry<String, BigQueryAction> entry : ACTIONS.entrySet()) {
actionAndParams.append("\n\t").append(entry.getKey());
String param = entry.getValue().params();
if (param != null && !param.isEmpty()) {
actionAndParams.append(' ').append(param.replace("\n", "\n\t\t"));
}
}
System.out.printf("Usage: %s [<project_id>] operation [entity] <args>*%s%n",
BigQueryExample.class.getSimpleName(), actionAndParams);
}
@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
if (args.length < 1) {
System.out.println("Missing required project id and action");
printUsage();
return;
}
BigQueryOptions.Builder optionsBuilder = BigQueryOptions.builder();
BigQueryAction action;
String actionName;
if (args.length >= 2 && !ACTIONS.containsKey(args[0])) {
actionName = args[1];
optionsBuilder.projectId(args[0]);
action = ACTIONS.get(args[1]);
args = Arrays.copyOfRange(args, 2, args.length);
} else {
actionName = args[0];
action = ACTIONS.get(args[0]);
args = Arrays.copyOfRange(args, 1, args.length);
}
if (action == null) {
System.out.println("Unrecognized action.");
printUsage();
return;
}
BigQuery bigquery = optionsBuilder.build().service();
Object arg;
try {
arg = action.parse(args);
} catch (IllegalArgumentException ex) {
System.out.printf("Invalid input for action '%s'. %s%n", actionName, ex.getMessage());
System.out.printf("Expected: %s%n", action.params());
return;
} catch (Exception ex) {
System.out.println("Failed to parse arguments.");
ex.printStackTrace();
return;
}
action.run(bigquery, arg);
}
}