Skip to content

Commit 5019fb6

Browse files
author
Jerjou Cheng
committed
Merge branch 'jerjou/xml-api' of github.com:GoogleCloudPlatform/java-docs-samples into jerjou/xml-api
2 parents b34b01c + 69e24dc commit 5019fb6

File tree

3 files changed

+138
-85
lines changed

3 files changed

+138
-85
lines changed
Lines changed: 85 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
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
*/
1515
package com.google.cloud.bigquery.samples;
@@ -31,14 +31,25 @@
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
}
Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
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
*/
1515
package com.google.cloud.bigquery.samples;
@@ -24,58 +24,74 @@
2424
import java.util.Iterator;
2525
import java.util.Scanner;
2626
/**
27-
* TODO: Insert description here. (generated by elibixby)
27+
* Runs a synchronous query against Bigtable.
2828
*/
29-
public class SyncQuerySample extends BigqueryUtils{
29+
public class SyncQuerySample {
30+
3031

32+
/**
33+
* Protected because this is a collection of static methods.
34+
*/
35+
protected SyncQuerySample() {
36+
37+
}
3138

3239
//[START main]
3340
/**
34-
* @param args
35-
* @throws IOException
41+
* @param args args
42+
* @throws IOException ioexceptino
3643
*/
37-
public static void main(String[] args)
38-
throws IOException{
39-
40-
44+
public static void main(final String[] args)
45+
throws IOException {
4146
Scanner scanner = new Scanner(System.in);
4247
System.out.println("Enter your project id: ");
4348
String projectId = scanner.nextLine();
4449
System.out.println("Enter your query string: ");
4550
String queryString = scanner.nextLine();
46-
System.out.println("Enter how long to wait for the query to complete (in milliseconds):\n " +
47-
"(if longer than 10 seconds, use an asynchronous query)");
51+
System.out.println("Enter how long to wait for the query to complete"
52+
+ " (in milliseconds):\n "
53+
+ "(if longer than 10 seconds, use an asynchronous query)");
4854
long waitTime = scanner.nextLong();
4955
scanner.close();
50-
Iterator<GetQueryResultsResponse> pages = run(projectId, queryString, waitTime);
51-
while(pages.hasNext()){
52-
printRows(pages.next().getRows(), System.out);
56+
Iterator<GetQueryResultsResponse> pages = run(projectId, queryString,
57+
waitTime);
58+
while (pages.hasNext()) {
59+
BigqueryUtils.printRows(pages.next().getRows(), System.out);
5360
}
5461
}
5562
// [END main]
56-
5763

64+
65+
/**
66+
*
67+
* @param projectId project id from developer console
68+
* @param queryString query to run
69+
* @param waitTime Timeout in milliseconds before we abort
70+
* @return Iterator that pages through the results of the query
71+
* @throws IOException ioexception
72+
*/
5873
// [START run]
59-
public static Iterator<GetQueryResultsResponse> run(String projectId,
60-
String queryString,
61-
long waitTime) throws IOException{
74+
public static Iterator<GetQueryResultsResponse> run(final String projectId,
75+
final String queryString,
76+
final long waitTime) throws IOException {
6277
Bigquery bigquery = BigqueryServiceFactory.getService();
6378
//Wait until query is done with 10 second timeout, at most 5 retries on error
6479
QueryResponse query = bigquery.jobs().query(
6580
projectId,
66-
new QueryRequest().setTimeoutMs(waitTime).setQuery(queryString)).execute();
67-
68-
//Make a request to get the results of the query
81+
new QueryRequest().setTimeoutMs(waitTime).setQuery(queryString))
82+
.execute();
83+
84+
//Make a request to get the results of the query
6985
//(timeout is zero since job should be complete)
70-
86+
7187
GetQueryResults getRequest = bigquery.jobs().getQueryResults(
7288
query.getJobReference().getProjectId(),
7389
query.getJobReference().getJobId());
74-
75-
76-
return getPages(getRequest);
90+
91+
92+
return BigqueryUtils.getPages(getRequest);
7793
}
7894
// [END run]
79-
95+
8096

8197
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<configuration>
3636
<configLocation>checkstyle-checker.xml</configLocation>
3737
<consoleOutput>true</consoleOutput>
38-
<failOnViolation>false</failOnViolation>
38+
<failOnViolation>true</failOnViolation>
3939
</configuration>
4040
<executions>
4141
<execution>

0 commit comments

Comments
 (0)