Skip to content

Commit 75bdcb7

Browse files
author
Jerjou Cheng
committed
Move in bigquery quickstart sample.
1 parent 1d03397 commit 75bdcb7

File tree

6 files changed

+257
-3
lines changed

6 files changed

+257
-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,165 @@
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.util.List;
33+
34+
/**
35+
* Invokes the BigQuery basic APIs for the given project id specified.
36+
*
37+
* Samples used in this page:
38+
*
39+
* https://cloud.google.com/bigquery/bigquery-api-quickstart
40+
*/
41+
public class ListDatasetsProjects {
42+
/**
43+
* Run the sample.
44+
*/
45+
public static void main(String[] args) throws IOException {
46+
if (args.length != 1) {
47+
System.err.println("Usage: QuickStart <project-id>");
48+
return;
49+
}
50+
String projectId = args[0];
51+
52+
Bigquery bigquery = BigqueryServiceFactory.getService();
53+
String query = "SELECT TOP( title, 10) as title, COUNT(*) as revision_count "
54+
+ "FROM [publicdata:samples.wikipedia] WHERE wp_namespace = 0;";
55+
56+
System.out.println();
57+
System.out.println("----- Running the asynchronous query and printing it to stdout.");
58+
runQueryRpcAndPrint(bigquery, projectId, query, System.out);
59+
60+
System.out.println();
61+
System.out.println("----- Listing all the Datasets in the projectId");
62+
listDatasets(bigquery, projectId);
63+
64+
System.out.println();
65+
System.out.println("----- Listing all the Projects");
66+
listProjects(bigquery);
67+
}
68+
69+
/**
70+
* Lists all Datasets in a project specified by the projectId.
71+
*
72+
* @param bigquery The BigQuery object.
73+
* @param projectId The projectId from which lists the existing Datasets.
74+
* @throws IOException if there's trouble with the network request.
75+
*/
76+
// [START listDatasets]
77+
public static void listDatasets(Bigquery bigquery, String projectId) throws IOException {
78+
Datasets.List datasetRequest = bigquery.datasets().list(projectId);
79+
DatasetList datasetList = datasetRequest.execute();
80+
81+
if (datasetList.getDatasets() != null) {
82+
List<DatasetList.Datasets> datasets = datasetList.getDatasets();
83+
System.out.println("Dataset list:");
84+
85+
for (DatasetList.Datasets dataset : datasets) {
86+
System.out.format("%s\n", dataset.getDatasetReference().getDatasetId());
87+
}
88+
}
89+
}
90+
// [END listDatasets]
91+
92+
/**
93+
* Lists all Projects.
94+
*
95+
* @param bigquery The BigQuery object.
96+
* @throws IOException if there's trouble with the network request.
97+
*/
98+
// [START listProjects]
99+
public static void listProjects(Bigquery bigquery) throws IOException {
100+
Bigquery.Projects.List projectListRequest = bigquery.projects().list();
101+
ProjectList projectList = projectListRequest.execute();
102+
103+
if (projectList.getProjects() != null) {
104+
List<ProjectList.Projects> projects = projectList.getProjects();
105+
System.out.println("Project list:");
106+
107+
for (ProjectList.Projects project : projects) {
108+
System.out.format("%s\n", project.getFriendlyName());
109+
}
110+
}
111+
}
112+
// [END listProjects]
113+
114+
/**
115+
* Runs a synchronous BigQuery query and displays the result.
116+
*
117+
* @param bigquery An authorized BigQuery client
118+
* @param projectId The current project id
119+
* @param query A String containing a BigQuery SQL statement
120+
* @param out A PrintStream for output, normally System.out
121+
*/
122+
static void runQueryRpcAndPrint(Bigquery bigquery, String projectId, String query,
123+
PrintStream out) throws IOException {
124+
QueryRequest queryRequest = new QueryRequest().setQuery(query);
125+
QueryResponse queryResponse = bigquery.jobs().query(projectId, queryRequest).execute();
126+
if (queryResponse.getJobComplete()) {
127+
printRows(queryResponse.getRows(), out);
128+
if (null == queryResponse.getPageToken()) {
129+
return;
130+
}
131+
}
132+
// This loop polls until results are present, then loops over result pages.
133+
String pageToken = null;
134+
while (true) {
135+
GetQueryResultsResponse queryResults = bigquery.jobs()
136+
.getQueryResults(projectId, queryResponse.getJobReference().getJobId())
137+
.setPageToken(pageToken).execute();
138+
if (queryResults.getJobComplete()) {
139+
printRows(queryResults.getRows(), out);
140+
pageToken = queryResults.getPageToken();
141+
if (null == pageToken) {
142+
return;
143+
}
144+
}
145+
}
146+
}
147+
148+
/**
149+
* Print the given rows.
150+
*
151+
* @param rows the rows to print.
152+
* @param out the place to print them.
153+
*/
154+
private static void printRows(java.util.List<TableRow> rows, PrintStream out) {
155+
if (rows != null) {
156+
for (TableRow row : rows) {
157+
for (TableCell cell : row.getF()) {
158+
// Data.isNull() is the recommended way to check for the 'null object' in TableCell.
159+
out.printf("%s, ", Data.isNull(cell.getV()) ? "null" : cell.getV().toString());
160+
}
161+
out.println();
162+
}
163+
}
164+
}
165+
}
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.PrintStream;
31+
import java.io.IOException;
32+
import java.io.FileNotFoundException;
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 IOException {
60+
ListDatasetsProjects.main(new String[] {});
61+
assertEquals("Usage: QuickStart <project-id>\n", stderr.toString());
62+
}
63+
64+
@Test
65+
public void testMain() throws IOException {
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, containsString("Bigquery Samples"));
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)