Skip to content

Commit 055ebc0

Browse files
author
Praful Makani
authored
docs(samples): add query partitioned table (#597)
1 parent 918c8bd commit 055ebc0

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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_partitioned_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.QueryParameterValue;
25+
import com.google.cloud.bigquery.TableResult;
26+
27+
// Sample to run query on partitioned table.
28+
public class QueryPartitionedTable {
29+
30+
public static void runQueryPartitionedTable() {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String datasetName = "MY_DATASET_NAME";
33+
String tableName = "MY_TABLE_NAME";
34+
String query =
35+
String.format(
36+
"SELECT * FROM `%s.%s` WHERE date BETWEEN @start_date AND @end_date",
37+
datasetName, tableName);
38+
queryPartitionedTable(query);
39+
}
40+
41+
public static void queryPartitionedTable(String query) {
42+
try {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
46+
47+
QueryJobConfiguration queryConfig =
48+
QueryJobConfiguration.newBuilder(query)
49+
.addNamedParameter("start_date", QueryParameterValue.date("1800-01-01"))
50+
.addNamedParameter("end_date", QueryParameterValue.date("1899-12-31"))
51+
.build();
52+
53+
TableResult results = bigquery.query(queryConfig);
54+
55+
results
56+
.iterateAll()
57+
.forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
58+
59+
System.out.println("Query partitioned table performed successfully.");
60+
} catch (BigQueryException | InterruptedException e) {
61+
System.out.println("Query not performed \n" + e.toString());
62+
}
63+
}
64+
}
65+
// [END bigquery_query_partitioned_table]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.util.UUID;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class QueryPartitionedTableIT {
31+
32+
private String tableName;
33+
private ByteArrayOutputStream bout;
34+
private PrintStream out;
35+
36+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
37+
38+
private static String requireEnvVar(String varName) {
39+
String value = System.getenv(varName);
40+
assertNotNull(
41+
"Environment variable " + varName + " is required to perform these tests.",
42+
System.getenv(varName));
43+
return value;
44+
}
45+
46+
@BeforeClass
47+
public static void checkRequirements() {
48+
requireEnvVar("BIGQUERY_DATASET_NAME");
49+
}
50+
51+
@Before
52+
public void setUp() throws Exception {
53+
bout = new ByteArrayOutputStream();
54+
out = new PrintStream(bout);
55+
System.setOut(out);
56+
57+
// Create a test table
58+
tableName = "LOAD_PARTITIONED_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
59+
String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states-by-date-no-header.csv";
60+
LoadPartitionedTable.loadPartitionedTable(BIGQUERY_DATASET_NAME, tableName, sourceUri);
61+
62+
bout = new ByteArrayOutputStream();
63+
out = new PrintStream(bout);
64+
System.setOut(out);
65+
}
66+
67+
@After
68+
public void tearDown() {
69+
// Clean up
70+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
71+
System.setOut(null);
72+
}
73+
74+
@Test
75+
public void testQueryPartitionedTable() {
76+
String query =
77+
String.format(
78+
"SELECT * FROM `%s.%s` WHERE date BETWEEN @start_date AND @end_date",
79+
BIGQUERY_DATASET_NAME, tableName);
80+
QueryPartitionedTable.queryPartitionedTable(query);
81+
assertThat(bout.toString()).contains("Query partitioned table performed successfully.");
82+
}
83+
}

0 commit comments

Comments
 (0)