Skip to content

Commit df59cc7

Browse files
feat: new sample - Query: Clustered Table (#121)
* feat: new sample - Query: Clustered Table * update base on comments, refactoring
1 parent 93f6cc2 commit df59cc7

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed

‎samples/src/main/java/com/example/bigquery/CreatePartitionedTable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public static void createPartitionedTable(String datasetName, String tableName)
5050
Schema schema =
5151
Schema.of(
5252
Field.of("stringField", StandardSQLTypeName.STRING),
53-
Field.of("booleanField", StandardSQLTypeName.BOOL));
54-
Field.of("dateField", StandardSQLTypeName.DATE);
53+
Field.of("booleanField", StandardSQLTypeName.BOOL),
54+
Field.of("dateField", StandardSQLTypeName.DATE));
5555

5656
StandardTableDefinition tableDefinition =
5757
StandardTableDefinition.newBuilder()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_clustered_table]
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 QueryClusteredTable {
27+
28+
public static void runQueryClusteredTable() throws Exception {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "MY_PROJECT_ID";
31+
String datasetName = "MY_DATASET_NAME";
32+
String tableName = "MY_TABLE_NAME";
33+
queryClusteredTable(projectId, datasetName, tableName);
34+
}
35+
36+
public static void queryClusteredTable(String projectId, String datasetName, String tableName) {
37+
try {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests.
40+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
41+
42+
String sourceTable = "`" + projectId + "." + datasetName + "." + tableName + "`";
43+
String query =
44+
"SELECT word, word_count\n"
45+
+ "FROM "
46+
+ sourceTable
47+
+ "\n"
48+
// Optimize query performance by filtering the clustered columns in sort order
49+
+ "WHERE corpus = 'romeoandjuliet'\n"
50+
+ "AND word_count >= 1";
51+
52+
QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build();
53+
54+
TableResult results = bigquery.query(queryConfig);
55+
56+
results
57+
.iterateAll()
58+
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
59+
60+
System.out.println("Query clustered table performed successfully.");
61+
} catch (BigQueryException | InterruptedException e) {
62+
System.out.println("Query not performed \n" + e.toString());
63+
}
64+
}
65+
}
66+
// [END bigquery_query_clustered_table]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 QueryClusteredTableIT {
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 queryClusteredTable() {
45+
String projectId = "java-docs-samples-testing";
46+
String datasetName = "bigquery_test_dataset";
47+
String tableName = "clustered_shakespeare";
48+
49+
QueryClusteredTable.queryClusteredTable(projectId, datasetName, tableName);
50+
assertThat(bout.toString()).contains("Query clustered table performed successfully.");
51+
}
52+
}

0 commit comments

Comments
 (0)