Skip to content

Commit 2931cc2

Browse files
author
Jerjou Cheng
committed
Move test over from other repo.
Also, tweak it so it's using ADC, and the synchronized query, since the doc says it does. This doc: https://cloud.google.com/bigquery/bigquery-api-quickstart
1 parent 8f0eb9b commit 2931cc2

File tree

2 files changed

+222
-0
lines changed

2 files changed

+222
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright (c) 2012 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+
19+
import com.google.api.client.auth.oauth2.Credential;
20+
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
21+
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
22+
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
23+
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
24+
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
25+
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
26+
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
27+
import com.google.api.client.http.HttpTransport;
28+
import com.google.api.client.http.javanet.NetHttpTransport;
29+
import com.google.api.client.json.JsonFactory;
30+
import com.google.api.client.json.jackson2.JacksonFactory;
31+
import com.google.api.client.util.store.DataStoreFactory;
32+
import com.google.api.client.util.store.FileDataStoreFactory;
33+
import com.google.api.services.bigquery.Bigquery.Datasets;
34+
import com.google.api.services.bigquery.Bigquery.Jobs.Insert;
35+
import com.google.api.services.bigquery.Bigquery;
36+
import com.google.api.services.bigquery.BigqueryScopes;
37+
import com.google.api.services.bigquery.model.DatasetList;
38+
import com.google.api.services.bigquery.model.GetQueryResultsResponse;
39+
import com.google.api.services.bigquery.model.Job;
40+
import com.google.api.services.bigquery.model.JobConfiguration;
41+
import com.google.api.services.bigquery.model.JobConfigurationQuery;
42+
import com.google.api.services.bigquery.model.JobReference;
43+
import com.google.api.services.bigquery.model.QueryRequest;
44+
import com.google.api.services.bigquery.model.QueryResponse;
45+
import com.google.api.services.bigquery.model.TableCell;
46+
import com.google.api.services.bigquery.model.TableRow;
47+
48+
import java.io.BufferedReader;
49+
import java.io.FileInputStream;
50+
import java.io.IOException;
51+
import java.io.InputStream;
52+
import java.io.InputStreamReader;
53+
import java.io.Reader;
54+
import java.util.Arrays;
55+
import java.util.List;
56+
import java.util.Scanner;
57+
58+
59+
/**
60+
* Example of authorizing with Bigquery and reading from a public dataset.
61+
*
62+
* Specifically, this queries the shakespeare dataset to fetch the 10 of Shakespeare's works with
63+
* the greatest number of distinct words.
64+
*/
65+
public class GettingStarted {
66+
// [START build_service]
67+
/**
68+
* Creates an authorized Bigquery client service using Application Default Credentials.
69+
*
70+
* @return an authorized Bigquery client
71+
* @throws IOException if there's an error getting the default credentials.
72+
*/
73+
public static Bigquery createAuthorizedClient() throws IOException {
74+
// Create the credential
75+
HttpTransport transport = new NetHttpTransport();
76+
JsonFactory jsonFactory = new JacksonFactory();
77+
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
78+
79+
// Depending on the environment that provides the default credentials (e.g. Compute Engine, App
80+
// Engine), the credentials may require us to specify the scopes we need explicitly.
81+
// Check for this case, and inject the Bigquery scope if required.
82+
if (credential.createScopedRequired()) {
83+
credential = credential.createScoped(BigqueryScopes.all());
84+
}
85+
86+
return new Bigquery.Builder(transport, jsonFactory, credential)
87+
.setApplicationName("Bigquery Samples").build();
88+
}
89+
// [END build_service]
90+
91+
// [START run_query]
92+
/**
93+
* Executes the given query synchronously.
94+
*
95+
* @param querySql the query to execute.
96+
* @param bigquery the Bigquery service object.
97+
* @param projectId the id of the project under which to run the query.
98+
* @return a list of the results of the query.
99+
* @throws IOException if there's an error communicating with the API.
100+
*/
101+
private static List<TableRow> executeQuery(String querySql, Bigquery bigquery, String projectId)
102+
throws IOException {
103+
QueryResponse query = bigquery.jobs().query(
104+
projectId,
105+
new QueryRequest().setQuery(querySql))
106+
.execute();
107+
108+
// Execute it
109+
GetQueryResultsResponse queryResult = bigquery.jobs().getQueryResults(
110+
query.getJobReference().getProjectId(),
111+
query.getJobReference().getJobId()).execute();
112+
113+
return queryResult.getRows();
114+
}
115+
// [END run_query]
116+
117+
// [START print_results]
118+
/**
119+
* Prints the results to standard out.
120+
*
121+
* @param rows the rows to print.
122+
*/
123+
private static void printResults(List<TableRow> rows) {
124+
System.out.print("\nQuery Results:\n------------\n");
125+
for (TableRow row : rows) {
126+
for (TableCell field : row.getF()) {
127+
System.out.printf("%-50s", field.getV());
128+
}
129+
System.out.println();
130+
}
131+
}
132+
// [END print_results]
133+
134+
/**
135+
* Exercises the methods defined in this class.
136+
*
137+
* In particular, it creates an authorized Bigquery service object using Application Default
138+
* Credentials, then executes a query against the public Shakespeare dataset and prints out the
139+
* results.
140+
*
141+
* @param args the first argument, if it exists, should be the id of the project to run the test
142+
* under. If no arguments are given, it will prompt for it.
143+
* @throws IOException if there's an error communicating with the API.
144+
*/
145+
public static void main(String[] args) throws IOException {
146+
Scanner sc;
147+
if(args.length == 0) {
148+
// Prompt the user to enter the id of the project to run the queries under
149+
System.out.print("Enter the project ID: ");
150+
sc = new Scanner(System.in);
151+
} else {
152+
sc = new Scanner(args[0]);
153+
}
154+
String projectId = sc.nextLine();
155+
156+
// Create a new Bigquery client authorized via OAuth 2.0 protocol
157+
// dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
158+
Bigquery bigquery = createAuthorizedClient();
159+
160+
List<TableRow> rows = executeQuery("SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words "
161+
+ "FROM [publicdata:samples.shakespeare]", bigquery, projectId);
162+
163+
printResults(rows);
164+
}
165+
166+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.google.cloud.bigquery.samples.test;
2+
3+
import com.google.api.services.bigquery.model.GetQueryResultsResponse;
4+
import com.google.cloud.bigquery.samples.GettingStarted;
5+
import com.google.gson.JsonIOException;
6+
import com.google.gson.JsonSyntaxException;
7+
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.junit.Test;
12+
13+
import static com.jcabi.matchers.RegexMatchers.*;
14+
import static org.junit.Assert.*;
15+
import static org.junit.matchers.JUnitMatchers.*;
16+
17+
import java.io.ByteArrayOutputStream;
18+
import java.io.FileNotFoundException;
19+
import java.io.IOException;
20+
import java.io.PrintStream;
21+
import java.util.Iterator;
22+
23+
24+
/**
25+
* Test for GettingStarted.java
26+
*/
27+
public class GettingStartedTest extends BigquerySampleTest {
28+
private final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
29+
private final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
30+
private static final PrintStream REAL_OUT = System.out;
31+
private static final PrintStream REAL_ERR = System.err;
32+
33+
public GettingStartedTest() throws FileNotFoundException {
34+
super();
35+
}
36+
37+
@Before
38+
public void setUp() {
39+
System.setOut(new PrintStream(stdout));
40+
System.setErr(new PrintStream(stderr));
41+
}
42+
43+
@After
44+
public void tearDown() {
45+
System.setOut(REAL_OUT);
46+
System.setErr(REAL_ERR);
47+
}
48+
49+
@Test
50+
public void testSyncQuery() throws IOException {
51+
GettingStarted.main(new String[] { CONSTANTS.getProjectId() });
52+
String out = stdout.toString();
53+
assertThat(out, containsPattern("Query Results:"));
54+
assertThat(out, containsString("hamlet"));
55+
}
56+
}

0 commit comments

Comments
 (0)