管理資料集

本文說明如何在 BigQuery 中於其他位置重建資料集、保護資料集、刪除資料集,以及從已刪除的資料集還原資料表。如要瞭解如何還原 (或取消刪除) 已刪除的資料集,請參閱「還原已刪除的資料集」。

身為 BigQuery 管理員,您可以整理分析師使用的資料表檢視區塊,並控管存取權。如要進一步瞭解資料集,請參閱「資料集簡介」。

現有資料集的名稱無法變更,資料集建立後也無法搬遷。

必要的角色

本節說明管理資料集所需的角色和權限。如果來源或目的地資料集與您用來複製的專案位於同一專案,則您不需要該資料集的額外權限或角色。

刪除資料集

授予這些角色,即可刪除資料集。

如要取得刪除資料集所需的權限,請要求管理員授予您專案的「BigQuery 資料擁有者 」(roles/bigquery.dataOwner) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

這個預先定義的角色具備刪除資料集所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:

所需權限

如要刪除資料集,您必須具備下列權限:

  • bigquery.datasets.delete 專案
  • bigquery.tables.delete 專案

您或許還可透過自訂角色或其他預先定義的角色取得這些權限。

在其他位置重新建立資料集

如要手動將資料集移至其他位置,請按照下列步驟操作:

  1. 從 BigQuery 資料表匯出資料至 Cloud Storage 值區。

    從 BigQuery 中匯出資料並不需要付費,但是在 Cloud Storage 儲存匯出的資料則會產生費用。BigQuery 匯出作業會受到擷取工作的相關限制。

  2. 從匯出 Cloud Storage 值區中,將資料複製或移動至您在目的地位置建立的新值區。舉例來說,如果您要將資料從US多地區移到asia-northeast1東京地區,則必須把資料移轉到您在東京建立的值區。如要瞭解如何轉移 Cloud Storage 物件,請參閱 Cloud Storage 說明文件中的複製、重新命名及移動物件一文。

    在不同地區之間轉移資料將導致 Cloud Storage 產生網路輸出費用

  3. 在新位置建立新的 BigQuery 資料集,然後將資料從 Cloud Storage bucket 載入新資料集。

    將資料載入 BigQuery 無須支付費用,但將資料儲存於 Cloud Storage 則須支付費用,直到您刪除資料或值區為止。載入資料之後,將資料儲存至 BigQuery 亦須支付相關費用。將資料載入 BigQuery 時,必須遵守載入工作的相關限制。

您也可以使用 Managed Service for Apache Airflow,以程式輔助方式移動及複製大型資料集。

如要進一步瞭解如何使用 Cloud Storage 儲存及移動大型資料集,請參閱搭配大數據使用 Cloud Storage

安全共用資料集

如要在 BigQuery 中控管資料集存取權,請參閱「控管資料集存取權」。如要瞭解資料加密,請參閱「靜態加密」。

刪除資料集

使用 Cloud de Confiance console 刪除資料集時,系統會自動刪除資料集中的資料表和檢視表 (包括其中的資料)。不過,使用任何其他方法時,您必須先清空資料集,或指定相應的旗標、參數或關鍵字,強制移除資料集內容。

如果您嘗試刪除非空白資料集,但未提供適當的標記或參數,作業就會失敗並顯示以下錯誤:Dataset project:dataset is still in use

刪除資料集時,系統會為資料集刪除作業建立一筆稽核記錄項目。不會為資料集內刪除的每個資料表建立個別的記錄檔項目。

如要刪除資料集,請選取下列其中一個選項:

控制台

  1. 前往「BigQuery」頁面。

    前往「BigQuery」

  2. 點選左側窗格中的 「Explorer」

    醒目顯示的「Explorer」窗格按鈕。

  3. 在「Explorer」窗格中展開專案,按一下「Datasets」,然後按一下資料集。

  4. 在詳細資料窗格中,按一下 「刪除」

  5. 在「Delete dataset」(刪除資料集) 對話方塊中,在欄位輸入 delete,然後按一下「Delete」(刪除)

