Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit a7a196b

Browse files
docs(sample): Added AuthorizeDataset Sample (#1909)
* Added AuthorizeDataset sample * Added AuthorizeDataset IT * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent b23fc36 commit a7a196b

3 files changed

Lines changed: 159 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-bigquery/tree
118118
| Auth Snippets | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/AuthSnippets.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthSnippets.java) |
119119
| Auth User Flow | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java) |
120120
| Auth User Query | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java) |
121+
| Authorize Dataset | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/AuthorizeDataset.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthorizeDataset.java) |
121122
| Authorized View Tutorial | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/AuthorizedViewTutorial.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/AuthorizedViewTutorial.java) |
122123
| Browse Table | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/BrowseTable.java) |
123124
| Cancel Job | [source code](https://github.com/googleapis/java-bigquery/blob/main/samples/snippets/src/main/java/com/example/bigquery/CancelJob.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-bigquery&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/bigquery/CancelJob.java) |
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2022 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_authorized_dataset]
20+
import com.google.cloud.bigquery.Acl;
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryException;
23+
import com.google.cloud.bigquery.BigQueryOptions;
24+
import com.google.cloud.bigquery.Dataset;
25+
import com.google.cloud.bigquery.DatasetId;
26+
import com.google.common.collect.ImmutableList;
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
30+
public class AuthorizeDataset {
31+
32+
public static void main(String[] args) {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "PROJECT_ID";
35+
String sourceDatasetName = "BIGQUERY_SOURCE_DATASET_NAME";
36+
String userDatasetName = "BIGQUERY_USER_DATASET_NAME";
37+
authorizeDataset(
38+
DatasetId.of(projectId, sourceDatasetName), DatasetId.of(projectId, userDatasetName));
39+
}
40+
41+
// This method will update userDataset's ACL with sourceDataset's ACL
42+
public static void authorizeDataset(DatasetId sourceDatasetId, DatasetId userDatasetId) {
43+
try {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests.
46+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
47+
48+
// Get both source and user dataset's references
49+
Dataset sourceDataset = bigquery.getDataset(sourceDatasetId);
50+
Dataset userDataset = bigquery.getDataset(userDatasetId);
51+
52+
// Get the source dataset's ACL
53+
List<Acl> sourceDatasetAcl = new ArrayList<>(sourceDataset.getAcl());
54+
55+
// Add the user dataset's DatasetAccessEntry object to the existing sourceDatasetAcl
56+
List<String> targetTypes = ImmutableList.of("VIEWS");
57+
Acl.DatasetAclEntity userDatasetAclEntity =
58+
new Acl.DatasetAclEntity(userDatasetId, targetTypes);
59+
sourceDatasetAcl.add(Acl.of(userDatasetAclEntity));
60+
61+
// update the user dataset with source dataset's ACL
62+
Dataset updatedUserDataset =
63+
userDataset.toBuilder().setAcl(sourceDatasetAcl).build().update();
64+
65+
System.out.printf(
66+
"Dataset %s updated with the added authorization\n", updatedUserDataset.getDatasetId());
67+
68+
} catch (BigQueryException e) {
69+
System.out.println("Dataset Authorization failed due to error: \n" + e);
70+
}
71+
}
72+
}
73+
// [END bigquery_authorized_dataset]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2022 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.DatasetId;
23+
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.PrintStream;
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
public class AuthorizeDatasetIT {
34+
private final Logger log = Logger.getLogger(this.getClass().getName());
35+
private String userDatasetName;
36+
private String srcDatasetName;
37+
private ByteArrayOutputStream bout;
38+
private PrintStream out;
39+
private PrintStream originalPrintStream;
40+
private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT");
41+
private DatasetId sourceDatasetId;
42+
private DatasetId userDatasetId;
43+
44+
private static void requireEnvVar(String varName) {
45+
assertNotNull(
46+
"Environment variable " + varName + " is required to perform these tests.",
47+
System.getenv(varName));
48+
}
49+
50+
@BeforeClass
51+
public static void checkRequirements() {
52+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
53+
}
54+
55+
@Before
56+
public void setUp() {
57+
userDatasetName = RemoteBigQueryHelper.generateDatasetName();
58+
srcDatasetName = RemoteBigQueryHelper.generateDatasetName();
59+
bout = new ByteArrayOutputStream();
60+
out = new PrintStream(bout);
61+
originalPrintStream = System.out;
62+
System.setOut(out);
63+
CreateDataset.createDataset(userDatasetName);
64+
CreateDataset.createDataset(srcDatasetName);
65+
userDatasetId = DatasetId.of(GOOGLE_CLOUD_PROJECT, userDatasetName);
66+
sourceDatasetId = DatasetId.of(GOOGLE_CLOUD_PROJECT, srcDatasetName);
67+
}
68+
69+
@After
70+
public void tearDown() {
71+
// Clean up
72+
DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, userDatasetName);
73+
DeleteDataset.deleteDataset(GOOGLE_CLOUD_PROJECT, srcDatasetName);
74+
// restores print statements in the original method
75+
System.out.flush();
76+
System.setOut(originalPrintStream);
77+
log.log(Level.INFO, "\n" + bout.toString());
78+
}
79+
80+
@Test
81+
public void testCreateDataset() {
82+
AuthorizeDataset.authorizeDataset(sourceDatasetId, userDatasetId);
83+
assertThat(bout.toString()).contains(userDatasetId + " updated with the added authorization");
84+
}
85+
}

0 commit comments

Comments
 (0)