Skip to content

Commit 37c0632

Browse files
author
Praful Makani
authored
docs(samples): add alter materialized view (#979)
1 parent 890098a commit 37c0632

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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_alter_materialized_view]
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.MaterializedViewDefinition;
24+
import com.google.cloud.bigquery.Table;
25+
import com.google.cloud.bigquery.TableId;
26+
27+
// Sample to alter materialized view
28+
public class AlterMaterializedView {
29+
30+
public static void main(String[] args) {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String datasetName = "MY_DATASET_NAME";
33+
String materializedViewName = "MY_MATERIALIZED_VIEW_NAME";
34+
alterMaterializedView(datasetName, materializedViewName);
35+
}
36+
37+
public static void alterMaterializedView(String datasetName, String materializedViewName) {
38+
try {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests.
41+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
42+
43+
TableId tableId = TableId.of(datasetName, materializedViewName);
44+
45+
// Get existing materialized view
46+
Table table = bigquery.getTable(tableId);
47+
MaterializedViewDefinition materializedViewDefinition = table.getDefinition();
48+
// Alter materialized view
49+
materializedViewDefinition
50+
.toBuilder()
51+
.setEnableRefresh(true)
52+
.setRefreshIntervalMs(1000L)
53+
.build();
54+
table.toBuilder().setDefinition(materializedViewDefinition).build().update();
55+
System.out.println("Materialized view altered successfully");
56+
} catch (BigQueryException e) {
57+
System.out.println("Materialized view was not altered. \n" + e.toString());
58+
}
59+
}
60+
}
61+
// [END bigquery_alter_materialized_view]
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 java.util.logging.Level;
29+
import java.util.logging.Logger;
30+
import org.junit.After;
31+
import org.junit.Before;
32+
import org.junit.BeforeClass;
33+
import org.junit.Test;
34+
35+
public class AlterMaterializedViewIT {
36+
37+
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
38+
private final Logger log = Logger.getLogger(this.getClass().getName());
39+
private String tableName;
40+
private String materializedViewName;
41+
private ByteArrayOutputStream bout;
42+
private PrintStream out;
43+
private PrintStream originalPrintStream;
44+
45+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
46+
47+
private static String requireEnvVar(String varName) {
48+
String value = System.getenv(varName);
49+
assertNotNull(
50+
"Environment variable " + varName + " is required to perform these tests.",
51+
System.getenv(varName));
52+
return value;
53+
}
54+
55+
@BeforeClass
56+
public static void checkRequirements() {
57+
requireEnvVar("BIGQUERY_DATASET_NAME");
58+
}
59+
60+
@Before
61+
public void setUp() {
62+
bout = new ByteArrayOutputStream();
63+
out = new PrintStream(bout);
64+
originalPrintStream = System.out;
65+
System.setOut(out);
66+
67+
tableName = "MY_TABLE_NAME_TEST_" + ID;
68+
materializedViewName = "MY_ALTER_MATERIALIZED_VIEW_NAME_TEST_" + ID;
69+
70+
Schema schema =
71+
Schema.of(
72+
Field.of("timestampField", StandardSQLTypeName.TIMESTAMP),
73+
Field.of("stringField", StandardSQLTypeName.STRING),
74+
Field.of("booleanField", StandardSQLTypeName.BOOL));
75+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema);
76+
String query =
77+
String.format(
78+
"SELECT MAX(TimestampField) AS TimestampField, StringField, "
79+
+ "MAX(BooleanField) AS BooleanField "
80+
+ "FROM %s.%s GROUP BY StringField",
81+
BIGQUERY_DATASET_NAME, tableName);
82+
CreateMaterializedView.createMaterializedView(
83+
BIGQUERY_DATASET_NAME, materializedViewName, query);
84+
}
85+
86+
@After
87+
public void tearDown() {
88+
// Clean up
89+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, materializedViewName);
90+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
91+
// restores print statements in the original method
92+
System.out.flush();
93+
System.setOut(originalPrintStream);
94+
log.log(Level.INFO, bout.toString());
95+
}
96+
97+
@Test
98+
public void testAlterMaterializedView() {
99+
AlterMaterializedView.alterMaterializedView(BIGQUERY_DATASET_NAME, materializedViewName);
100+
assertThat(bout.toString()).contains("Materialized view altered successfully");
101+
}
102+
}

0 commit comments

Comments
 (0)