8000 feat(bigquery): add dataset samples to cloud-client (2/3) (#10035) · rebeccaellis/java-docs-samples@93ac167 · GitHub
[go: up one dir, main page]

Skip to content

Commit 93ac167

Browse files
feat(bigquery): add dataset samples to cloud-client (2/3) (GoogleCloudPlatform#10035)
* feat(bigquery): add dataset samples to cloud-client * use Util helpers * add helper method grantAccessToDataset * address comments
1 parent d2f8053 commit 93ac167

File tree

7 files changed

+500
-0
lines changed

7 files changed

+500
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2025 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_view_dataset_access_policy]
20+
21+
import com.google.cloud.bigquery.Acl;
22+
import com.google.cloud.bigquery.BigQuery;
23+
import com.google.cloud.bigquery.BigQueryException;
24+
import com.google.cloud.bigquery.BigQueryOptions;
25+
import com.google.cloud.bigquery.Dataset;
26+
import com.google.cloud.bigquery.DatasetId;
27+
import java.util.List;
28+
29+
public class GetDatasetAccessPolicy {
30+
31+
public static void main(String[] args) {
32+
// TODO(developer): Replace these variables before running the sample.
33+
// Project and dataset from which to get the access policy.
34+
String projectId = "MY_PROJECT_ID";
35+
String datasetName = "MY_DATASET_NAME";
36+
getDatasetAccessPolicy(projectId, datasetName);
37+
}
38+
39+
public static void getDatasetAccessPolicy(String projectId, String datasetName) {
40+
try {
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests.
43+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
44+
45+
// Create datasetId with the projectId and the datasetName.
46+
DatasetId datasetId = DatasetId.of(projectId, datasetName);
47+
Dataset dataset = bigquery.getDataset(datasetId);
48+
49+
// Show ACL details.
50+
// Find more information about ACL and the Acl Class here:
51+
// https://cloud.google.com/storage/docs/access-control/lists
52+
// https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl
53+
List<Acl> acls = dataset.getAcl();
54+
System.out.println("ACLs in dataset \"" + dataset.getDatasetId().getDataset() + "\":");
55+
System.out.println(acls.toString());
56+
for (Acl acl : acls) {
57+
System.out.println();
58+
System.out.println("Role: " + acl.getRole());
59+
System.out.println("Entity: " + acl.getEntity());
60+
}
61+
} catch (BigQueryException e) {
62+
System.out.println("ACLs info not retrieved. \n" + e.toString());
63+
}
64+
}
65+
}
66+
// [END bigquery_view_dataset_access_policy]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2025 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_grant_access_to_dataset]
20+
import com.google.cloud.bigquery.Acl;
21+
import com.google.cloud.bigquery.Acl.Entity;
22+
import com.google.cloud.bigquery.Acl.Group;
23+
import com.google.cloud.bigquery.Acl.Role;
24+
import com.google.cloud.bigquery.BigQuery;
25+
import com.google.cloud.bigquery.BigQueryException;
26+
import com.google.cloud.bigquery.BigQueryOptions;
27+
import com.google.cloud.bigquery.Dataset;
28+
import com.google.cloud.bigquery.DatasetId;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
public class GrantAccessToDataset {
33+
34+
public static void main(String[] args) {
35+
// TODO(developer): Replace these variables before running the sample.
36+
// Project and dataset from which to get the access policy
37+
String projectId = "MY_PROJECT_ID";
38+
String datasetName = "MY_DATASET_NAME";
39+
// Group to add to the ACL
40+
String entityEmail = "group-to-add@example.com";
41+
42+
grantAccessToDataset(projectId, datasetName, entityEmail);
43+
}
44+
45+
public static void grantAccessToDataset(
46+
String projectId, String datasetName, String entityEmail) {
47+
try {
48+
// Initialize client that will be used to send requests. This client only needs to be created
49+
// once, and can be reused for multiple requests.
50+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
51+
52+
// Create datasetId with the projectId and the datasetName.
53+
DatasetId datasetId = DatasetId.of(projectId, datasetName);
54+
Dataset dataset = bigquery.getDataset(datasetId);
55+
56+
// Create a new Entity with the corresponding type and email
57+
// "user-or-group-to-add@example.com"
58+
// For more information on the types of Entities available see:
59+
// https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.Entity
60+
// and
61+
// https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.Entity.Type
62+
Entity entity = new Group(entityEmail);
63+
64+
// Create a new ACL granting the READER role to the group with the entity email
65+
// "user-or-group-to-add@example.com"
66+
// For more information on the types of ACLs available see:
67+
// https://cloud.google.com/storage/docs/access-control/lists
68+
Acl newEntry = Acl.of(entity, Role.READER);
69+
70+
// Get a copy of the ACLs list from the dataset and append the new entry.
71+
List<Acl> acls = new ArrayList<>(dataset.getAcl());
72+
acls.add(newEntry);
73+
74+
// Update the ACLs by setting the new list.
75+
Dataset updatedDataset = bigquery.update(dataset.toBuilder().setAcl(acls).build());
76+
System.out.println(
77+
"ACLs of dataset \""
78+
+ updatedDataset.getDatasetId().getDataset()
79+
+ "\" updated successfully");
80+
} catch (BigQueryException e) {
81+
System.out.println("ACLs were not updated \n" + e.toString());
82+
}
83+
}
84+
}
85+
// [END bigquery_grant_access_to_dataset]
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2025 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_revoke_dataset_access]
20+
21+
import com.google.cloud.bigquery.Acl;
22+
import com.google.cloud.bigquery.Acl.Entity;
23+
import com.google.cloud.bigquery.Acl.Group;
24+
import com.google.cloud.bigquery.BigQuery;
25+
import com.google.cloud.bigquery.BigQueryException;
26+
import com.google.cloud.bigquery.BigQueryOptions;
27+
import com.google.cloud.bigquery.Dataset;
28+
import com.google.cloud.bigquery.DatasetId;
29+
import java.util.List;
30+
31+
public class RevokeDatasetAccess {
32+
33+
public static void main(String[] args) {
34+
// TODO(developer): Replace these variables before running the sample.
35+
// Project and dataset from which to get the access policy.
36+
String projectId = "MY_PROJECT_ID";
37+
String datasetName = "MY_DATASET_NAME";
38+
// Group to remove from the ACL
39+
String entityEmail = "group-to-remove@example.com";
40+
41+
revokeDatasetAccess(projectId, datasetName, entityEmail);
42+
}
43+
44+
public static void revokeDatasetAccess(String projectId, String datasetName, String entityEmail) {
45+
try {
46+
// Initialize client that will be used to send requests. This client only needs
47+
// to be created once, and can be reused for multiple requests.
48+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
49+
50+
// Create datasetId with the projectId and the datasetName.
51+
DatasetId datasetId = DatasetId.of(projectId, datasetName);
52+
Dataset dataset = bigquery.getDataset(datasetId);
53+
54+
// Create a new Entity with the corresponding type and email
55+
// "user-or-group-to-remove@example.com"
56+
// For more information on the types of Entities available see:
57+
// https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.Entity
58+
// and
59+
// https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.Entity.Type
60+
Entity entity = new Group(entityEmail);
61+
62+
// To revoke access to a dataset, remove elements from the Acl list.
63+
// Find more information about ACL and the Acl Class here:
64+
// https://cloud.google.com/storage/docs/access-control/lists
65+
// https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl
66+
// Remove the entity from the ACLs list.
67+
List<Acl> acls =
68+
dataset.getAcl().stream().filter(acl -> !acl.getEntity().equals(entity)).toList();
69+
70+
// Update the ACLs by setting the new list.
71+
bigquery.update(dataset.toBuilder().setAcl(acls).build());
72+
System.out.println("ACLs of \"" + datasetName + "\" updated successfully");
73+
} catch (BigQueryException e) {
74+
System.out.println("ACLs were not updated \n" + e.toString());
75+
}
76+
}
77+
}
78+
// [END bigquery_revoke_dataset_access]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2025 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.testing.RemoteBigQueryHelper;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
public class GetDatasetAccessPolicyIT {
33+
34+
private final Logger log = Logger.getLogger(this.getClass().getName());
35+
private String datasetName;
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
private PrintStream originalPrintStream;
39+
40+
private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT");
41+
42+
private static void requireEnvVar(String varName) {
43+
assertNotNull(
44+
"Environment variable " + varName + " is required to perform these tests.",
45+
System.getenv(varName));
46+
}
47+
48+
@BeforeClass
49+
public static void checkRequirements() {
50+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
51+
}
52+
53+
@Before
54+
public void setUp() throws Exception {
55+
bout = new ByteArrayOutputStream();
56+
out = new PrintStream(bout);
57+
originalPrintStream = System.out;
58+
System.setOut(out);
59+
datasetName = RemoteBigQueryHelper.generateDatasetName();
60+
61+
// Create a dataset in order to get its ACL policy.
62+
Util.setUpTest_createDataset(GOOGLE_CLOUD_PROJECT, datasetName);
63+
}
64+
65+
@After
66+
public void tearDown() {
67+
// Clean up.
68+
Util.tearDownTest_deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName);
69+
70+
// Restores print statements to the original output stream.
71+
System.out.flush();
72+
System.setOut(originalPrintStream);
73+
log.log(Level.INFO, "\n" + bout.toString());
74+
}
75+
76+
@Test
77+
public void getDatasetAccessPolicy() {
78+
// Get dataset ACLs
79+
GetDatasetAccessPolicy.getDatasetAccessPolicy(GOOGLE_CLOUD_PROJECT, datasetName);
80+
assertThat(bout.toString()).contains("ACLs in dataset \"" + datasetName);
81+
}
82+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2025 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.testing.RemoteBigQueryHelper;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
32+
public class GrantAccessToDatasetIT {
33+
34+
private final Logger log = Logger.getLogger(this.getClass().getName());
35+
private String datasetName;
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
private PrintStream originalPrintStream;
39+
40+
private static final String GOOGLE_CLOUD_PROJECT = System.getenv("GOOGLE_CLOUD_PROJECT");
41+
42+
private static void requireEnvVar(String varName) {
43+
assertNotNull(
44+
"Environment variable " + varName + " is required to perform these tests.",
45+
System.getenv(varName));
46+
}
47+
48+
@BeforeClass
49+
public static void checkRequirements() {
50+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
51+
}
52+
53+
@Before
54+
public void setUp() throws Exception {
55+
bout = new ByteArrayOutputStream();
56+
out = new PrintStream(bout);
57+
originalPrintStream = System.out;
58+
System.setOut(out);
59+
datasetName = RemoteBigQueryHelper.generateDatasetName();
60+
61+
// Create a dataset in order to modify its ACL policy.
62+
Util.setUpTest_createDataset(GOOGLE_CLOUD_PROJECT, datasetName);
63+
}
64+
65+
@After
66+
public void tearDown() {
67+
// Clean up.
68+
Util.tearDownTest_deleteDataset(GOOGLE_CLOUD_PROJECT, datasetName);
69+
70+
// Restores print statements to the original output stream.
71+
System.out.flush();
72+
System.setOut(originalPrintStream);
73+
log.log(Level.INFO, "\n" + bout.toString());
74+
}
75+
76+
@Test
77+
public void grantAccessToDataset() {
78+
String groupEmail = "cloud-developer-relations@google.com";
79+
// Modify dataset's ACL
80+
GrantAccessToDataset.grantAccessToDataset(GOOGLE_CLOUD_PROJECT, datasetName, groupEmail);
81+
assertThat(bout.toString())
82+
.contains("ACLs of dataset \"" + datasetName + "\" updated successfully");
83+
}
84+
}

0 commit comments

Comments
 (0)
0