Skip to content

Commit cf36b82

Browse files
author
Praful Makani
authored
docs(samples): add query materialized view (#978)
* docs(samples): add query materialized view * docs(samples): address feedback
1 parent aeecac9 commit cf36b82

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 com.google.cloud.bigquery.BigQuery;
20+
import com.google.cloud.bigquery.BigQueryException;
21+
import com.google.cloud.bigquery.BigQueryOptions;
22+
import com.google.cloud.bigquery.QueryJobConfiguration;
23+
import com.google.cloud.bigquery.TableResult;
24+
25+
public class QueryMaterializedView {
26+
27+
public static void main(String[] args) throws InterruptedException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String datasetName = "MY_DATASET_NAME";
30+
String materializedViewName = "MY_MATERIALIZED_VIEW_NAME";
31+
String query = String.format("SELECT * FROM %s.%s", datasetName, materializedViewName);
32+
queryMaterializedView(query);
33+
}
34+
35+
public static void queryMaterializedView(String query) throws InterruptedException {
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+
TableResult results = bigquery.query(QueryJobConfiguration.of(query));
42+
results
43+
.iterateAll()
44+
.forEach(row -> row.forEach(val -> System.out.printf("%s\n", val.toString())));
45+
46+
System.out.println("Query performed successfully.");
47+
} catch (BigQueryException e) {
48+
System.out.println("Query was not performed. \n" + e.toString());
49+
}
50+
}
51+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 QueryMaterializedViewIT {
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_QUERY_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 testQueryMaterializedView() throws InterruptedException {
99+
String query =
100+
String.format("SELECT * FROM %s.%s", BIGQUERY_DATASET_NAME, materializedViewName);
101+
QueryMaterializedView.queryMaterializedView(query);
102+
assertThat(bout.toString()).contains("Query performed successfully.");
103+
}
104+
}

0 commit comments

Comments
 (0)