Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 59426df

Browse files
authoredJun 4, 2020
docs(samples): adding simple query example for completeness (#417)
1 parent 16a956d commit 59426df

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_query]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.QueryJobConfiguration;
24+
import com.google.cloud.bigquery.TableResult;
25+
26+
public class SimpleQuery {
27+
28+
public static void runSimpleQuery() {
29+
// TODO(developer): Replace this query before running the sample.
30+
String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
31+
simpleQuery(query);
32+
}
33+
34+
public static void simpleQuery(String query) {
35+
try {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests.
38+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
39+
40+
// Create the query job.
41+
QueryJobConfiguration queryConfig =
42+
QueryJobConfiguration.newBuilder(query).build();
43+
44+
// Execute the query.
45+
TableResult result = bigquery.query(queryConfig);
46+
47+
// Print the results.
48+
result.iterateAll().forEach(rows -> rows.forEach(row -> System.out.println(row.getValue())));
49+
50+
System.out.println("Query ran successfully");
51+
} catch (BigQueryException | InterruptedException e) {
52+
System.out.println("Query did not run \n" + e.toString());
53+
}
54+
}
55+
}
56+
// [END bigquery_query]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
public class SimpleQueryIT {
28+
private ByteArrayOutputStream bout;
29+
private PrintStream out;
30+
31+
@Before
32+
public void setUp() {
33+
bout = new ByteArrayOutputStream();
34+
out = new PrintStream(bout);
35+
System.setOut(out);
36+
}
37+
38+
@After
39+
public void tearDown() {
40+
System.setOut(null);
41+
}
42+
43+
@Test
44+
public void testSimpleQuery() {
45+
String query =
46+
"SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
47+
48+
SimpleQuery.simpleQuery(query);
49+
assertThat(bout.toString()).contains("Query ran successfully");
50+
}
51+
}

0 commit comments

Comments
 (0)
Failed to load comments.