Skip to content

Commit e5db01c

Browse files
author
Praful Makani
authored
docs(samples): add query (#463)
* docs(samples): add query * docs(samples): add comment
1 parent 68140d5 commit e5db01c

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
// Sample to query in a table
27+
public class Query {
28+
29+
public static void runQuery() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "MY_PROJECT_ID";
32+
String datasetName = "MY_DATASET_NAME";
33+
String tableName = "MY_TABLE_NAME";
34+
String query =
35+
"SELECT name, SUM(number) as total_people\n"
36+
+ " FROM `"
37+
+ projectId
38+
+ "."
39+
+ datasetName
40+
+ "."
41+
+ tableName
42+
+ "`"
43+
+ " WHERE state = 'TX'"
44+
+ " GROUP BY name, state"
45+
+ " ORDER BY total_people DESC"
46+
+ " LIMIT 20";
47+
query(query);
48+
}
49+
50+
public static void query(String query) {
51+
try {
52+
// Initialize client that will be used to send requests. This client only needs to be created
53+
// once, and can be reused for multiple requests.
54+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
55+
56+
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build();
57+
58+
TableResult results = bigquery.query(queryConfig);
59+
60+
results
61+
.iterateAll()
62+
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
63+
64+
System.out.println("Query performed successfully.");
65+
} catch (BigQueryException | InterruptedException e) {
66+
System.out.println("Query not performed \n" + e.toString());
67+
}
68+
}
69+
}
70+
// [END bigquery_query]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 QueryIT {
28+
29+
private ByteArrayOutputStream bout;
30+
private PrintStream out;
31+
32+
@Before
33+
public void setUp() {
34+
bout = new ByteArrayOutputStream();
35+
out = new PrintStream(bout);
36+
System.setOut(out);
37+
}
38+
39+
@After
40+
public void tearDown() {
41+
System.setOut(null);
42+
}
43+
44+
@Test
45+
public void testQuery() {
46+
String query =
47+
"SELECT name, SUM(number) as total_people"
48+
+ " FROM `bigquery-public-data.usa_names.usa_1910_2013`"
49+
+ " WHERE state = 'TX'"
50+
+ " GROUP BY name, state"
51+
+ " ORDER BY total_people DESC"
52+
+ " LIMIT 20";
53+
54+
Query.query(query);
55+
assertThat(bout.toString()).contains("Query performed successfully.");
56+
}
57+
}

0 commit comments

Comments
 (0)