Skip to content
This repository was archived by the owner on Aug 30, 2026. It is now read-only.
Merged
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
Next Next commit
Added AuthorizeDataset sample
  • Loading branch information
prash-mi committed Mar 9, 2022
commit f763b1f09570ab33195b1463503499c1f6bd309f
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2022 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_authorized_dataset]
import com.google.cloud.bigquery.Acl;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetId;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;

public class AuthorizeDataset {

public static void main(String[] args) {
// TODO(developer): Replace these variables before running the sample.
String projectId = "PROJECT_ID";
String sourceDatasetName = "BIGQUERY_SOURCE_DATASET_NAME";
String userDatasetName = "BIGQUERY_USER_DATASET_NAME";
authorizeDataset(
DatasetId.of(projectId, sourceDatasetName), DatasetId.of(projectId, userDatasetName));
}

// This method will update userDataset's ACL with sourceDataset's ACL
public static void authorizeDataset(DatasetId sourceDatasetId, DatasetId userDatasetId) {
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();

// Get both source and user dataset's references
Dataset sourceDataset = bigquery.getDataset(sourceDatasetId);
Dataset userDataset = bigquery.getDataset(userDatasetId);

// Get the source dataset's ACL
List<Acl> sourceDatasetAcl = new ArrayList<>(sourceDataset.getAcl());

// Add the user dataset's DatasetAccessEntry object to the existing sourceDatasetAcl
List<String> targetTypes = ImmutableList.of("VIEWS");
Acl.DatasetAclEntity userDatasetAclEntity =
new Acl.DatasetAclEntity(userDatasetId, targetTypes);
sourceDatasetAcl.add(Acl.of(userDatasetAclEntity));

// update the user dataset with source dataset's ACL
Dataset updatedUserDataset =
userDataset.toBuilder().setAcl(sourceDatasetAcl).build().update();

System.out.printf(
"Dataset %s updated with the added authorization\n", updatedUserDataset.getDatasetId());

} catch (BigQueryException e) {
System.out.println("Dataset Authorization failed due to error: \n" + e);
}
}
}
// [END bigquery_authorized_dataset]