Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions bigquery/cloud-client/snippets/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
Copyright 2025 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.bigquery</groupId>
<artifactId>cloud-client-snippets</artifactId>
<packaging>jar</packaging>
<name>Google Cloud BigQuery Cloud Client Snippets</name>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.2.0</version>
</parent>

<properties>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.32.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.4.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

// [START bigquery_view_table_or_view_access_policy]

import com.google.cloud.Policy;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.TableId;

public class GetTableOrViewAccessPolicy {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
// Project, dataset and resource (table or view) from which to get the access policy.
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String resourceName = "MY_RESOURCE_NAME";
getTableOrViewAccessPolicy(projectId, datasetName, resourceName);
}

public static void getTableOrViewAccessPolicy(
String projectId, String datasetName, String resourceName) {
try {
// Initialize client that will be used to send requests. This client only needs
// to be created once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

// Create table identity given the projectId, the datasetName and the resourceName.
TableId tableId = TableId.of(projectId, datasetName, resourceName);

// Get the table IAM policy.
Policy policy = bigquery.getIamPolicy(tableId);

// Show policy details.
// Find more information about the Policy Class here:
// https://cloud.google.com/java/docs/reference/google-cloud-core/latest/com.google.cloud.Policy
System.out.println(
"IAM policy info of resource \"" + resourceName + "\" retrieved succesfully");
System.out.println();
System.out.println("IAM policy info: " + policy.toString());
} catch (BigQueryException e) {
System.out.println("IAM policy info not retrieved. \n" + e.toString());
}
}
}
// [END bigquery_view_table_or_view_access_policy]
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

// [START bigquery_grant_access_to_table_or_view]
import com.google.cloud.Identity;
import com.google.cloud.Policy;
import com.google.cloud.Role;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.TableId;

public class GrantAccessToTableOrView {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
// Project, dataset and resource (table or view) from which to get the access policy.
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String resourceName = "MY_TABLE_NAME";
// Role to add to the policy access
Role role = Role.of("roles/bigquery.dataViewer");
// Identity to add to the policy access
Identity identity = Identity.user("[email protected]");
grantAccessToTableOrView(projectId, datasetName, resourceName, role, identity);
}

public static void grantAccessToTableOrView(
String projectId, String datasetName, String resourceName, Role role, Identity identity) {
try {
// Initialize client that will be used to send requests. This client only needs
// to be created once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

// Create table identity given the projectId, the datasetName and the resourceName.
TableId tableId = TableId.of(projectId, datasetName, resourceName);

// Add new user identity to current IAM policy.
Policy policy = bigquery.getIamPolicy(tableId);
policy = policy.toBuilder().addIdentity(role, identity).build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: insert line breaks for each method call after toBuilder() for better readability.


// Update the IAM policy by setting the new one.
bigquery.setIamPolicy(tableId, policy);

System.out.println("IAM policy of resource \"" + resourceName + "\" updated successfully");
} catch (BigQueryException e) {
System.out.println("IAM policy was not updated. \n" + e.toString());
}
}
}
// [END bigquery_grant_access_to_table_or_view]
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.bigquery;

// [START bigquery_revoke_access_to_table_or_view]
import com.google.cloud.Identity;
import com.google.cloud.Policy;
import com.google.cloud.Role;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.TableId;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class RevokeAccessToTableOrView {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
// Project, dataset and resource (table or view) from which to get the access policy
String projectId = "MY_PROJECT_ID";
String datasetName = "MY_DATASET_NAME";
String resourceName = "MY_RESOURCE_NAME";
// Role to remove from the access policy
Role role = Role.of("roles/bigquery.dataViewer");
// Identity to remove from the access policy
Identity user = Identity.user("[email protected]");
revokeAccessToTableOrView(projectId, datasetName, resourceName, role, user);
}

public static void revokeAccessToTableOrView(
String projectId, String datasetName, String resourceName, Role role, Identity identity) {
try {
// Initialize client that will be used to send requests. This client only needs
// to be created once, and can be reused for multiple requests.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

// Create table identity given the projectId, the datasetName and the resourceName.
TableId tableId = TableId.of(projectId, datasetName, resourceName);

// Remove either identities or roles, or both from bindings and replace it in
// the current IAM policy.
Policy policy = bigquery.getIamPolicy(tableId);
// Create a copy of a inmutable map.
Map<Role, Set<Identity>> bindings = new HashMap<>(policy.getBindings());

// Remove all identities with a specific role.
bindings.remove(role);
// Update bindings.
policy = policy.toBuilder().setBindings(bindings).build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: see previous about putting successive method calls onto separate lines.


// Remove one identity in all the existing roles.
for (Role roleKey : bindings.keySet()) {
if (bindings.get(roleKey).contains(identity)) {
// Create a copy of a inmutable set if the identity is present in the role.
Set<Identity> identities = new HashSet<>(bindings.get(roleKey));
// Remove identity.
identities.remove(identity);
bindings.put(roleKey, identities);
if (bindings.get(roleKey).isEmpty()) {
// Remove the role if it has no identities.
bindings.remove(roleKey);
}
}
}
// Update bindings.
policy = policy.toBuilder().setBindings(bindings).build();

// Update the IAM policy by setting the new one.
bigquery.setIamPolicy(tableId, policy);

System.out.println("IAM policy of resource \"" + resourceName + "\" updated successfully");
} catch (BigQueryException e) {
System.out.println("IAM policy was not updated. \n" + e.toString());
}
}
}
// [END bigquery_revoke_access_to_table_or_view]
Loading