Skip to content

Commit 09cf7f1

Browse files
author
Praful Makani
authored
docs(samples): add create view (#497)
1 parent 62e0020 commit 09cf7f1

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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_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.TableId;
24+
import com.google.cloud.bigquery.TableInfo;
25+
import com.google.cloud.bigquery.ViewDefinition;
26+
27+
// Sample to create a view
28+
public class CreateView {
29+
30+
public static void runCreateView() {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String datasetName = "MY_DATASET_NAME";
33+
String tableName = "MY_TABLE_NAME";
34+
String viewName = "MY_VIEW_NAME";
35+
String query =
36+
String.format(
37+
"SELECT TimestampField, StringField, BooleanField FROM %s.%s", datasetName, tableName);
38+
createView(datasetName, viewName, query);
39+
}
40+
41+
public static void createView(String datasetName, String viewName, 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+
TableId tableId = TableId.of(datasetName, viewName);
48+
49+
ViewDefinition viewDefinition =
50+
ViewDefinition.newBuilder(query).setUseLegacySql(false).build();
51+
52+
bigquery.create(TableInfo.of(tableId, viewDefinition));
53+
System.out.println("View created successfully");
54+
} catch (BigQueryException e) {
55+
System.out.println("View was not created. \n" + e.toString());
56+
}
57+
}
58+
}
59+
// [END bigquery_create_view]
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 CreateViewIT {
34+
35+
private String tableName;
36+
private String viewName;
37+
private ByteArrayOutputStream bout;
38+
private PrintStream out;
39+
40+
private static final String BIGQUERY_DATASET_NAME = requireEnvVar("BIGQUERY_DATASET_NAME");
41+
42+
private static String requireEnvVar(String varName) {
43+
String value = System.getenv(varName);
44+
assertNotNull(
45+
"Environment variable " + varName + " is required to perform these tests.",
46+
System.getenv(varName));
47+
return value;
48+
}
49+
50+
@BeforeClass
51+
public static void checkRequirements() {
52+
requireEnvVar("BIGQUERY_DATASET_NAME");
53+
}
54+
55+
@Before
56+
public void setUp() {
57+
bout = new ByteArrayOutputStream();
58+
out = new PrintStream(bout);
59+
System.setOut(out);
60+
61+
tableName = "MY_TABLE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
62+
viewName = "MY_VIEW_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
63+
64+
Schema schema =
65+
Schema.of(
66+
Field.of("timestampField", StandardSQLTypeName.TIMESTAMP),
67+
Field.of("stringField", StandardSQLTypeName.STRING),
68+
Field.of("booleanField", StandardSQLTypeName.BOOL));
69+
CreateTable.createTable(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, viewName);
80+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
81+
System.setOut(null);
82+
}
83+
84+
@Test
85+
public void testCreateView() {
86+
String query =
87+
String.format(
88+
"SELECT timestampField, stringField, booleanField FROM %s.%s",
89+
BIGQUERY_DATASET_NAME, tableName);
90+
CreateView.createView(BIGQUERY_DATASET_NAME, viewName, query);
91+
assertThat(bout.toString()).contains("View created successfully");
92+
}
93+
}

0 commit comments

Comments
 (0)