Skip to content

Commit 38b7ab2

Browse files
author
Praful Makani
authored
docs(samples): add get view (#508)
1 parent 8221e0c commit 38b7ab2

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-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_get_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.Table;
24+
import com.google.cloud.bigquery.TableId;
25+
26+
// Sample to get a view
27+
public class GetView {
28+
29+
public static void runGetView() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String datasetName = "MY_DATASET_NAME";
32+
String viewName = "MY_VIEW_NAME";
33+
getView(datasetName, viewName);
34+
}
35+
36+
public static void getView(String datasetName, String viewName) {
37+
try {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests.
40+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
41+
42+
TableId tableId = TableId.of(datasetName, viewName);
43+
Table view = bigquery.getTable(tableId);
44+
System.out.println("View retrieved successfully" + view.getDescription());
45+
} catch (BigQueryException e) {
46+
System.out.println("View not retrieved. \n" + e.toString());
47+
}
48+
}
49+
}
50+
// [END bigquery_get_view]
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 GetViewIT {
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+
// Create a new table to be deleted
62+
tableName = "MY_TABLE_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
63+
viewName = "MY_VIEW_NAME_TEST_" + UUID.randomUUID().toString().substring(0, 8);
64+
65+
Schema schema =
66+
Schema.of(
67+
Field.of("timestampField", StandardSQLTypeName.TIMESTAMP),
68+
Field.of("stringField", StandardSQLTypeName.STRING),
69+
Field.of("booleanField", StandardSQLTypeName.BOOL));
70+
CreateTable.createTable(BIGQUERY_DATASET_NAME, tableName, schema);
71+
72+
// Create a new view to be deleted
73+
String query =
74+
String.format(
75+
"SELECT timestampField, stringField, booleanField FROM %s.%s",
76+
BIGQUERY_DATASET_NAME, tableName);
77+
CreateView.createView(BIGQUERY_DATASET_NAME, viewName, query);
78+
79+
bout = new ByteArrayOutputStream();
80+
out = new PrintStream(bout);
81+
System.setOut(out);
82+
}
83+
84+
@After
85+
public void tearDown() {
86+
// Clean up
87+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, viewName);
88+
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, tableName);
89+
System.setOut(null);
90+
}
91+
92+
@Test
93+
public void testGetView() {
94+
GetView.getView(BIGQUERY_DATASET_NAME, viewName);
95+
assertThat(bout.toString()).contains("View retrieved successfully");
96+
}
97+
}

0 commit comments

Comments
 (0)