Skip to content

Commit

Permalink
Merge pull request #33 from GoogleCloudPlatform/bq-getting-started
Browse files Browse the repository at this point in the history
Move test over from other repo.
  • Loading branch information
jerjou committed Sep 8, 2015
2 parents 8f0eb9b + 2931cc2 commit c7fb77a
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright (c) 2012 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.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
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.client.util.store.DataStoreFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.bigquery.Bigquery.Datasets;
import com.google.api.services.bigquery.Bigquery.Jobs.Insert;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.BigqueryScopes;
import com.google.api.services.bigquery.model.DatasetList;
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 com.google.api.services.bigquery.model.JobReference;
import com.google.api.services.bigquery.model.QueryRequest;
import com.google.api.services.bigquery.model.QueryResponse;
import com.google.api.services.bigquery.model.TableCell;
import com.google.api.services.bigquery.model.TableRow;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


/**
* Example of authorizing with Bigquery and reading from a public dataset.
*
* Specifically, this queries the shakespeare dataset to fetch the 10 of Shakespeare's works with
* the greatest number of distinct words.
*/
public class GettingStarted {
// [START build_service]
/**
* Creates an authorized Bigquery client service using Application Default Credentials.
*
* @return an authorized Bigquery client
* @throws IOException if there's an error getting the default credentials.
*/
public static Bigquery createAuthorizedClient() throws IOException {
// Create the credential
HttpTransport transport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);

// Depending on the environment that provides the default credentials (e.g. Compute Engine, App
// Engine), the credentials may require us to specify the scopes we need explicitly.
// Check for this case, and inject the Bigquery scope if required.
if (credential.createScopedRequired()) {
credential = credential.createScoped(BigqueryScopes.all());
}

return new Bigquery.Builder(transport, jsonFactory, credential)
.setApplicationName("Bigquery Samples").build();
}
// [END build_service]

// [START run_query]
/**
* Executes the given query synchronously.
*
* @param querySql the query to execute.
* @param bigquery the Bigquery service object.
* @param projectId the id of the project under which to run the query.
* @return a list of the results of the query.
* @throws IOException if there's an error communicating with the API.
*/
private static List<TableRow> executeQuery(String querySql, Bigquery bigquery, String projectId)
throws IOException {
QueryResponse query = bigquery.jobs().query(
projectId,
new QueryRequest().setQuery(querySql))
.execute();

// Execute it
GetQueryResultsResponse queryResult = bigquery.jobs().getQueryResults(
query.getJobReference().getProjectId(),
query.getJobReference().getJobId()).execute();

return queryResult.getRows();
}
// [END run_query]

// [START print_results]
/**
* Prints the results to standard out.
*
* @param rows the rows to print.
*/
private static void printResults(List<TableRow> rows) {
System.out.print("\nQuery Results:\n------------\n");
for (TableRow row : rows) {
for (TableCell field : row.getF()) {
System.out.printf("%-50s", field.getV());
}
System.out.println();
}
}
// [END print_results]

/**
* Exercises the methods defined in this class.
*
* In particular, it creates an authorized Bigquery service object using Application Default
* Credentials, then executes a query against the public Shakespeare dataset and prints out the
* results.
*
* @param args the first argument, if it exists, should be the id of the project to run the test
* under. If no arguments are given, it will prompt for it.
* @throws IOException if there's an error communicating with the API.
*/
public static void main(String[] args) throws IOException {
Scanner sc;
if(args.length == 0) {
// Prompt the user to enter the id of the project to run the queries under
System.out.print("Enter the project ID: ");
sc = new Scanner(System.in);
} else {
sc = new Scanner(args[0]);
}
String projectId = sc.nextLine();

// Create a new Bigquery client authorized via OAuth 2.0 protocol
// dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
Bigquery bigquery = createAuthorizedClient();

List<TableRow> rows = executeQuery("SELECT TOP(corpus, 10) as title, COUNT(*) as unique_words "
+ "FROM [publicdata:samples.shakespeare]", bigquery, projectId);

printResults(rows);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.google.cloud.bigquery.samples.test;

import com.google.api.services.bigquery.model.GetQueryResultsResponse;
import com.google.cloud.bigquery.samples.GettingStarted;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;

import static com.jcabi.matchers.RegexMatchers.*;
import static org.junit.Assert.*;
import static org.junit.matchers.JUnitMatchers.*;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Iterator;


/**
* Test for GettingStarted.java
*/
public class GettingStartedTest extends BigquerySampleTest {
private final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
private final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
private static final PrintStream REAL_OUT = System.out;
private static final PrintStream REAL_ERR = System.err;

public GettingStartedTest() throws FileNotFoundException {
super();
}

@Before
public void setUp() {
System.setOut(new PrintStream(stdout));
System.setErr(new PrintStream(stderr));
}

@After
public void tearDown() {
System.setOut(REAL_OUT);
System.setErr(REAL_ERR);
}

@Test
public void testSyncQuery() throws IOException {
GettingStarted.main(new String[] { CONSTANTS.getProjectId() });
String out = stdout.toString();
assertThat(out, containsPattern("Query Results:"));
assertThat(out, containsString("hamlet"));
}
}

0 comments on commit c7fb77a

Please sign in to comment.