Skip to content

Commit 91cdf72

Browse files
author
Praful Makani
authored
docs(samples): add authorized view tutorial (googleapis#637)
1 parent c02a36c commit 91cdf72

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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.Acl;
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.Dataset;
24+
import com.google.cloud.bigquery.DatasetInfo;
25+
import com.google.cloud.bigquery.QueryJobConfiguration;
26+
import com.google.cloud.bigquery.Table;
27+
import com.google.cloud.bigquery.TableId;
28+
import com.google.cloud.bigquery.TableInfo;
29+
import com.google.cloud.bigquery.ViewDefinition;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
33+
// Sample of authorized view tutorial.
34+
public class AuthorizedViewTutorial {
35+
36+
public static void runAuthorizedViewTutorial() {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String projectId = "MY_PROJECT_ID";
39+
String sourceDatasetId = "MY_SOURCE_DATASET";
40+
String sourceTableId = "MY_SOURCE_TABLE";
41+
String sharedDatasetId = "SHARED_VIEWS";
42+
String sharedViewId = "MY_VIEW";
43+
authorizedViewTutorial(
44+
projectId, sourceDatasetId, sourceTableId, sharedDatasetId, sharedViewId);
45+
}
46+
47+
public static void authorizedViewTutorial(
48+
String projectId,
49+
String sourceDatasetId,
50+
String sourceTableId,
51+
String sharedDatasetId,
52+
String sharedViewId) {
53+
try {
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests.
56+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
57+
// [START bigquery_authorized_view_tutorial]
58+
// [START bigquery_avt_create_source_dataset]
59+
// Create a source dataset to store your table.
60+
Dataset sourceDataset = bigquery.create(DatasetInfo.of(sourceDatasetId));
61+
// [END bigquery_avt_create_source_dataset]
62+
63+
// [START bigquery_avt_create_source_table]
64+
// Populate a source table
65+
String tableQuery =
66+
"SELECT commit, author, committer, repo_name"
67+
+ " FROM `bigquery-public-data.github_repos.commits`"
68+
+ " LIMIT 1000";
69+
QueryJobConfiguration queryConfig =
70+
QueryJobConfiguration.newBuilder(tableQuery)
71+
.setDestinationTable(TableId.of(sourceDatasetId, sourceTableId))
72+
.build();
73+
bigquery.query(queryConfig);
74+
// [END bigquery_avt_create_source_table]
75+
76+
// [START bigquery_avt_create_shared_dataset]
77+
// Create a separate dataset to store your view
78+
Dataset sharedDataset = bigquery.create(DatasetInfo.of(sharedDatasetId));
79+
// [END bigquery_avt_create_shared_dataset]
80+
81+
// [START bigquery_avt_create_view]
82+
// Create the view in the new dataset
83+
String viewQuery =
84+
String.format(
85+
"SELECT commit, author.name as author, committer.name as committer, repo_name FROM %s.%s.%s",
86+
projectId, sourceDatasetId, sourceTableId);
87+
88+
ViewDefinition viewDefinition = ViewDefinition.of(viewQuery);
89+
90+
Table view =
91+
bigquery.create(TableInfo.of(TableId.of(sharedDatasetId, sharedViewId), viewDefinition));
92+
// [END bigquery_avt_create_view]
93+
94+
// [START bigquery_avt_shared_dataset_access]
95+
// Assign access controls to the dataset containing the view
96+
List<Acl> viewAcl = new ArrayList<>(sharedDataset.getAcl());
97+
viewAcl.add(Acl.of(new Acl.Group("[email protected]"), Acl.Role.READER));
98+
sharedDataset.toBuilder().setAcl(viewAcl).build().update();
99+
// [END bigquery_avt_shared_dataset_access]
100+
101+
// [START bigquery_avt_source_dataset_access]
102+
// Authorize the view to access the source dataset
103+
List<Acl> srcAcl = new ArrayList<>(sourceDataset.getAcl());
104+
srcAcl.add(Acl.of(new Acl.View(view.getTableId())));
105+
sourceDataset.toBuilder().setAcl(srcAcl).build().update();
106+
// [END bigquery_avt_source_dataset_access]
107+
// [END bigquery_authorized_view_tutorial]
108+
System.out.println("Authorized view tutorial successfully");
109+
} catch (BigQueryException | InterruptedException e) {
110+
System.out.println("Authorized view tutorial was not success. \n" + e.toString());
111+
}
112+
}
113+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import java.util.UUID;
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class AuthorizedViewTutorialIT {
31+
32+
private String sourceDatasetId;
33+
private String sourceTableId;
34+
private String sharedDatasetId;
35+
private String sharedViewId;
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
private static final String PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT");
40+
41+
private static String requireEnvVar(String varName) {
42+
String value = System.getenv(varName);
43+
assertNotNull(
44+
"Environment variable " + varName + " is required to perform these tests.",
45+
System.getenv(varName));
46+
return value;
47+
}
48+
49+
@BeforeClass
50+
public static void checkRequirements() {
51+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
52+
}
53+
54+
@Before
55+
public void setUp() {
56+
sourceDatasetId = "SOURCE_DATASET_TEST_" + UUID.randomUUID().toString().substring(0, 8);
57+
sourceTableId = "SOURCE_TABLE_TEST_" + UUID.randomUUID().toString().substring(0, 8);
58+
sharedDatasetId = "SHARED_DATASET_TEST_" + UUID.randomUUID().toString().substring(0, 8);
59+
sharedViewId = "SHARED_VIEW_TEST_" + UUID.randomUUID().toString().substring(0, 8);
60+
61+
bout = new ByteArrayOutputStream();
62+
out = new PrintStream(bout);
63+
System.setOut(out);
64+
}
65+
66+
@After
67+
public void tearDown() {
68+
// Clean up
69+
DeleteDataset.deleteDataset(PROJECT_ID, sourceDatasetId);
70+
DeleteDataset.deleteDataset(PROJECT_ID, sharedDatasetId);
71+
System.setOut(null);
72+
}
73+
74+
@Test
75+
public void testAuthorizedViewTutorial() {
76+
AuthorizedViewTutorial.authorizedViewTutorial(
77+
PROJECT_ID, sourceDatasetId, sourceTableId, sharedDatasetId, sharedViewId);
78+
assertThat(bout.toString()).contains("Authorized view tutorial successfully");
79+
}
80+
}

0 commit comments

Comments
 (0)