Skip to content

Commit 1b64da0

Browse files
committed
Merge pull request GoogleCloudPlatform#10 from GoogleCloudPlatform/bigquery
Added Files from bigquery-samples-python/java
2 parents a25adc9 + e73d9fe commit 1b64da0

18 files changed

+1157
-5
lines changed

bigquery/pom.xml

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.google.cloud.bigquery.samples</groupId>
5+
<artifactId>bigquery-samples</artifactId>
6+
<packaging>jar</packaging>
7+
8+
<parent>
9+
<artifactId>doc-samples</artifactId>
10+
<groupId>com.google.cloud</groupId>
11+
<version>1.0.0</version>
12+
<relativePath>..</relativePath>
13+
</parent>
14+
15+
16+
<repositories>
17+
<repository>
18+
<id>googleapis</id>
19+
<url>https://google-api-client-libraries.appspot.com/mavenrepo</url>
20+
</repository>
21+
</repositories>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>com.google.apis</groupId>
26+
<artifactId>google-api-services-bigquery</artifactId>
27+
<version>v2-rev158-1.19.0</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.google.oauth-client</groupId>
31+
<artifactId>google-oauth-client</artifactId>
32+
<version>${project.oauth.version}</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.google.http-client</groupId>
36+
<artifactId>google-http-client-jackson2</artifactId>
37+
<version>${project.http.version}</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.google.oauth-client</groupId>
41+
<artifactId>google-oauth-client-jetty</artifactId>
42+
<version>${project.oauth.version}</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.google.code.gson</groupId>
50+
<artifactId>gson</artifactId>
51+
<version>2.3.1</version>
52+
</dependency>
53+
</dependencies>
54+
55+
<properties>
56+
<project.http.version>1.19.0</project.http.version>
57+
<project.oauth.version>1.19.0</project.oauth.version>
58+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
59+
</properties>
60+
61+
<build>
62+
<sourceDirectory>src/main/java</sourceDirectory>
63+
<resources>
64+
<resource>
65+
<directory>src/main/resources</directory>
66+
</resource>
67+
</resources>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-compiler-plugin</artifactId>
72+
<version>3.2</version>
73+
<configuration>
74+
<source>5</source>
75+
<target>5</target>
76+
</configuration>
77+
</plugin>
78+
</plugins>
79+
</build>
80+
81+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2015 Google Inc.
3+
*
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
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
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
12+
* the License.
13+
*/
14+
15+
package com.google.cloud.bigquery.samples;
16+
17+
18+
import com.google.api.services.bigquery.Bigquery;
19+
import com.google.api.services.bigquery.Bigquery.Jobs.GetQueryResults;
20+
import com.google.api.services.bigquery.model.GetQueryResultsResponse;
21+
import com.google.api.services.bigquery.model.Job;
22+
import com.google.api.services.bigquery.model.JobConfiguration;
23+
import com.google.api.services.bigquery.model.JobConfigurationQuery;
24+
25+
import java.io.IOException;
26+
import java.util.Iterator;
27+
import java.util.Scanner;
28+
29+
30+
/**
31+
* Example of authorizing with BigQuery and reading from a public dataset.
32+
*/
33+
public class AsyncQuerySample extends BigqueryUtils{
34+
35+
36+
// [START main]
37+
/**
38+
* @param args
39+
* @throws IOException
40+
* @throws InterruptedException
41+
*/
42+
public static void main(String[] args)
43+
throws IOException, InterruptedException {
44+
45+
Scanner scanner = new Scanner(System.in);
46+
System.out.println("Enter your project id: ");
47+
String projectId = scanner.nextLine();
48+
System.out.println("Enter your query string: ");
49+
String queryString = scanner.nextLine();
50+
System.out.println("Run query in batch mode? [true|false] ");
51+
boolean batch = Boolean.valueOf(scanner.nextLine());
52+
System.out.println("Enter how often to check if your job is complete (milliseconds): ");
53+
long waitTime = scanner.nextLong();
54+
scanner.close();
55+
Iterator<GetQueryResultsResponse> pages = run(projectId, queryString, batch, waitTime);
56+
while(pages.hasNext()){
57+
printRows(pages.next().getRows(), System.out);
58+
}
59+
60+
}
61+
// [END main]
62+
63+
// [START run]
64+
public static Iterator<GetQueryResultsResponse> run(String projectId,
65+
String queryString,
66+
boolean batch,
67+
long waitTime)
68+
throws IOException, InterruptedException{
69+
70+
Bigquery bigquery = BigqueryServiceFactory.getService();
71+
72+
Job query = asyncQuery(bigquery, projectId, queryString, batch);
73+
Bigquery.Jobs.Get getRequest = bigquery.jobs().get(
74+
projectId, query.getJobReference().getJobId());
75+
76+
//Poll every waitTime milliseconds,
77+
//retrying at most retries times if there are errors
78+
pollJob(getRequest, waitTime);
79+
80+
GetQueryResults resultsRequest = bigquery.jobs().getQueryResults(
81+
projectId, query.getJobReference().getJobId());
82+
83+
return getPages(resultsRequest);
84+
}
85+
// [END run]
86+
87+
// [START asyncQuery]
88+
/**
89+
* Inserts an asynchronous query Job for a particular query
90+
*
91+
* @param bigquery an authorized BigQuery client
92+
* @param projectId a String containing the project ID
93+
* @param querySql the actual query string
94+
* @return a reference to the inserted query job
95+
* @throws IOException
96+
*/
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()
103+
.setQuery(querySql);
104+
105+
if(batch){
106+
query_config.setPriority("BATCH");
107+
}
108+
109+
Job job = new Job().setConfiguration(
110+
new JobConfiguration().setQuery(query_config));
111+
112+
return bigquery.jobs().insert(projectId, job).execute();
113+
}
114+
// [END asyncQuery]
115+
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2015 Google Inc.
3+
*
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
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
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
12+
* the License.
13+
*/
14+
15+
package com.google.cloud.bigquery.samples;
16+
17+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
18+
import com.google.api.client.http.HttpTransport;
19+
import com.google.api.client.http.javanet.NetHttpTransport;
20+
import com.google.api.client.json.JsonFactory;
21+
import com.google.api.client.json.jackson2.JacksonFactory;
22+
import com.google.api.services.bigquery.Bigquery;
23+
import com.google.api.services.bigquery.BigqueryScopes;
24+
25+
import java.io.IOException;
26+
import java.util.Collection;
27+
28+
public class BigqueryServiceFactory {
29+
30+
private static Bigquery service = null;
31+
private static Object service_lock = new Object();
32+
33+
public static Bigquery getService() throws IOException{
34+
if(service==null){
35+
synchronized(service_lock){
36+
if(service==null){
37+
service=createAuthorizedClient();
38+
}
39+
}
40+
}
41+
return service;
42+
}
43+
44+
// [START get_service]
45+
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);
52+
}
53+
return new Bigquery.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("BigQuery Samples").build();
54+
}
55+
// [END get_service]
56+
57+
}

0 commit comments

Comments
 (0)