Skip to content

Commit 155e688

Browse files
author
Praful Makani
authored
docs(samples): add create materialized view (#977)
1 parent 0cfc98b commit 155e688

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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_create_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.TableId;
25+
import com.google.cloud.bigquery.TableInfo;
26+
27+
// Sample to create materialized view
28+
public class CreateMaterializedView {
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 tableName = "MY_TABLE_NAME";
34+
String materializedViewName = "MY_MATERIALIZED_VIEW_NAME";
35+
String query =
36+
String.format(
37+
"SELECT MAX(TimestampField) AS TimestampField, StringField, "
38+
+ "MAX(BooleanField) AS BooleanField "
39+
+ "FROM %s.%s GROUP BY StringField",
40+
datasetName, tableName);
41+
createMaterializedView(datasetName, materializedViewName, query);
42+
}
43+
44+
public static void createMaterializedView(
45+
String datasetName, String materializedViewName, String query) {
46+
try {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests.
49+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
50+
51+
TableId tableId = TableId.of(datasetName, materializedViewName);
52+
53+
MaterializedViewDefinition materializedViewDefinition =
54+
MaterializedViewDefinition.newBuilder(query).build();
55+
56+
bigquery.create(TableInfo.of(tableId, materializedViewDefinition));
57+
System.out.println("Materialized view created successfully");
58+
} catch (BigQueryException e) {
59+
System.out.println("Materialized view was not created. \n" + e.toString());
60+
}
61+
}
62+
}
63+
// [END bigquery_create_materialized_view]
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 CreateMaterializedViewIT {
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_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+
}
77+
78+
@After
79+
public void tearDown() {
80+
// Clean up
81+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, materializedViewName);
82+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
83+
// restores print statements in the original method
84+
System.out.flush();
85+
System.setOut(originalPrintStream);
86+
log.log(Level.INFO, bout.toString());
87+
}
88+
89+
@Test
90+
public void testCreateMaterializedView() {
91+
String query =
92+
String.format(
93+
"SELECT MAX(TimestampField) AS TimestampField, StringField, "
94+
+ "MAX(BooleanField) AS BooleanField "
95+
+ "FROM %s.%s GROUP BY StringField",
96+
BIGQUERY_DATASET_NAME, tableName);
97+
CreateMaterializedView.createMaterializedView(
98+
BIGQUERY_DATASET_NAME, materializedViewName, query);
99+
assertThat(bout.toString()).contains("Materialized view created successfully");
100+
}
101+
}

0 commit comments

Comments
 (0)