SQL

如要刪除資料集,請使用 DROP SCHEMA DDL 陳述式

以下範例會刪除名為 mydataset 的資料集:

  1. 前往 Cloud de Confiance 控制台的「BigQuery」頁面。

    前往「BigQuery」

  2. 在查詢編輯器中輸入下列陳述式:

    DROP SCHEMA IF EXISTS mydataset;

    根據預設,這項功能只能刪除空白資料集。 如要刪除資料集和所有內容,請使用 CASCADE 關鍵字:

    DROP SCHEMA IF EXISTS mydataset CASCADE;

  3. 按一下「執行」

如要進一步瞭解如何執行查詢,請參閱「執行互動式查詢」。

bq

使用 bq rm 指令,並加上 --dataset-d 旗標 (選用)。如果資料集中包含資料表,您就必須使用 -r 標記來移除資料集中的所有資料表。如果您使用 -r 旗標,則可以省略 --dataset-d 旗標。

執行指令後,系統會要求確認。您可以使用 -f 標記略過確認程序。

如要刪除非預設專案中的資料表,請使用下列格式將專案 ID 新增至資料集名稱:PROJECT_ID:DATASET

bq rm -r -f -d PROJECT_ID:DATASET

更改下列內容:

  • PROJECT_ID:專案 ID
  • DATASET:要刪除的資料集名稱

範例:

輸入下列指令,從預設專案中移除名為 mydataset 的資料集及其所有資料表。這個指令會使用 -d 旗標。

bq rm -r -d mydataset

當系統提示時,請輸入 y,然後按下 Enter 鍵。

輸入下列指令,即可從 myotherproject 中移除 mydataset 及其中的所有資料表。這個指令不會使用選用的 -d 旗標。-f 標記可用來略過確認程序。

bq rm -r -f myotherproject:mydataset

您可以使用 bq ls 指令確認資料集是否已刪除。

API

呼叫 datasets.delete 方法來刪除資料集,然後將 deleteContents 參數設為 true 來刪除當中的資料表。

C#

下列程式碼範例會刪除空白資料集。

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 C# 設定說明操作。詳情請參閱 BigQuery C# API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證作業」。

執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

使用 pip install google-cloud-bigquery-datatransfer 安裝 BigQuery Data Transfer API 的 Python 用戶端。然後建立移轉設定,複製資料集。

using Google.Cloud.BigQuery.V2;
using System;

public class BigQueryDeleteDataset
{
    public void DeleteDataset(
        string projectId = "your-project-id",
        string datasetId = "your_empty_dataset"
    )
    {
        BigQueryClient client = BigQueryClient.Create(projectId);
        // Delete a dataset that does not contain any tables
        client.DeleteDataset(datasetId: datasetId);
        Console.WriteLine($"Dataset {datasetId} deleted.");
    }
}

以下程式碼範例會刪除資料集及其所有內容:

// Copyright(c) 2018 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.
//

using Google.Cloud.BigQuery.V2;
using System;

public class BigQueryDeleteDatasetAndContents
{
    public void DeleteDatasetAndContents(
        string projectId = "your-project-id",
        string datasetId = "your_dataset_with_tables"
    )
    {
        BigQueryClient client = BigQueryClient.Create(projectId);
        // Use the DeleteDatasetOptions to delete a dataset and its contents
        client.DeleteDataset(
            datasetId: datasetId,
            options: new DeleteDatasetOptions() { DeleteContents = true }
        );
        Console.WriteLine($"Dataset {datasetId} and contents deleted.");
    }
}

Go

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Go 設定說明操作。詳情請參閱 BigQuery Go API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證作業」。

執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

