Authorize a BigQuery Dataset
Stay organized with collections
Save and categorize content based on your preferences.
Authorize a BigQuery Dataset
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page provides code samples in Java and Ruby demonstrating how to authorize a BigQuery dataset to grant access to another dataset.\u003c/p\u003e\n"],["\u003cp\u003eThe Java code sample shows how to update a source dataset's Access Control List (ACL) to include access from a user dataset, granting the user dataset access.\u003c/p\u003e\n"],["\u003cp\u003eThe Ruby code sample demonstrates adding a user dataset as an authorized dataset in a source dataset by modifying the source dataset's access rules, allowing target types of \u003ccode\u003eVIEWS\u003c/code\u003e to be used.\u003c/p\u003e\n"],["\u003cp\u003eBoth code samples require setting up Application Default Credentials for authentication, as detailed in the provided documentation links.\u003c/p\u003e\n"],["\u003cp\u003eThe page directs users to the Google Cloud sample browser to find more code samples related to other Google Cloud Products.\u003c/p\u003e\n"]]],[],null,["Authorize a BigQuery Dataset\n\nCode sample \n\nJava\n\n\nBefore trying this sample, follow the Java setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Java API\nreference documentation](/java/docs/reference/google-cloud-bigquery/latest/overview).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Dataset.html;\n import com.google.cloud.bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.DatasetId.html;\n import com.google.common.collect.ImmutableList;\n import java.util.ArrayList;\n import java.util.List;\n\n public class AuthorizeDataset {\n\n public static void main(String[] args) {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"PROJECT_ID\";\n String sourceDatasetName = \"BIGQUERY_SOURCE_DATASET_NAME\";\n String userDatasetName = \"BIGQUERY_USER_DATASET_NAME\";\n authorizeDataset(\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.DatasetId.html.of(projectId, sourceDatasetName), https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.DatasetId.html.of(projectId, userDatasetName));\n }\n\n // This method will update sourceDataset's ACL with userDataset's ACL\n public static void authorizeDataset(https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.DatasetId.html sourceDatasetId, https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.DatasetId.html userDatasetId) {\n try {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests.\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html bigquery = https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryOptions.html.getDefaultInstance().getService();\n\n // Get both source and user dataset's references\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Dataset.html sourceDataset = bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html#com_google_cloud_bigquery_BigQuery_getDataset_com_google_cloud_bigquery_DatasetId_com_google_cloud_bigquery_BigQuery_DatasetOption____(sourceDatasetId);\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Dataset.html userDataset = bigquery.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuery.html#com_google_cloud_bigquery_BigQuery_getDataset_com_google_cloud_bigquery_DatasetId_com_google_cloud_bigquery_BigQuery_DatasetOption____(userDatasetId);\n\n // Get the source dataset's ACL\n List\u003cAcl\u003e sourceDatasetAcl = new ArrayList\u003c\u003e(sourceDataset.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.DatasetInfo.html#com_google_cloud_bigquery_DatasetInfo_getAcl__());\n\n // Add the user dataset's DatasetAccessEntry object to the existing sourceDatasetAcl\n List\u003cString\u003e targetTypes = ImmutableList.of(\"VIEWS\");\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.html.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.DatasetAclEntity.html userDatasetAclEntity =\n new https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.html.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.DatasetAclEntity.html(userDatasetId, targetTypes);\n sourceDatasetAcl.add(https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Acl.html.of(userDatasetAclEntity));\n\n // update the source dataset with user dataset's ACL\n https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Dataset.html updatedSourceDataset =\n sourceDataset.https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Dataset.html#com_google_cloud_bigquery_Dataset_toBuilder__().setAcl(sourceDatasetAcl).build().update();\n\n System.out.printf(\n \"Dataset %s updated with the added authorization\\n\", updatedSourceDataset.getDatasetId());\n\n } catch (https://cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryException.html e) {\n System.out.println(\"Dataset Authorization failed due to error: \\n\" + e);\n }\n }\n }\n\nRuby\n\n\nBefore trying this sample, follow the Ruby setup instructions in the\n[BigQuery quickstart using\nclient libraries](/bigquery/docs/quickstarts/quickstart-client-libraries).\n\n\nFor more information, see the\n[BigQuery Ruby API\nreference documentation](https://googleapis.dev/ruby/google-cloud-bigquery/latest/Google/Cloud/Bigquery.html).\n\n\nTo authenticate to BigQuery, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for client libraries](/bigquery/docs/authentication#client-libs).\n\n require \"google/cloud/bigquery\"\n\n ##\n # This is a snippet for showcasing how to authorize a dataset.\n #\n # Note: Only views target types are supported for now.\n #\n # @param source_project_id [String] The ID of the source Google Cloud project.\n # @param source_database_id [String] The ID of the source database.\n # @param user_project_id [String] The ID of the user Google Cloud project.\n # @param user_database_id [String] The ID of the user database.\n # @param target_types [Array\u003cString\u003e] List of target types for authorization.\n #\n def authorized_dataset source_project_id:, source_database_id:, user_project_id:, user_database_id:, target_types:\n # Initialize client and get source dataset's references\n source_bigquery = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery-storage/latest/Google-Cloud-Bigquery.html.https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery.html project_id: source_project_id\n source_dataset = source_bigquery.dataset source_database_id\n\n # Initialize client and get user dataset's references\n user_bigquery = Google::Cloud::https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery-storage/latest/Google-Cloud-Bigquery.html.https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery.html project_id: user_project_id\n user_dataset = user_bigquery.dataset user_database_id\n\n # Add the user dataset's DatasetAccessEntry object to the existing source dataset rules\n source_dataset.access do |access|\n access.https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Dataset-Access.html user_dataset.https://cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Dataset.html(target_types: target_types)\n end\n\n puts \"Dataset #{user_dataset.dataset_id} added as authorized dataset in dataset #{source_dataset.dataset_id}\"\n end\n\nWhat's next\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=bigquery)."]]