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 ;
3131
3232
3333/**
34- * TODO: Insert description here. (generated by elibixby)
34+ * Example of Bigquery Streaming.
3535 */
36- public class StreamingSample extends BigqueryUtils {
36+ public class StreamingSample {
37+
38+ /**
39+ * Empty constructor since this is just a collection of static methods.
40+ */
41+ protected StreamingSample () {
42+
43+ }
44+
3745
38-
39-
46+ /**
47+ * Command line that demonstrates Bigquery streaming.
48+ * @param args Command line args, should be empty
49+ * @throws IOException IOexception
50+ */
4051 // [START main]
41- public static void main (String [] args ) throws IOException {
52+ public static void main (final String [] args ) throws IOException {
4253 final Scanner scanner = new Scanner (System .in );
4354 System .out .println ("Enter your project id: " );
4455 String projectId = scanner .nextLine ();
@@ -47,54 +58,70 @@ public static void main(String[] args) throws IOException{
4758 System .out .println ("Enter your table id: " );
4859 String tableId = scanner .nextLine ();
4960 scanner .close ();
50-
61+
5162 System .out .println ("Enter JSON to stream to BigQuery: \n "
5263 + "Press End-of-stream (CTRL-D) to stop" );
53-
64+
5465 JsonReader fromCLI = new JsonReader (new InputStreamReader (System .in ));
55-
66+
5667 Iterator <TableDataInsertAllResponse > responses = run (projectId ,
5768 datasetId ,
5869 tableId ,
5970 fromCLI );
60-
61- while (responses .hasNext ()){
71+
72+ while (responses .hasNext ()) {
6273 System .out .println (responses .next ());
6374 }
64-
75+
6576 fromCLI .close ();
6677 }
6778 // [END main]
68-
69-
70-
71- // [START run]
79+
80+
81+ /**
82+ * Run the bigquery ClI.
83+ * @param projectId Project id
84+ * @param datasetId datasetid
85+ * @param tableId tableid
86+ * @param rows The source of the JSON rows we are streaming in.
87+ * @return Returns Iterates through the stream responses
88+ * @throws IOException Thrown if there is an error connecting to Bigquery.
89+ * @throws InterruptedException Should never be thrown
90+ */
91+ // [START run]
7292 public static Iterator <TableDataInsertAllResponse > run (final String projectId ,
73- final String datasetId ,
93+ final String datasetId ,
7494 final String tableId ,
75- final JsonReader rows ) throws IOException {
76-
77-
95+ final JsonReader rows ) throws IOException {
96+
97+
7898 final Bigquery bigquery = BigqueryServiceFactory .getService ();
7999 final Gson gson = new Gson ();
80100 rows .beginArray ();
81-
82- return new Iterator <TableDataInsertAllResponse >(){
83101
102+ return new Iterator <TableDataInsertAllResponse >() {
103+
104+ /**
105+ * Get the next row in the stream
106+ * @return True if there is another row in the stream
107+ */
84108 public boolean hasNext () {
85109 try {
86110 return rows .hasNext ();
87111 } catch (IOException e ) {
88- // TODO(elibixby): Auto-generated catch block
89112 e .printStackTrace ();
90113 }
91114 return false ;
92115 }
93116
117+ /**
118+ *
119+ * @return Next page of data
120+ */
94121 public TableDataInsertAllResponse next () {
95122 try {
96123 Map <String , Object > rowData = gson .<Map <String , Object >>fromJson (
97- rows ,
124+ rows ,
98125 (new HashMap <String , Object >()).getClass ());
99126 return streamRow (bigquery ,
100127 projectId ,
@@ -112,25 +139,35 @@ public TableDataInsertAllResponse next() {
112139 public void remove () {
113140 this .next ();
114141 }
115-
142+
116143 };
117-
144+
118145 }
119146// [END run]
120-
147+
148+ /**
149+ *
150+ * @param bigquery The bigquery service
151+ * @param projectId project id from Google Developers console
152+ * @param datasetId id of teh dataset
153+ * @param tableId if the table we're streaming
154+ * @param row Id of the row we're inserting
155+ * @return Response from the insert
156+ * @throws IOException ioexception
157+ */
121158// [START streamRow]
122- public static TableDataInsertAllResponse streamRow (Bigquery bigquery ,
123- String projectId ,
124- String datasetId ,
125- String tableId ,
126- TableDataInsertAllRequest .Rows row ) throws IOException {
127-
159+ public static TableDataInsertAllResponse streamRow (final Bigquery bigquery ,
160+ final String projectId ,
161+ final String datasetId ,
162+ final String tableId ,
163+ final TableDataInsertAllRequest .Rows row ) throws IOException {
164+
128165 return bigquery .tabledata ().insertAll (
129- projectId ,
130- datasetId ,
131- tableId ,
132- new TableDataInsertAllRequest ().setRows (Collections . singletonList ( row ))). execute ();
133-
166+ projectId ,
167+ datasetId ,
168+ tableId ,
169+ new TableDataInsertAllRequest ().setRows (
170+ Collections . singletonList ( row ))). execute ();
134171 }
135172// [END streamRow]
136173}
0 commit comments