Skip to content

Commit d918b3c

Browse files
committed
Merge pull request GoogleCloudPlatform#14 from GoogleCloudPlatform/jerjou/xml-api
Move examples from the cloud-storage-docs-xml-api-examples repo, and update them to use Application Default Credentials.
2 parents 7dd669a + a33bbb0 commit d918b3c

File tree

24 files changed

+1383
-284
lines changed

24 files changed

+1383
-284
lines changed

bigquery/src/main/java/com/google/cloud/bigquery/samples/AsyncQuerySample.java

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
/*
1+
/**
22
* Copyright (c) 2015 Google Inc.
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5-
* in compliance with the License. You may obtain a copy of the License at
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
67
*
78
* http://www.apache.org/licenses/LICENSE-2.0
89
*
9-
* Unless required by applicable law or agreed to in writing, software distributed under the License
10-
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11-
* or implied. See the License for the specific language governing permissions and limitations under
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
1214
* the License.
1315
*/
1416

@@ -30,16 +32,16 @@
3032
/**
3133
* Example of authorizing with BigQuery and reading from a public dataset.
3234
*/
33-
public class AsyncQuerySample extends BigqueryUtils{
35+
public class AsyncQuerySample extends BigqueryUtils {
36+
3437

35-
3638
// [START main]
3739
/**
38-
* @param args
39-
* @throws IOException
40-
* @throws InterruptedException
40+
* @param args Command line args
41+
* @throws IOException IOException
42+
* @throws InterruptedException InterruptedException
4143
*/
42-
public static void main(String[] args)
44+
public static void main(final String[] args)
4345
throws IOException, InterruptedException {
4446

4547
Scanner scanner = new Scanner(System.in);
@@ -49,66 +51,79 @@ public static void main(String[] args)
4951
String queryString = scanner.nextLine();
5052
System.out.println("Run query in batch mode? [true|false] ");
5153
boolean batch = Boolean.valueOf(scanner.nextLine());
52-
System.out.println("Enter how often to check if your job is complete (milliseconds): ");
54+
System.out.println("Enter how often to check if your job is complete "
55+
+ "(milliseconds): ");
5356
long waitTime = scanner.nextLong();
5457
scanner.close();
55-
Iterator<GetQueryResultsResponse> pages = run(projectId, queryString, batch, waitTime);
56-
while(pages.hasNext()){
58+
Iterator<GetQueryResultsResponse> pages = run(projectId, queryString,
59+
batch, waitTime);
60+
while (pages.hasNext()) {
5761
printRows(pages.next().getRows(), System.out);
5862
}
5963

6064
}
6165
// [END main]
62-
6366
// [START run]
64-
public static Iterator<GetQueryResultsResponse> run(String projectId,
65-
String queryString,
66-
boolean batch,
67-
long waitTime)
68-
throws IOException, InterruptedException{
69-
67+
68+
/**
69+
*
70+
* @param projectId Get this from Google Developers console
71+
* @param queryString Query we want to run against BigQuery
72+
* @param batch True if you want to batch the queries
73+
* @param waitTime How long to wait before retries
74+
* @return An interator to the result of your pages
75+
* @throws IOException Thrown if there's an IOException
76+
* @throws InterruptedException Thrown if there's an Interrupted Exception
77+
*/
78+
public static Iterator<GetQueryResultsResponse> run(final String projectId,
79+
final String queryString,
80+
final boolean batch,
81+
final long waitTime)
82+
throws IOException, InterruptedException {
83+
7084
Bigquery bigquery = BigqueryServiceFactory.getService();
7185

7286
Job query = asyncQuery(bigquery, projectId, queryString, batch);
7387
Bigquery.Jobs.Get getRequest = bigquery.jobs().get(
7488
projectId, query.getJobReference().getJobId());
75-
76-
//Poll every waitTime milliseconds,
89+
90+
//Poll every waitTime milliseconds,
7791
//retrying at most retries times if there are errors
7892
pollJob(getRequest, waitTime);
7993

8094
GetQueryResults resultsRequest = bigquery.jobs().getQueryResults(
8195
projectId, query.getJobReference().getJobId());
82-
96+
8397
return getPages(resultsRequest);
8498
}
8599
// [END run]
86-
100+
87101
// [START asyncQuery]
88102
/**
89-
* Inserts an asynchronous query Job for a particular query
103+
* Inserts an asynchronous query Job for a particular query.
90104
*
91105
* @param bigquery an authorized BigQuery client
92106
* @param projectId a String containing the project ID
93107
* @param querySql the actual query string
108+
* @param batch True if you want to run the query as BATCH
94109
* @return a reference to the inserted query job
95-
* @throws IOException
110+
* @throws IOException Thrown if there's a network exception
96111
*/
97-
public static Job asyncQuery(Bigquery bigquery,
98-
String projectId,
99-
String querySql,
100-
boolean batch) throws IOException {
101-
102-
JobConfigurationQuery query_config = new JobConfigurationQuery()
112+
public static Job asyncQuery(final Bigquery bigquery,
113+
final String projectId,
114+
final String querySql,
115+
final boolean batch) throws IOException {
116+
117+
JobConfigurationQuery queryConfig = new JobConfigurationQuery()
103118
.setQuery(querySql);
104-
105-
if(batch){
106-
query_config.setPriority("BATCH");
119+
120+
if (batch) {
121+
queryConfig.setPriority("BATCH");
107122
}
108-
123+
109124
Job job = new Job().setConfiguration(
110-
new JobConfiguration().setQuery(query_config));
111-
125+
new JobConfiguration().setQuery(queryConfig));
126+
112127
return bigquery.jobs().insert(projectId, job).execute();
113128
}
114129
// [END asyncQuery]

bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/*
22
* Copyright (c) 2015 Google Inc.
33
*
4-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5-
* in compliance with the License. You may obtain a copy of the License at
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain a
6+
* copy of the License at
67
*
78
* http://www.apache.org/licenses/LICENSE-2.0
89
*
9-
* Unless required by applicable law or agreed to in writing, software distributed under the License
10-
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11-
* or implied. See the License for the specific language governing permissions and limitations under
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
1214
* the License.
1315
*/
1416

@@ -25,32 +27,61 @@
2527
import java.io.IOException;
2628
import java.util.Collection;
2729

28-
public class BigqueryServiceFactory {
30+
/**
31+
* This class creates our Service to connect to Bigquery including auth.
32+
*/
33+
public final class BigqueryServiceFactory {
34+
35+
/**
36+
* Private constructor to disable creation of this utility Factory class.
37+
*/
38+
private BigqueryServiceFactory() {
2939

40+
}
41+
42+
/**
43+
* Singleton service used through the app.
44+
*/
3045
private static Bigquery service = null;
31-
private static Object service_lock = new Object();
3246

33-
public static Bigquery getService() throws IOException{
34-
if(service==null){
35-
synchronized(service_lock){
36-
if(service==null){
37-
service=createAuthorizedClient();
47+
/**
48+
* Mutex created to create the singleton in thread-safe fashion.
49+
*/
50+
private static Object serviceLock = new Object();
51+
52+
/**
53+
* Threadsafe Factory that provides an authorized Bigquery service.
54+
* @return The Bigquery service
55+
* @throws IOException Thronw if there is an error connecting to Bigquery.
56+
*/
57+
public static Bigquery getService() throws IOException {
58+
if (service == null) {
59+
synchronized (serviceLock) {
60+
if (service == null) {
61+
service = createAuthorizedClient();
3862
}
3963
}
4064
}
4165
return service;
4266
}
4367

68+
/**
69+
* Creates an authorized client to Google Bigquery.
70+
* @return The BigQuery Service
71+
* @throws IOException Thrown if there is an error connecting
72+
*/
4473
// [START get_service]
4574
private static Bigquery createAuthorizedClient() throws IOException {
46-
Collection<String> BIGQUERY_SCOPES = BigqueryScopes.all();
47-
HttpTransport TRANSPORT = new NetHttpTransport();
48-
JsonFactory JSON_FACTORY = new JacksonFactory();
49-
GoogleCredential credential = GoogleCredential.getApplicationDefault(TRANSPORT, JSON_FACTORY);
50-
if(credential.createScopedRequired()){
51-
credential = credential.createScoped(BIGQUERY_SCOPES);
75+
Collection<String> bigqueryScopes = BigqueryScopes.all();
76+
HttpTransport transport = new NetHttpTransport();
77+
JsonFactory jsonFactory = new JacksonFactory();
78+
GoogleCredential credential = GoogleCredential.getApplicationDefault(
79+
transport, jsonFactory);
80+
if (credential.createScopedRequired()) {
81+
credential = credential.createScoped(bigqueryScopes);
5282
}
53-
return new Bigquery.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("BigQuery Samples").build();
83+
return new Bigquery.Builder(transport, jsonFactory, credential)
84+
.setApplicationName("BigQuery Samples").build();
5485
}
5586
// [END get_service]
5687

0 commit comments

Comments
 (0)