Skip to content

Commit 29272bb

Browse files
author
Praful Makani
authored
docs(samples): add update table require partition filter (#624)
1 parent a889800 commit 29272bb

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_update_table_require_partition_filter]
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.Table;
24+
25+
// Sample to update require partition filter on a table.
26+
public class UpdateTableRequirePartitionFilter {
27+
28+
public static void runUpdateTableRequirePartitionFilter() {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String datasetName = "MY_DATASET_NAME";
31+
String tableName = "MY_TABLE_NAME";
32+
updateTableRequirePartitionFilter(datasetName, tableName);
33+
}
34+
35+
public static void updateTableRequirePartitionFilter(String datasetName, String tableName) {
36+
try {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
40+
41+
Table table = bigquery.getTable(datasetName, tableName);
42+
table.toBuilder().setRequirePartitionFilter(true).build().update();
43+
44+
System.out.println("Table require partition filter updated successfully");
45+
} catch (BigQueryException e) {
46+
System.out.println("Table require partition filter was not updated \n" + e.toString());
47+
}
48+
}
49+
}
50+
// [END bigquery_update_table_require_partition_filter]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 com.google.cloud.bigquery.Field;
23+
import com.google.cloud.bigquery.Schema;
24+
import com.google.cloud.bigquery.StandardSQLTypeName;
25+
import java.io.ByteArrayOutputStream;
26+
import java.io.PrintStream;
27+
import java.util.UUID;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class UpdateTableRequirePartitionFilterIT {
34+
35+
private String tableName;
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
40+
41+
private static String requireEnvVar(String varName) {
42+
String value = System.getenv(varName);
43+
assertNotNull(
44+
"Environment variable " + varName + " is required to perform these tests.",
45+
System.getenv(varName));
46+
return value;
47+
}
48+
49+
@BeforeClass
50+
public static void checkRequirements() {
51+
requireEnvVar("BIGQUERY_DATASET_NAME");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
bout = new ByteArrayOutputStream();
57+
out = new PrintStream(bout);
58+
System.setOut(out);
59+
60+
tableName =
61+
"UPDATE_TABLE_REQUIRE_PARTITION_FILTER_TEST_"
62+
+ UUID.randomUUID().toString().substring(0, 8);
63+
// Create a table in order to modify its partition filter.
64+
Schema schema =
65+
Schema.of(
66+
Field.of("name", StandardSQLTypeName.STRING),
67+
Field.of("post_abbr", StandardSQLTypeName.STRING),
68+
Field.of("date", StandardSQLTypeName.DATE));
69+
CreatePartitionedTable.createPartitionedTable(BIGQUERY_DATASET_NAME, tableName, schema);
70+
71+
bout = new ByteArrayOutputStream();
72+
out = new PrintStream(bout);
73+
System.setOut(out);
74+
}
75+
76+
@After
77+
public void tearDown() {
78+
// Clean up
79+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
80+
System.setOut(null);
81+
}
82+
83+
@Test
84+
public void testUpdateTableRequirePartitionFilter() {
85+
UpdateTableRequirePartitionFilter.updateTableRequirePartitionFilter(
86+
BIGQUERY_DATASET_NAME, tableName);
87+
assertThat(bout.toString()).contains("Table require partition filter updated successfully");
88+
}
89+
}

0 commit comments

Comments
 (0)