Skip to content

Commit 19461a0

Browse files
committed
---
yaml --- r: 1507 b: refs/heads/master c: 7e052da h: refs/heads/master i: 1505: 76ff76e 1503: 8f5cc7d
1 parent 325a76e commit 19461a0

5 files changed

Lines changed: 102 additions & 44 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 329bd70010b0bf49a2a096e1b35524102f364ff0
2+
refs/heads/master: 7e052dae27b857ed3301e6aed83e41bd01d40e48
33
refs/heads/travis: e21ee7b88a5edc3f3d8c71f90c3fc32abf7e8dd6
44
refs/heads/gh-pages: d1b373c30c176edc08692348167bec3a244bb823
55
refs/heads/bigquery: 762fa5830e6c398c0396177e3e7fd243bd62cfc3

trunk/gcloud-java-examples/src/main/java/com/google/gcloud/examples/BigQueryExample.java

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import java.util.Map;
5050

5151
/**
52-
* An example of using the Google BigQuery.
52+
* An example of using Google BigQuery.
5353
*
5454
* <p>This example demonstrates a simple/typical BigQuery usage.
5555
*
@@ -58,17 +58,25 @@
5858
* <li>login using gcloud SDK - {@code gcloud auth login}.</li>
5959
* <li>compile using maven - {@code mvn compile}</li>
6060
* <li>run using maven -
61-
* {@code mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.BigQueryExample"
62-
* -Dexec.args="[<project_id>] list datasets | list tables <dataset> | list jobs |
63-
* list data <dataset> <table> | info dataset <dataset> | info table <dataset> <table> |
64-
* info job <job> | create dataset <dataset> |
61+
* <pre>{@code mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.BigQueryExample" -Dexec.args="[<project_id>]
62+
* list datasets |
63+
* list tables <dataset> |
64+
* list jobs |
65+
* list data <dataset> <table> |
66+
* info dataset <dataset> |
67+
* info table <dataset> <table> |
68+
* info job <job> |
69+
* create dataset <dataset> |
6570
* create table <dataset> <table> (<fieldName>:<primitiveType>)+ |
6671
* create view <dataset> <table> <query> |
6772
* create external-table <dataset> <table> <format> (<fieldName>:<primitiveType>)+ <sourceUri> |
68-
* delete dataset <dataset> | delete table <dataset> <table> | cancel <job> |
73+
* delete dataset <dataset> |
74+
* delete table <dataset> <table> |
75+
* cancel <job> |
6976
* copy <sourceDataset> <sourceTable> <destinationDataset> <destinationTable> |
7077
* load <dataset> <table> <format> <sourceUri>+ |
71-
* extract <dataset> <table> <format> <destinationUri>+ | query <query>"}
78+
* extract <dataset> <table> <format> <destinationUri>+ |
79+
* query <query>"}</pre>
7280
* </li>
7381
* </ol>
7482
*
@@ -122,16 +130,18 @@ Tuple<BigQueryAction, Object> parse(String... args) throws Exception {
122130
if (action != null) {
123131
Object actionArguments = action.parse(Arrays.copyOfRange(args, 1, args.length));
124132
return Tuple.of(action, actionArguments);
133+
} else {
134+
throw new IllegalArgumentException("Unrecognized entity '" + args[0] + "'.");
125135
}
126136
}
127-
throw new IllegalArgumentException();
137+
throw new IllegalArgumentException("Missing required entity.");
128138
}
129139

130140
@Override
131141
public String params() {
132142
StringBuilder builder = new StringBuilder();
133143
for (Map.Entry<String, BigQueryAction> entry : subActions.entrySet()) {
134-
builder.append("\n").append(entry.getKey());
144+
builder.append('\n').append(entry.getKey());
135145
String param = entry.getValue().params();
136146
if (param != null && !param.isEmpty()) {
137147
builder.append(' ').append(param);
@@ -141,13 +151,13 @@ public String params() {
141151
}
142152
}
143153

144-
private abstract static class VoidAction extends BigQueryAction<Void> {
154+
private abstract static class NoArgsAction extends BigQueryAction<Void> {
145155
@Override
146156
Void parse(String... args) throws Exception {
147157
if (args.length == 0) {
148158
return null;
149159
}
150-
throw new IllegalArgumentException();
160+
throw new IllegalArgumentException("This action takes no arguments.");
151161
}
152162
}
153163

@@ -157,7 +167,7 @@ Void parse(String... args) throws Exception {
157167
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/datasets/list">Datasets: list
158168
* </a>
159169
*/
160-
private static class ListDatasetsAction extends VoidAction {
170+
private static class ListDatasetsAction extends NoArgsAction {
161171
@Override
162172
public void run(BigQuery bigquery, Void arg) {
163173
Iterator<DatasetInfo> datasetInfoIterator = bigquery.listDatasets().iterateAll();
@@ -170,10 +180,15 @@ public void run(BigQuery bigquery, Void arg) {
170180
private abstract static class DatasetAction extends BigQueryAction<DatasetId> {
171181
@Override
172182
DatasetId parse(String... args) throws Exception {
183+
String message;
173184
if (args.length == 1) {
174185
return DatasetId.of(args[0]);
186+
} else if (args.length > 1) {
187+
message = "Too many arguments.";
188+
} else {
189+
message = "Missing required dataset id.";
175190
}
176-
throw new IllegalArgumentException();
191+
throw new IllegalArgumentException(message);
177192
}
178193

179194
@Override
@@ -233,18 +248,26 @@ public void run(BigQuery bigquery, DatasetId datasetId) {
233248
private static class DeleteDatasetAction extends DatasetAction {
234249
@Override
235250
public void run(BigQuery bigquery, DatasetId datasetId) {
236-
bigquery.delete(datasetId);
237-
System.out.println("Dataset " + datasetId + " was deleted");
251+
if (bigquery.delete(datasetId)) {
252+
System.out.println("Dataset " + datasetId + " was deleted");
253+
} else {
254+
System.out.println("Dataset " + datasetId + " not found");
255+
}
238256
}
239257
}
240258

241259
private abstract static class TableAction extends BigQueryAction<TableId> {
242260
@Override
243261
TableId parse(String... args) throws Exception {
262+
String message;
244263
if (args.length == 2) {
245264
return TableId.of(args[0], args[1]);
265+
} else if (args.length < 2) {
266+
message = "Missing required dataset and table id.";
267+
} else {
268+
message = "Too many arguments.";
246269
}
247-
throw new IllegalArgumentException();
270+
throw new IllegalArgumentException(message);
248271
}
249272

250273
@Override
@@ -274,8 +297,11 @@ public void run(BigQuery bigquery, TableId tableId) {
274297
private static class DeleteTableAction extends TableAction {
275298
@Override
276299
public void run(BigQuery bigquery, TableId tableId) {
277-
bigquery.delete(tableId);
278-
System.out.println("Table " + tableId + " was deleted");
300+
if (bigquery.delete(tableId)) {
301+
System.out.println("Table " + tableId + " was deleted");
302+
} else {
303+
System.out.println("Table " + tableId + " not found");
304+
}
279305
}
280306
}
281307

@@ -298,10 +324,15 @@ public void run(BigQuery bigquery, TableId tableId) {
298324
private abstract static class JobAction extends BigQueryAction<JobId> {
299325
@Override
300326
JobId parse(String... args) throws Exception {
327+
String message;
301328
if (args.length == 1) {
302329
return JobId.of(args[0]);
330+
} else if (args.length > 1) {
331+
message = "Too many arguments.";
332+
} else {
333+
message = "Missing required query.";
303334
}
304-
throw new IllegalArgumentException();
335+
throw new IllegalArgumentException(message);
305336
}
306337

307338
@Override
@@ -315,7 +346,7 @@ public String params() {
315346
*
316347
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/list">Jobs: list</a>
317348
*/
318-
private static class ListJobsAction extends VoidAction {
349+
private static class ListJobsAction extends NoArgsAction {
319350
@Override
320351
public void run(BigQuery bigquery, Void arg) {
321352
Iterator<JobInfo> datasetInfoIterator = bigquery.listJobs().iterateAll();
@@ -345,16 +376,19 @@ public void run(BigQuery bigquery, JobId jobId) {
345376
private static class CancelJobAction extends JobAction {
346377
@Override
347378
public void run(BigQuery bigquery, JobId jobId) {
348-
bigquery.cancel(jobId);
349-
System.out.println("Requested cancel for job " + jobId);
379+
if (bigquery.cancel(jobId)) {
380+
System.out.println("Requested cancel for job " + jobId);
381+
} else {
382+
System.out.println("Job " + jobId + " not found");
383+
}
350384
}
351385
}
352386

353387
private abstract static class CreateTableAction extends BigQueryAction<BaseTableInfo> {
354388
@Override
355389
void run(BigQuery bigquery, BaseTableInfo table) throws Exception {
356390
BaseTableInfo createTable = bigquery.create(table);
357-
System.out.println("Created table " + createTable.tableId());
391+
System.out.println("Created table:");
358392
System.out.println(createTable.toString());
359393
}
360394

@@ -363,10 +397,10 @@ static Schema parseSchema(String[] args, int start, int end) {
363397
for (int i = start; i < end; i++) {
364398
String[] fieldsArray = args[i].split(":");
365399
if (fieldsArray.length != 2) {
366-
throw new IllegalArgumentException();
400+
throw new IllegalArgumentException("Unrecognized field definition '" + args[i] + "'.");
367401
}
368402
String fieldName = fieldsArray[0];
369-
String typeString = fieldsArray[1];
403+
String typeString = fieldsArray[1].toLowerCase();
370404
Field.Type fieldType;
371405
switch (typeString) {
372406
case "string":
@@ -385,7 +419,7 @@ static Schema parseSchema(String[] args, int start, int end) {
385419
fieldType = Field.Type.bool();
386420
break;
387421
default:
388-
throw new IllegalArgumentException();
422+
throw new IllegalArgumentException("Unrecognized field type '" + typeString + "'.");
389423
}
390424
builder.addField(Field.of(fieldName, fieldType));
391425
}
@@ -409,7 +443,7 @@ BaseTableInfo parse(String... args) throws Exception {
409443
TableId tableId = TableId.of(dataset, table);
410444
return TableInfo.of(tableId, parseSchema(args, 2, args.length));
411445
}
412-
throw new IllegalArgumentException();
446+
throw new IllegalArgumentException("Missing required arguments.");
413447
}
414448

415449
@Override
@@ -437,7 +471,7 @@ BaseTableInfo parse(String... args) throws Exception {
437471
parseSchema(args, 3, args.length - 1), FormatOptions.of(args[2]));
438472
return ExternalTableInfo.of(tableId, configuration);
439473
}
440-
throw new IllegalArgumentException();
474+
throw new IllegalArgumentException("Missing required arguments.");
441475
}
442476

443477
@Override
@@ -456,14 +490,19 @@ protected String params() {
456490
private static class CreateViewAction extends CreateTableAction {
457491
@Override
458492
BaseTableInfo parse(String... args) throws Exception {
493+
String message;
459494
if (args.length == 3) {
460495
String dataset = args[0];
461496
String table = args[1];
462497
String query = args[2];
463498
TableId tableId = TableId.of(dataset, table);
464499
return ViewInfo.of(tableId, query);
500+
} else if (args.length < 3) {
501+
message = "Missing required dataset id, table id or query.";
502+
} else {
503+
message = "Too many arguments.";
465504
}
466-
throw new IllegalArgumentException();
505+
throw new IllegalArgumentException(message);
467506
}
468507

469508
@Override
@@ -508,7 +547,7 @@ LoadJobInfo parse(String... args) throws Exception {
508547
.formatOptions(FormatOptions.of(format))
509548
.build();
510549
}
511-
throw new IllegalArgumentException();
550+
throw new IllegalArgumentException("Missing required arguments.");
512551
}
513552

514553
@Override
@@ -534,7 +573,7 @@ ExtractJobInfo parse(String... args) throws Exception {
534573
.format(format)
535574
.build();
536575
}
537-
throw new IllegalArgumentException();
576+
throw new IllegalArgumentException("Missing required arguments.");
538577
}
539578

540579
@Override
@@ -551,12 +590,17 @@ protected String params() {
551590
private static class CopyAction extends JobRunAction {
552591
@Override
553592
CopyJobInfo parse(String... args) throws Exception {
593+
String message;
554594
if (args.length == 4) {
555595
TableId sourceTableId = TableId.of(args[0], args[1]);
556596
TableId destinationTableId = TableId.of(args[2], args[3]);
557597
return CopyJobInfo.of(destinationTableId, sourceTableId);
598+
} else if (args.length < 3) {
599+
message = "Missing required source or destination table.";
600+
} else {
601+
message = "Too many arguments.";
558602
}
559-
throw new IllegalArgumentException();
603+
throw new IllegalArgumentException(message);
560604
}
561605

562606
@Override
@@ -597,10 +641,15 @@ void run(BigQuery bigquery, QueryRequest queryRequest) throws Exception {
597641

598642
@Override
599643
QueryRequest parse(String... args) throws Exception {
644+
String message;
600645
if (args.length == 1) {
601646
return QueryRequest.of(args[0]);
647+
} else if (args.length > 1) {
648+
message = "Too many arguments.";
649+
} else {
650+
message = "Missing required query.";
602651
}
603-
throw new IllegalArgumentException();
652+
throw new IllegalArgumentException(message);
604653
}
605654

606655
@Override
@@ -650,6 +699,11 @@ private static void printUsage() {
650699

651700
@SuppressWarnings("unchecked")
652701
public static void main(String... args) throws Exception {
702+
if (args.length < 1) {
703+
System.out.println("Missing required project id and action");
704+
printUsage();
705+
return;
706+
}
653707
BigQueryOptions.Builder optionsBuilder = BigQueryOptions.builder();
654708
BigQueryAction action;
655709
String actionName;
@@ -673,7 +727,7 @@ public static void main(String... args) throws Exception {
673727
try {
674728
request = action.parse(args);
675729
} catch (IllegalArgumentException ex) {
676-
System.out.println("Invalid input for action '" + actionName + "'");
730+
System.out.println("Invalid input for action '" + actionName + "'. " + ex.getMessage());
677731
System.out.println("Expected: " + action.params());
678732
return;
679733
} catch (Exception ex) {

trunk/gcloud-java-examples/src/main/java/com/google/gcloud/examples/DatastoreExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import java.util.TreeMap;
3737

3838
/**
39-
* An example of using the Google Cloud Datastore.
39+
* An example of using Google Cloud Datastore.
4040
*
4141
* <p>This example adds, display or clear comments for a given user.
4242
*

trunk/gcloud-java-examples/src/main/java/com/google/gcloud/examples/ResourceManagerExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.util.Scanner;
2828

2929
/**
30-
* An example of using the Google Cloud Resource Manager.
30+
* An example of using Google Cloud Resource Manager.
3131
*
3232
* <p>This example creates, deletes, gets, and lists projects.
3333
*

trunk/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
import java.util.concurrent.TimeUnit;
5959

6060
/**
61-
* An example of using the Google Cloud Storage.
61+
* An example of using Google Cloud Storage.
6262
*
6363
* <p>This example demonstrates a simple/typical storage usage.
6464
*
@@ -67,12 +67,16 @@
6767
* <li>login using gcloud SDK - {@code gcloud auth login}.</li>
6868
* <li>compile using maven - {@code mvn compile}</li>
6969
* <li>run using maven -
70-
* {@code mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample"
71-
* -Dexec.args="[<project_id>] list [<bucket>] | info [<bucket> [<file>]] |
72-
* download <bucket> <path> [local_file] | upload <local_file> <bucket> [<path>] |
73-
* delete <bucket> <path>+ | cp <from_bucket> <from_path> <to_bucket> <to_path> |
74-
* compose <bucket> <from_path>+ <to_path> | update_metadata <bucket> <file> [key=value]* |
75-
* sign_url <service_account_private_key_file> <service_account_email> <bucket> <path>"}
70+
* <pre>{@code mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample" -Dexec.args="[<project_id>]
71+
* list [<bucket>] |
72+
* info [<bucket> [<file>]] |
73+
* download <bucket> <path> [local_file] |
74+
* upload <local_file> <bucket> [<path>] |
75+
* delete <bucket> <path>+ |
76+
* cp <from_bucket> <from_path> <to_bucket> <to_path> |
77+
* compose <bucket> <from_path>+ <to_path> |
78+
* update_metadata <bucket> <file> [key=value]* |
79+
* sign_url <service_account_private_key_file> <service_account_email> <bucket> <path>"}</pre>
7680
* </li>
7781
* </ol>
7882
*

0 commit comments

Comments
 (0)