Skip to content

Commit e7c7050

Browse files
committed
Merge pull request #25 from GoogleCloudPlatform/bigquery-quickstart
Move in bigquery quickstart sample.
2 parents 1d03397 + fd96b73 commit e7c7050

File tree

6 files changed

+259
-3
lines changed

6 files changed

+259
-3
lines changed

bigquery/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
<groupId>junit</groupId>
4646
<artifactId>junit</artifactId>
4747
</dependency>
48+
<dependency>
49+
<groupId>com.jcabi</groupId>
50+
<artifactId>jcabi-matchers</artifactId>
51+
</dependency>
4852
<dependency>
4953
<groupId>com.google.code.gson</groupId>
5054
<artifactId>gson</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Copyright (c) 2015 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain a
6+
* copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.bigquery.samples;
18+
19+
import com.google.api.client.util.Data;
20+
import com.google.api.services.bigquery.Bigquery;
21+
import com.google.api.services.bigquery.Bigquery.Datasets;
22+
import com.google.api.services.bigquery.model.DatasetList;
23+
import com.google.api.services.bigquery.model.GetQueryResultsResponse;
24+
import com.google.api.services.bigquery.model.ProjectList;
25+
import com.google.api.services.bigquery.model.QueryRequest;
26+
import com.google.api.services.bigquery.model.QueryResponse;
27+
import com.google.api.services.bigquery.model.TableCell;
28+
import com.google.api.services.bigquery.model.TableRow;
29+
30+
import java.io.IOException;
31+
import java.io.PrintStream;
32+
import java.lang.Thread;
33+
import java.util.List;
34+
35+
/**
36+
* Invokes the BigQuery basic APIs for the given project id specified.
37+
*
38+
* Samples used in this page:
39+
*
40+
* https://cloud.google.com/bigquery/bigquery-api-quickstart
41+
*/
42+
public class ListDatasetsProjects {
43+
/**
44+
* Run the sample.
45+
*/
46+
public static void main(String[] args) throws IOException, InterruptedException {
47+
if (args.length != 1) {
48+
System.err.println("Usage: QuickStart <project-id>");
49+
return;
50+
}
51+
String projectId = args[0];
52+
53+
Bigquery bigquery = BigqueryServiceFactory.getService();
54+
String query = "SELECT TOP( title, 10) as title, COUNT(*) as revision_count "
55+
+ "FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;";
56+
57+
System.out.println();
58+
System.out.println("----- Running the asynchronous query and printing it to stdout.");
59+
runQueryRpcAndPrint(bigquery, projectId, query, System.out);
60+
61+
System.out.println();
62+
System.out.println("----- Listing all the Datasets in the projectId");
63+
listDatasets(bigquery, projectId);
64+
65+
System.out.println();
66+
System.out.println("----- Listing all the Projects");
67+
listProjects(bigquery);
68+
}
69+
70+
/**
71+
* Lists all Datasets in a project specified by the projectId.
72+
*
73+
* @param bigquery The BigQuery object.
74+
* @param projectId The projectId from which lists the existing Datasets.
75+
* @throws IOException if there's trouble with the network request.
76+
*/
77+
// [START listDatasets]
78+
public static void listDatasets(Bigquery bigquery, String projectId) throws IOException {
79+
Datasets.List datasetRequest = bigquery.datasets().list(projectId);
80+
DatasetList datasetList = datasetRequest.execute();
81+
82+
if (datasetList.getDatasets() != null) {
83+
List<DatasetList.Datasets> datasets = datasetList.getDatasets();
84+
System.out.println("Dataset list:");
85+
86+
for (DatasetList.Datasets dataset : datasets) {
87+
System.out.format("%s\n", dataset.getDatasetReference().getDatasetId());
88+
}
89+
}
90+
}
91+
// [END listDatasets]
92+
93+
/**
94+
* Lists all Projects.
95+
*
96+
* @param bigquery The BigQuery object.
97+
* @throws IOException if there's trouble with the network request.
98+
*/
99+
// [START listProjects]
100+
public static void listProjects(Bigquery bigquery) throws IOException {
101+
Bigquery.Projects.List projectListRequest = bigquery.projects().list();
102+
ProjectList projectList = projectListRequest.execute();
103+
104+
if (projectList.getProjects() != null) {
105+
List<ProjectList.Projects> projects = projectList.getProjects();
106+
System.out.println("Project list:");
107+
108+
for (ProjectList.Projects project : projects) {
109+
System.out.format("%s\n", project.getFriendlyName());
110+
}
111+
}
112+
}
113+
// [END listProjects]
114+
115+
/**
116+
* Runs a synchronous BigQuery query and displays the result.
117+
*
118+
* @param bigquery An authorized BigQuery client
119+
* @param projectId The current project id
120+
* @param query A String containing a BigQuery SQL statement
121+
* @param out A PrintStream for output, normally System.out
122+
*/
123+
static void runQueryRpcAndPrint(Bigquery bigquery, String projectId, String query,
124+
PrintStream out) throws IOException, InterruptedException {
125+
QueryRequest queryRequest = new QueryRequest().setQuery(query);
126+
QueryResponse queryResponse = bigquery.jobs().query(projectId, queryRequest).execute();
127+
if (queryResponse.getJobComplete()) {
128+
printRows(queryResponse.getRows(), out);
129+
if (null == queryResponse.getPageToken()) {
130+
return;
131+
}
132+
}
133+
// This loop polls until results are present, then loops over result pages.
134+
String pageToken = null;
135+
while (true) {
136+
GetQueryResultsResponse queryResults = bigquery.jobs()
137+
.getQueryResults(projectId, queryResponse.getJobReference().getJobId())
138+
.setPageToken(pageToken).execute();
139+
if (queryResults.getJobComplete()) {
140+
printRows(queryResults.getRows(), out);
141+
pageToken = queryResults.getPageToken();
142+
if (null == pageToken) {
143+
return;
144+
}
145+
}
146+
Thread.sleep(500);
147+
}
148+
}
149+
150+
/**
151+
* Print the given rows.
152+
*
153+
* @param rows the rows to print.
154+
* @param out the place to print them.
155+
*/
156+
private static void printRows(java.util.List<TableRow> rows, PrintStream out) {
157+
if (rows != null) {
158+
for (TableRow row : rows) {
159+
for (TableCell cell : row.getF()) {
160+
// Data.isNull() is the recommended way to check for the 'null object' in TableCell.
161+
out.printf("%s, ", Data.isNull(cell.getV()) ? "null" : cell.getV().toString());
162+
}
163+
out.println();
164+
}
165+
}
166+
}
167+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2015 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain a
6+
* copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package com.google.cloud.bigquery.samples.test;
18+
19+
import static com.jcabi.matchers.RegexMatchers.*;
20+
import static org.junit.Assert.*;
21+
import static org.junit.matchers.JUnitMatchers.*;
22+
23+
import com.google.cloud.bigquery.samples.ListDatasetsProjects;
24+
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.FileNotFoundException;
31+
import java.io.PrintStream;
32+
import java.lang.Exception;
33+
34+
/** Unit tests for {@link ListDatasetsProjects}. */
35+
public class ListDatasetsProjectsTest extends BigquerySampleTest {
36+
37+
public ListDatasetsProjectsTest() throws FileNotFoundException {
38+
super();
39+
}
40+
41+
private final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
42+
private final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
43+
private static final PrintStream REAL_OUT = System.out;
44+
private static final PrintStream REAL_ERR = System.err;
45+
46+
@Before
47+
public void setUp() {
48+
System.setOut(new PrintStream(stdout));
49+
System.setErr(new PrintStream(stderr));
50+
}
51+
52+
@After
53+
public void tearDown() {
54+
System.setOut(REAL_OUT);
55+
System.setErr(REAL_ERR);
56+
}
57+
58+
@Test
59+
public void testUsage() throws Exception {
60+
ListDatasetsProjects.main(new String[] {});
61+
assertEquals("Usage: QuickStart <project-id>\n", stderr.toString());
62+
}
63+
64+
@Test
65+
public void testMain() throws Exception {
66+
ListDatasetsProjects.main(new String[] { CONSTANTS.getProjectId() });
67+
String out = stdout.toString();
68+
assertThat(out, containsString("Running the asynchronous query"));
69+
assertThat(out, containsPattern("George W. Bush, [0-9]+"));
70+
assertThat(out, containsPattern("Wikipedia, [0-9]+"));
71+
72+
assertThat(out, containsString("Listing all the Datasets"));
73+
assertThat(out, containsString("test_dataset"));
74+
75+
assertThat(out, containsString("Listing all the Projects"));
76+
assertThat(out, containsString("Project list:"));
77+
assertThat(out, containsPattern("Bigquery Samples|cloud-samples-tests"));
78+
}
79+
}

monitoring/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
<dependency>
4949
<groupId>com.jcabi</groupId>
5050
<artifactId>jcabi-matchers</artifactId>
51-
<version>1.3</version>
5251
</dependency>
5352
</dependencies>
5453

monitoring/src/test/java/CloudMonitoringAuthSampleTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class CloudMonitoringAuthSampleTest {
3333
new ByteArrayOutputStream();
3434
private final ByteArrayOutputStream stderr =
3535
new ByteArrayOutputStream();
36+
private static final PrintStream REAL_OUT = System.out;
37+
private static final PrintStream REAL_ERR = System.err;
3638

3739
@Before
3840
public void setUp() {
@@ -42,8 +44,8 @@ public void setUp() {
4244

4345
@After
4446
public void tearDown() {
45-
System.setOut(null);
46-
System.setErr(null);
47+
System.setOut(this.REAL_OUT);
48+
System.setErr(this.REAL_ERR);
4749
}
4850

4951
@Test

pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@
119119
<version>2.0.28-beta</version>
120120
<scope>test</scope>
121121
</dependency>
122+
<dependency>
123+
<groupId>com.jcabi</groupId>
124+
<artifactId>jcabi-matchers</artifactId>
125+
<version>1.3</version>
126+
</dependency>
122127
<dependency>
123128
<groupId>com.google.appengine</groupId>
124129
<artifactId>appengine-testing</artifactId>

0 commit comments

Comments
 (0)