11/*
2- Copyright 2015, Google, Inc.
3- Licensed under the Apache License, Version 2.0 (the "License");
4- you may not use this file except in compliance with the License.
5- You may obtain a copy of the License at
6-
7- http://www.apache.org/licenses/LICENSE-2.0
8-
9- Unless required by applicable law or agreed to in writing, software
10- distributed under the License is distributed on an "AS IS" BASIS,
11- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12- See the License for the specific language governing permissions and
2+ Copyright 2015, Google, Inc.
3+ Licensed under the Apache License, Version 2.0 (the "License");
4+ you may not use this file except in compliance with the License.
5+ You may obtain a copy of the License at
6+
7+ http://www.apache.org/licenses/LICENSE-2.0
8+
9+ Unless required by applicable law or agreed to in writing, software
10+ distributed under the License is distributed on an "AS IS" BASIS,
11+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ See the License for the specific language governing permissions and
1313 limitations under the License.
1414*/
1515package com .google .cloud .bigquery .samples ;
3535import java .util .NoSuchElementException ;
3636
3737/**
38- * TODO: Insert description here. (generated by elibixby)
38+ * Helper functions for the other classes.
3939 */
40- public class BigqueryUtils {
41-
40+ public final class BigqueryUtils {
41+
42+ /**
43+ * Private contructor to prevent creation of this class, which is just all
44+ * static helper methods.
45+ */
46+ private BigqueryUtils () {
47+
48+ }
49+
50+ /**
51+ * Print rows to the output stream in a formatted way.
52+ * @param rows rows in bigquery
53+ * @param out Output stream we want to print to
54+ */
4255 // [START print_rows]
43- public static void printRows (List <TableRow > rows , PrintStream out ){
56+ public static void printRows (final List <TableRow > rows , final PrintStream
57+ out ) {
4458 for (TableRow row : rows ) {
4559 for (TableCell field : row .getF ()) {
4660 out .printf ("%-50s" , field .getV ());
@@ -49,40 +63,73 @@ public static void printRows(List<TableRow> rows, PrintStream out){
4963 }
5064 }
5165 // [END print_rows]
52-
66+
67+ /**
68+ * Polls the job for completion.
69+ * @param request The bigquery request to poll for completion
70+ * @param interval Number of milliseconds between each poll
71+ * @return The finished job
72+ * @throws IOException IOException
73+ * @throws InterruptedException InterruptedException
74+ */
5375 // [START poll_job]
54- public static Job pollJob (Bigquery .Jobs .Get request , long interval )
55- throws IOException , InterruptedException {
76+ public static Job pollJob (final Bigquery .Jobs .Get request , final long
77+ interval )
78+ throws IOException , InterruptedException {
5679 Job job = request .execute ();
57- while (!job .getStatus ().getState ().equals ("DONE" )) {
58- System .out .println ("Job is "
59- + job .getStatus ().getState ()
80+ while (!job .getStatus ().getState ().equals ("DONE" )) {
81+ System .out .println ("Job is "
82+ + job .getStatus ().getState ()
6083 + " waiting " + interval + " milliseconds..." );
6184 Thread .sleep (interval );
6285 job = request .execute ();
6386 }
6487 return job ;
6588 }
6689 // [END poll_job]
67-
90+
91+ /**
92+ * Pages through the results of an arbitrary Bigquery request.
93+ * @param requestTemplate The object that represents the call to fetch
94+ * the results.
95+ * @param <T> The type of the returned objects
96+ * @return An iterator that pages through the returned object
97+ */
6898 // [START paging]
69- public static <T extends GenericJson > Iterator <T > getPages (BigqueryRequest <T > request_template ){
99+ public static <T extends GenericJson > Iterator <T > getPages (
100+ final BigqueryRequest <T > requestTemplate ) {
70101
71- class PageIterator implements Iterator <T >{
102+ /**
103+ * An iterator class that pages through a Bigquery request.
104+ */
105+ class PageIterator implements Iterator <T > {
72106
73- BigqueryRequest <T > request ;
74- boolean hasNext = true ;
107+ private BigqueryRequest <T > request ;
108+ private boolean hasNext = true ;
75109
76- public PageIterator (BigqueryRequest <T > request_template ){
77- this .request = request_template ;
110+ /**
111+ * Inner class that represents our iterator to page through results.
112+ * @param requestTemplate The object that represents the call to fetch
113+ * the results.
114+ */
115+ public PageIterator (final BigqueryRequest <T > requestTemplate ) {
116+ this .request = requestTemplate ;
78117 }
79118
119+ /**
120+ * Checks whether there is another page of results.
121+ * @return True if there is another page of results.
122+ */
80123 public boolean hasNext () {
81- return hasNext ;
124+ return hasNext ;
82125 }
83-
126+
127+ /**
128+ * Returns the next page of results.
129+ * @return The next page of resul.ts
130+ */
84131 public T next () {
85- if (!hasNext ){
132+ if (!hasNext ) {
86133 throw new NoSuchElementException ();
87134 }
88135 try {
@@ -98,38 +145,49 @@ public T next() {
98145 return null ;
99146 }
100147 }
148+
149+ /**
150+ * Skips the page by moving the iterator to the next page.
151+ */
101152 public void remove () {
102153 this .next ();
103- }
154+ }
104155 }
105156
106- return new PageIterator (request_template );
157+ return new PageIterator (requestTemplate );
107158 }
108159 // [END paging]
109-
160+
161+ /**
162+ * Loads a Bigquery schema.
163+ * @param schemaSource The source of the schema
164+ * @return The TableSchema
165+ */
110166 // [START load_schema]
111- public static TableSchema loadSchema (Reader schemaSource ){
167+ public static TableSchema loadSchema (final Reader schemaSource ) {
112168 TableSchema sourceSchema = new TableSchema ();
113-
114- List <TableFieldSchema > fields = (new Gson ()).< List < TableFieldSchema >> fromJson (
115- schemaSource ,
169+
170+ List <TableFieldSchema > fields = (new Gson ())
171+ .< List < TableFieldSchema >> fromJson ( schemaSource ,
116172 (new ArrayList <TableFieldSchema >()).getClass ());
117-
173+
118174 sourceSchema .setFields (fields );
119-
175+
120176 return sourceSchema ;
121177 }
122178 // [END load_schema]
123-
179+
124180 // [START list_datasets]
125181 /**
126- * Display all BigQuery datasets associated with a project
182+ * Display all BigQuery datasets associated with a project.
127183 *
128184 * @param bigquery an authorized BigQuery client
129185 * @param projectId a string containing the current project ID
130- * @throws IOException
186+ * @throws IOException Thrown if there is a network error connecting to
187+ * Bigquery.
131188 */
132- public static void listDatasets (Bigquery bigquery , String projectId )
189+ public static void listDatasets (final Bigquery bigquery , final String
190+ projectId )
133191 throws IOException {
134192 Datasets .List datasetRequest = bigquery .datasets ().list (projectId );
135193 DatasetList datasetList = datasetRequest .execute ();
@@ -143,5 +201,5 @@ public static void listDatasets(Bigquery bigquery, String projectId)
143201 }
144202 }
145203 // [END list_datasets]
146-
204+
147205}
0 commit comments