4949import 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 *
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 ) {
0 commit comments