Skip to content

Commit

Permalink
Merge pull request #25 from GoogleCloudPlatform/bigquery-quickstart
Browse files Browse the repository at this point in the history
Move in bigquery quickstart sample.
  • Loading branch information
jerjou committed Jul 31, 2015
2 parents 1d03397 + fd96b73 commit e7c7050
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 3 deletions.
4 changes: 4 additions & 0 deletions bigquery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-matchers</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* 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.util.Data;
import com.google.api.services.bigquery.Bigquery;
import com.google.api.services.bigquery.Bigquery.Datasets;
import com.google.api.services.bigquery.model.DatasetList;
import com.google.api.services.bigquery.model.GetQueryResultsResponse;
import com.google.api.services.bigquery.model.ProjectList;
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.IOException;
import java.io.PrintStream;
import java.lang.Thread;
import java.util.List;

/**
* Invokes the BigQuery basic APIs for the given project id specified.
*
* Samples used in this page:
*
* https://cloud.google.com/bigquery/bigquery-api-quickstart
*/
public class ListDatasetsProjects {
/**
* Run the sample.
*/
public static void main(String[] args) throws IOException, InterruptedException {
if (args.length != 1) {
System.err.println("Usage: QuickStart <project-id>");
return;
}
String projectId = args[0];

Bigquery bigquery = BigqueryServiceFactory.getService();
String query = "SELECT TOP( title, 10) as title, COUNT(*) as revision_count "
+ "FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;";

System.out.println();
System.out.println("----- Running the asynchronous query and printing it to stdout.");
runQueryRpcAndPrint(bigquery, projectId, query, System.out);

System.out.println();
System.out.println("----- Listing all the Datasets in the projectId");
listDatasets(bigquery, projectId);

System.out.println();
System.out.println("----- Listing all the Projects");
listProjects(bigquery);
}

/**
* Lists all Datasets in a project specified by the projectId.
*
* @param bigquery The BigQuery object.
* @param projectId The projectId from which lists the existing Datasets.
* @throws IOException if there's trouble with the network request.
*/
// [START listDatasets]
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("Dataset list:");

for (DatasetList.Datasets dataset : datasets) {
System.out.format("%s\n", dataset.getDatasetReference().getDatasetId());
}
}
}
// [END listDatasets]

/**
* Lists all Projects.
*
* @param bigquery The BigQuery object.
* @throws IOException if there's trouble with the network request.
*/
// [START listProjects]
public static void listProjects(Bigquery bigquery) throws IOException {
Bigquery.Projects.List projectListRequest = bigquery.projects().list();
ProjectList projectList = projectListRequest.execute();

if (projectList.getProjects() != null) {
List<ProjectList.Projects> projects = projectList.getProjects();
System.out.println("Project list:");

for (ProjectList.Projects project : projects) {
System.out.format("%s\n", project.getFriendlyName());
}
}
}
// [END listProjects]

/**
* Runs a synchronous BigQuery query and displays the result.
*
* @param bigquery An authorized BigQuery client
* @param projectId The current project id
* @param query A String containing a BigQuery SQL statement
* @param out A PrintStream for output, normally System.out
*/
static void runQueryRpcAndPrint(Bigquery bigquery, String projectId, String query,
PrintStream out) throws IOException, InterruptedException {
QueryRequest queryRequest = new QueryRequest().setQuery(query);
QueryResponse queryResponse = bigquery.jobs().query(projectId, queryRequest).execute();
if (queryResponse.getJobComplete()) {
printRows(queryResponse.getRows(), out);
if (null == queryResponse.getPageToken()) {
return;
}
}
// This loop polls until results are present, then loops over result pages.
String pageToken = null;
while (true) {
GetQueryResultsResponse queryResults = bigquery.jobs()
.getQueryResults(projectId, queryResponse.getJobReference().getJobId())
.setPageToken(pageToken).execute();
if (queryResults.getJobComplete()) {
printRows(queryResults.getRows(), out);
pageToken = queryResults.getPageToken();
if (null == pageToken) {
return;
}
}
Thread.sleep(500);
}
}

/**
* Print the given rows.
*
* @param rows the rows to print.
* @param out the place to print them.
*/
private static void printRows(java.util.List<TableRow> rows, PrintStream out) {
if (rows != null) {
for (TableRow row : rows) {
for (TableCell cell : row.getF()) {
// Data.isNull() is the recommended way to check for the 'null object' in TableCell.
out.printf("%s, ", Data.isNull(cell.getV()) ? "null" : cell.getV().toString());
}
out.println();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.test;

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

import com.google.cloud.bigquery.samples.ListDatasetsProjects;

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

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.lang.Exception;

/** Unit tests for {@link ListDatasetsProjects}. */
public class ListDatasetsProjectsTest extends BigquerySampleTest {

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

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;

@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 testUsage() throws Exception {
ListDatasetsProjects.main(new String[] {});
assertEquals("Usage: QuickStart <project-id>\n", stderr.toString());
}

@Test
public void testMain() throws Exception {
ListDatasetsProjects.main(new String[] { CONSTANTS.getProjectId() });
String out = stdout.toString();
assertThat(out, containsString("Running the asynchronous query"));
assertThat(out, containsPattern("George W. Bush, [0-9]+"));
assertThat(out, containsPattern("Wikipedia, [0-9]+"));

assertThat(out, containsString("Listing all the Datasets"));
assertThat(out, containsString("test_dataset"));

assertThat(out, containsString("Listing all the Projects"));
assertThat(out, containsString("Project list:"));
assertThat(out, containsPattern("Bigquery Samples|cloud-samples-tests"));
}
}
1 change: 0 additions & 1 deletion monitoring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-matchers</artifactId>
<version>1.3</version>
</dependency>
</dependencies>

Expand Down
6 changes: 4 additions & 2 deletions monitoring/src/test/java/CloudMonitoringAuthSampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class CloudMonitoringAuthSampleTest {
new ByteArrayOutputStream();
private final ByteArrayOutputStream stderr =
new ByteArrayOutputStream();
private static final PrintStream REAL_OUT = System.out;
private static final PrintStream REAL_ERR = System.err;

@Before
public void setUp() {
Expand All @@ -42,8 +44,8 @@ public void setUp() {

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

@Test
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@
<version>2.0.28-beta</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-matchers</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
Expand Down

0 comments on commit e7c7050

Please sign in to comment.