Memberi otorisasi Set Data BigQuery

Memberi otorisasi Set Data BigQuery

Contoh kode

Java

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Java di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Java API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.

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 sourceDataset's ACL with userDataset'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 source dataset with user dataset's ACL
      Dataset updatedSourceDataset =
          sourceDataset.toBuilder().setAcl(sourceDatasetAcl).build().update();

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

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

Ruby

Sebelum mencoba contoh ini, ikuti petunjuk penyiapan Ruby di Panduan memulai BigQuery menggunakan library klien. Untuk mengetahui informasi selengkapnya, lihat Dokumentasi referensi BigQuery Ruby API.

Untuk melakukan autentikasi ke BigQuery, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk library klien.

require "google/cloud/bigquery"

##
# This is a snippet for showcasing how to authorize a dataset.
#
# Note: Only views target types are supported for now.
#
# @param source_project_id [String] The ID of the source Google Cloud project.
# @param source_database_id [String] The ID of the source database.
# @param user_project_id [String] The ID of the user Google Cloud project.
# @param user_database_id [String] The ID of the user database.
# @param target_types [Array<String>] List of target types for authorization.
#
def authorized_dataset source_project_id:, source_database_id:, user_project_id:, user_database_id:, target_types:
  # Initialize client and get source dataset's references
  source_bigquery = Google::Cloud::Bigquery.new project_id: source_project_id
  source_dataset  = source_bigquery.dataset source_database_id

  # Initialize client and get user dataset's references
  user_bigquery = Google::Cloud::Bigquery.new project_id: user_project_id
  user_dataset  = user_bigquery.dataset user_database_id

  # Add the user dataset's DatasetAccessEntry object to the existing source dataset rules
  source_dataset.access do |access|
    access.add_reader_dataset user_dataset.build_access_entry(target_types: target_types)
  end

  puts "Dataset #{user_dataset.dataset_id} added as authorized dataset in dataset #{source_dataset.dataset_id}"
end

Langkah selanjutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat browser contoh Google Cloud.