使用 pip install google-cloud-bigquery-datatransfer 安裝 BigQuery Data Transfer API 的 Python 用戶端。然後建立移轉設定,複製資料集。
import (
	"context"
	"fmt"

	"cloud.google.com/go/bigquery"
)

// deleteDataset demonstrates the deletion of an empty dataset.
func deleteDataset(projectID, datasetID string) error {
	// projectID := "my-project-id"
	// datasetID := "mydataset"
	ctx := context.Background()

	client, err := bigquery.NewClient(ctx, projectID)
	if err != nil {
		return fmt.Errorf("bigquery.NewClient: %v", err)
	}
	defer client.Close()

	// To recursively delete a dataset and contents, use DeleteWithContents.
	if err := client.Dataset(datasetID).Delete(ctx); err != nil {
		return fmt.Errorf("Delete: %v", err)
	}
	return nil
}

Java

下列程式碼範例會刪除空白資料集。

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Java 設定說明操作。詳情請參閱 BigQuery Java API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證作業」。

執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

使用 pip install google-cloud-bigquery-datatransfer 安裝 BigQuery Data Transfer API 的 Python 用戶端。然後建立移轉設定,複製資料集。
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.DatasetId;

public class DeleteDataset {

  public static void runDeleteDataset() {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "MY_PROJECT_ID";
    String datasetName = "MY_DATASET_NAME";
    deleteDataset(projectId, datasetName);
  }

  public static void deleteDataset(String projectId, String datasetName) {
    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();

      DatasetId datasetId = DatasetId.of(projectId, datasetName);
      boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
      if (success) {
        System.out.println("Dataset deleted successfully");
      } else {
        System.out.println("Dataset was not found");
      }
    } catch (BigQueryException e) {
      System.out.println("Dataset was not deleted. \n" + e.toString());
    }
  }
}

以下程式碼範例會刪除資料集及其所有內容:

/*
 * Copyright 2020 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;

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.DatasetId;

// Sample to delete dataset with contents.
public class DeleteDatasetAndContents {

  public static void main(String[] args) {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "MY_PROJECT_ID";
    String datasetName = "MY_DATASET_NAME";
    deleteDatasetAndContents(projectId, datasetName);
  }

  public static void deleteDatasetAndContents(String projectId, String datasetName) {
    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();

      DatasetId datasetId = DatasetId.of(projectId, datasetName);
      // Use the force parameter to delete a dataset and its contents
      boolean success = bigquery.delete(datasetId, BigQuery.DatasetDeleteOption.deleteContents());
      if (success) {
        System.out.println("Dataset deleted with contents successfully");
      } else {
        System.out.println("Dataset was not found");
      }
    } catch (BigQueryException e) {
      System.out.println("Dataset was not deleted with contents. \n" + e.toString());
    }
  }
}

Node.js

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Node.js 設定說明操作。詳情請參閱 BigQuery Node.js API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證作業」。

執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

使用 pip install google-cloud-bigquery-datatransfer 安裝 BigQuery Data Transfer API 的 Python 用戶端。然後建立移轉設定,複製資料集。
// Import the Google Cloud client library
const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();

async function deleteDataset() {
  // Deletes a dataset named "my_dataset".

  /**
   * TODO(developer): Uncomment the following lines before running the sample.
   */
  // const datasetId = 'my_dataset';

  // Create a reference to the existing dataset
  const dataset = bigquery.dataset(datasetId);

  // Delete the dataset and its contents
  await dataset.delete({force: true});
  console.log(`Dataset ${dataset.id} deleted.`);
}

PHP

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 PHP 設定說明操作。詳情請參閱 BigQuery PHP API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證作業」。

執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

使用 pip install google-cloud-bigquery-datatransfer 安裝 BigQuery Data Transfer API 的 Python 用戶端。然後建立移轉設定,複製資料集。
use Google\Cloud\BigQuery\BigQueryClient;

/** Uncomment and populate these variables in your code */
// $projectId = 'The Google project ID';
// $datasetId = 'The BigQuery dataset ID';

