-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from GoogleCloudPlatform/bigquery-quickstart
Move in bigquery quickstart sample.
- Loading branch information
Showing
6 changed files
with
259 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
bigquery/src/main/java/com/google/cloud/bigquery/samples/ListDatasetsProjects.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
bigquery/src/test/java/com/google/cloud/bigquery/samples/test/ListDatasetsProjectsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters