Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Bigquery samples from /bigquery-samples-python #8

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions bigquery/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud.bigquery.samples</groupId>
<artifactId>bq-gettingstarted</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>bq-gettingstarted</name>
<url>http://maven.apache.org</url>

<repositories>
<repository>
<id>googleapis</id>
<url>https://google-api-client-libraries.appspot.com/mavenrepo</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-bigquery</artifactId>
<version>v2-rev158-1.19.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>${project.oauth.version}</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>${project.http.version}</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>${project.oauth.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>

<properties>
<project.http.version>1.19.0</project.http.version>
<project.oauth.version>1.19.0</project.oauth.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>5</source>
<target>5</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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
*
* 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
* the License.
*/

package com.google.cloud.bigquery.samples;


import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.Bigquery.Jobs.GetQueryResults;
import com.google.api.services.bigquery.model.GetQueryResultsResponse;
import com.google.api.services.bigquery.model.Job;
import com.google.api.services.bigquery.model.JobConfiguration;
import com.google.api.services.bigquery.model.JobConfigurationQuery;

import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;


/**
* Example of authorizing with BigQuery and reading from a public dataset.
*/
public class AsyncQuerySample extends BigqueryUtils{


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

Scanner scanner = new Scanner(System.in);
System.out.println("Enter your project id: ");
String projectId = scanner.nextLine();
System.out.println("Enter your query string: ");
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): ");
long waitTime = scanner.nextLong();
scanner.close();
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{

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,
//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
*
* @param bigquery an authorized BigQuery client
* @param projectId a String containing the project ID
* @param querySql the actual query string
* @return a reference to the inserted query job
* @throws IOException
*/
public static Job asyncQuery(Bigquery bigquery,
String projectId,
String querySql,
boolean batch) throws IOException {

JobConfigurationQuery query_config = new JobConfigurationQuery()
.setQuery(querySql);

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

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

return bigquery.jobs().insert(projectId, job).execute();
}
// [END asyncQuery]

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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
*
* 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
* the License.
*/

package com.google.cloud.bigquery.samples;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.BigqueryScopes;

import java.io.IOException;
import java.util.Collection;

public class BigqueryServiceFactory {

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();
}
}
}
return service;
}

// [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);
}
return new Bigquery.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("BigQuery Samples").build();
}
// [END get_service]

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
Copyright 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

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 the License.
*/
package com.google.cloud.bigquery.samples;

import com.google.api.client.json.GenericJson;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.Bigquery.Datasets;
import com.google.api.services.bigquery.BigqueryRequest;
import com.google.api.services.bigquery.model.DatasetList;
import com.google.api.services.bigquery.model.Job;
import com.google.api.services.bigquery.model.TableCell;
import com.google.api.services.bigquery.model.TableFieldSchema;
import com.google.api.services.bigquery.model.TableRow;
import com.google.api.services.bigquery.model.TableSchema;
import com.google.gson.Gson;

import java.io.IOException;
import java.io.PrintStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

/**
* TODO: Insert description here. (generated by elibixby)
*/
public class BigqueryUtils {

// [START print_rows]
public static void printRows(List<TableRow> rows, PrintStream out){
for (TableRow row : rows) {
for (TableCell field : row.getF()) {
out.printf("%-50s", field.getV());
}
out.println();
}
}
// [END print_rows]

// [START poll_job]
public static Job pollJob(Bigquery.Jobs.Get request, long interval)
throws IOException, InterruptedException{
Job job = request.execute();
while(!job.getStatus().getState().equals("DONE")) {
System.out.println("Job is "
+ job.getStatus().getState()
+ " waiting " + interval + " milliseconds...");
Thread.sleep(interval);
job = request.execute();
}
return job;
}
// [END poll_job]

// [START paging]
public static <T extends GenericJson> Iterator<T> getPages(BigqueryRequest<T> request_template){

class PageIterator implements Iterator<T>{

BigqueryRequest<T> request;
boolean hasNext = true;

public PageIterator(BigqueryRequest<T> request_template){
this.request = request_template;
}

public boolean hasNext() {
return hasNext ;
}

public T next() {
if(!hasNext){
throw new NoSuchElementException();
}
try {
T response = request.execute();
if (response.containsKey("pageToken")) {
request = request.set("pageToken", response.get("pageToken"));
} else {
hasNext = false;
}
return response;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public void remove() {
this.next();
}
}

return new PageIterator(request_template);
}
// [END paging]

// [START load_schema]
public static TableSchema loadSchema(Reader schemaSource){
TableSchema sourceSchema = new TableSchema();

List<TableFieldSchema> fields = (new Gson()).<List<TableFieldSchema>>fromJson(
schemaSource,
(new ArrayList<TableFieldSchema>()).getClass());

sourceSchema.setFields(fields);

return sourceSchema;
}
// [END load_schema]

// [START list_datasets]
/**
* Display all BigQuery datasets associated with a project
*
* @param bigquery an authorized BigQuery client
* @param projectId a string containing the current project ID
* @throws IOException
*/
public static void listDatasets(Bigquery bigquery, String projectId)
throws IOException {
Datasets.List datasetRequest = bigquery.datasets().list(projectId);
DatasetList datasetList = datasetRequest.execute();
if (datasetList.getDatasets() != null) {
List<DatasetList.Datasets> datasets = datasetList.getDatasets();
System.out.println("Available datasets\n----------------");
System.out.println(datasets.toString());
for (DatasetList.Datasets dataset : datasets) {
System.out.format("%s\n", dataset.getDatasetReference().getDatasetId());
}
}
}
// [END list_datasets]

}
Loading