$bigQuery = new BigQueryClient([
    'projectId' => $projectId,
]);
$dataset = $bigQuery->dataset($datasetId);
$table = $dataset->delete();
printf('Deleted dataset %s' . PHP_EOL, $datasetId);

Python

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Python 設定說明操作。詳情請參閱 BigQuery Python API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證作業」。

執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

使用 pip install google-cloud-bigquery-datatransfer 安裝 BigQuery Data Transfer API 的 Python 用戶端。然後建立移轉設定,複製資料集。

from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

# TODO(developer): Set model_id to the ID of the model to fetch.
# dataset_id = 'your-project.your_dataset'

# Use the delete_contents parameter to delete a dataset and its contents.
# Use the not_found_ok parameter to not receive an error if the dataset has already been deleted.
client.delete_dataset(
    dataset_id, delete_contents=True, not_found_ok=True
)  # Make an API request.

print("Deleted dataset '{}'.".format(dataset_id))

Ruby

下列程式碼範例會刪除空白資料集。

在試用這個範例之前,請先按照「使用用戶端程式庫的 BigQuery 快速入門導覽課程」中的 Ruby 設定說明操作。詳情請參閱 BigQuery Ruby API 參考說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「設定用戶端程式庫的驗證作業」。

執行程式碼範例前,請將 GOOGLE_CLOUD_UNIVERSE_DOMAIN 環境變數設為 s3nsapis.fr

使用 pip install google-cloud-bigquery-datatransfer 安裝 BigQuery Data Transfer API 的 Python 用戶端。然後建立移轉設定,複製資料集。
require "google/cloud/bigquery"

def delete_dataset dataset_id = "my_empty_dataset"
  bigquery = Google::Cloud::Bigquery.new

  # Delete a dataset that does not contain any tables
  dataset = bigquery.dataset dataset_id
  dataset.delete
  puts "Dataset #{dataset_id} deleted."
end

以下程式碼範例會刪除資料集及其所有內容:

# Copyright 2020 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.
require "google/cloud/bigquery"

def delete_dataset_and_contents dataset_id = "my_dataset_with_tables"
  bigquery = Google::Cloud::Bigquery.new

  # Use the force parameter to delete a dataset and its contents
  dataset = bigquery.dataset dataset_id
  dataset.delete force: true
  puts "Dataset #{dataset_id} and contents deleted."
end

從已刪除的資料集還原資料表

您可以還原已刪除資料集中的資料表,但必須在資料集的時間回溯期內。如要還原整個資料集,請參閱「還原已刪除的資料集」。

  1. 建立名稱和位置與原始資料集相同的資料集。
  2. 使用自 Epoch 紀元以來的毫秒格式 (例如 1418864998000),選擇原始資料集刪除前的時間戳記。
  3. originaldataset.table1 資料表在 1418864998000 時間點的狀態複製到新資料集:

    bq cp originaldataset.table1@1418864998000 mydataset.mytable
    

    如要找出已刪除資料集中的非空白資料表名稱,請在時間回溯期內查詢資料集的 INFORMATION_SCHEMA.TABLE_STORAGE 檢視區塊

還原已刪除的資料集

如要瞭解如何還原 (或取消刪除) 已刪除的資料集,請參閱「還原已刪除的資料集」。

配額

如要瞭解複製配額,請參閱「複製作業」。 您可以在 INFORMATION_SCHEMA 中查看複製工作的用量。如要瞭解如何查詢 INFORMATION_SCHEMA.JOBS 檢視畫面,請參閱JOBS 檢視畫面

定價

如要瞭解複製資料集的定價資訊,請參閱「資料複製定價」。

BigQuery 會傳送壓縮資料以跨地區進行複製,因此計費的資料量可能小於資料集的實際大小。詳情請參閱「BigQuery 定價」一文。

後續步驟