Skip to content

Commit

Permalink
Merge pull request #14 from GoogleCloudPlatform/jerjou/xml-api
Browse files Browse the repository at this point in the history
Move examples from the cloud-storage-docs-xml-api-examples repo, and update them to use Application Default Credentials.
  • Loading branch information
jerjou committed May 29, 2015
2 parents 7dd669a + a33bbb0 commit d918b3c
Show file tree
Hide file tree
Showing 24 changed files with 1,383 additions and 284 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/*
/**
* Copyright (c) 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

Expand All @@ -30,16 +32,16 @@
/**
* Example of authorizing with BigQuery and reading from a public dataset.
*/
public class AsyncQuerySample extends BigqueryUtils{
public class AsyncQuerySample extends BigqueryUtils {



// [START main]
/**
* @param args
* @throws IOException
* @throws InterruptedException
* @param args Command line args
* @throws IOException IOException
* @throws InterruptedException InterruptedException
*/
public static void main(String[] args)
public static void main(final String[] args)
throws IOException, InterruptedException {

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

}
// [END main]

// [START run]
public static Iterator<GetQueryResultsResponse> run(String projectId,
String queryString,
boolean batch,
long waitTime)
throws IOException, InterruptedException{


/**
*
* @param projectId Get this from Google Developers console
* @param queryString Query we want to run against BigQuery
* @param batch True if you want to batch the queries
* @param waitTime How long to wait before retries
* @return An interator to the result of your pages
* @throws IOException Thrown if there's an IOException
* @throws InterruptedException Thrown if there's an Interrupted Exception
*/
public static Iterator<GetQueryResultsResponse> run(final String projectId,
final String queryString,
final boolean batch,
final long waitTime)
throws IOException, InterruptedException {

Bigquery bigquery = BigqueryServiceFactory.getService();

Job query = asyncQuery(bigquery, projectId, queryString, batch);
Bigquery.Jobs.Get getRequest = bigquery.jobs().get(
projectId, query.getJobReference().getJobId());
//Poll every waitTime milliseconds,

//Poll every waitTime milliseconds,
//retrying at most retries times if there are errors
pollJob(getRequest, waitTime);

GetQueryResults resultsRequest = bigquery.jobs().getQueryResults(
projectId, query.getJobReference().getJobId());

return getPages(resultsRequest);
}
// [END run]

// [START asyncQuery]
/**
* Inserts an asynchronous query Job for a particular query
* Inserts an asynchronous query Job for a particular query.
*
* @param bigquery an authorized BigQuery client
* @param projectId a String containing the project ID
* @param querySql the actual query string
* @param batch True if you want to run the query as BATCH
* @return a reference to the inserted query job
* @throws IOException
* @throws IOException Thrown if there's a network exception
*/
public static Job asyncQuery(Bigquery bigquery,
String projectId,
String querySql,
boolean batch) throws IOException {
JobConfigurationQuery query_config = new JobConfigurationQuery()
public static Job asyncQuery(final Bigquery bigquery,
final String projectId,
final String querySql,
final boolean batch) throws IOException {

JobConfigurationQuery queryConfig = new JobConfigurationQuery()
.setQuery(querySql);
if(batch){
query_config.setPriority("BATCH");

if (batch) {
queryConfig.setPriority("BATCH");
}

Job job = new Job().setConfiguration(
new JobConfiguration().setQuery(query_config));
new JobConfiguration().setQuery(queryConfig));

return bigquery.jobs().insert(projectId, job).execute();
}
// [END asyncQuery]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/*
* Copyright (c) 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

Expand All @@ -25,32 +27,61 @@
import java.io.IOException;
import java.util.Collection;

public class BigqueryServiceFactory {
/**
* This class creates our Service to connect to Bigquery including auth.
*/
public final class BigqueryServiceFactory {

/**
* Private constructor to disable creation of this utility Factory class.
*/
private BigqueryServiceFactory() {

}

/**
* Singleton service used through the app.
*/
private static Bigquery service = null;
private static Object service_lock = new Object();

public static Bigquery getService() throws IOException{
if(service==null){
synchronized(service_lock){
if(service==null){
service=createAuthorizedClient();
/**
* Mutex created to create the singleton in thread-safe fashion.
*/
private static Object serviceLock = new Object();

/**
* Threadsafe Factory that provides an authorized Bigquery service.
* @return The Bigquery service
* @throws IOException Thronw if there is an error connecting to Bigquery.
*/
public static Bigquery getService() throws IOException {
if (service == null) {
synchronized (serviceLock) {
if (service == null) {
service = createAuthorizedClient();
}
}
}
return service;
}

/**
* Creates an authorized client to Google Bigquery.
* @return The BigQuery Service
* @throws IOException Thrown if there is an error connecting
*/
// [START get_service]
private static Bigquery createAuthorizedClient() throws IOException {
Collection<String> BIGQUERY_SCOPES = BigqueryScopes.all();
HttpTransport TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(TRANSPORT, JSON_FACTORY);
if(credential.createScopedRequired()){
credential = credential.createScoped(BIGQUERY_SCOPES);
Collection<String> bigqueryScopes = BigqueryScopes.all();
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(
transport, jsonFactory);
if (credential.createScopedRequired()) {
credential = credential.createScoped(bigqueryScopes);
}
return new Bigquery.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("BigQuery Samples").build();
return new Bigquery.Builder(transport, jsonFactory, credential)
.setApplicationName("BigQuery Samples").build();
}
// [END get_service]

Expand Down
Loading

0 comments on commit d918b3c

Please sign in to comment.