管理資料集

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

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

資料集建立後,您無法變更現有資料集的名稱,也無法重新定位資料集。

必要的角色

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

刪除資料集

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

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

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

所需權限

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

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

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

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

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

  1. 將 BigQuery 資料表中的資料匯出至 Cloud Storage bucket。

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

  2. 將匯出 Cloud Storage bucket 中的資料複製或移至您在目的地位置建立的新 bucket。舉例來說,如果您要將資料從 US 多區域移至 asia-northeast1 東京區域,則應將資料轉移至在東京建立的 bucket。如要瞭解如何轉移 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 控制台刪除資料集時,系統會自動刪除資料集中的資料表和檢視表,包括當中的資料。不過,如果使用其他方法,則必須先清空資料集,或指定對應的旗標、參數或關鍵字,強制移除資料集內容。

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

如果嘗試刪除不存在的資料集,除非您指定忽略遺失資料集的選項 (例如 GoogleSQL 中的 IF EXISTS 或 bq 指令列工具中的 -f (--force) 旗標),否則作業會失敗並顯示 Not found 錯誤。

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

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

控制台

  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;

    IF EXISTS 子句可避免在資料集不存在時發生錯誤。如果沒有 IF EXISTS,嘗試刪除不存在的資料集會傳回 Not found 錯誤。

    根據預設,DROP SCHEMA 只會刪除空白資料集。 如要刪除資料集及其所有內容,請使用 CASCADE 關鍵字:

    DROP SCHEMA IF EXISTS mydataset CASCADE;

  3. 按一下「執行」。

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

bq

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

執行指令後,系統會要求確認。您可以使用 -f (或 --force) 旗標略過確認,並在資料集不存在時忽略 Not found 錯誤。如果沒有 -f 旗標,嘗試刪除不存在的資料集會失敗,並顯示 Not found 錯誤。

如要刪除預設專案以外的資料表,請按照下列格式,在資料集名稱中加入專案 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,即可刪除其中的資料表。如果指定的資料集不存在,方法會傳回 404 Not Found 錯誤。

C#

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

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

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

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

使用 pip install google-cloud-bigquery-datatransfer 安裝 BigQuery 資料移轉 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 資料移轉 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 資料移轉 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 資料移轉 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 資料移轉 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 資料移轉 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 資料移轉 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 定價」一文。

後續步驟