管理資料表

這份文件說明如何在 BigQuery 中管理資料表。 您可透過下列方式管理 BigQuery 資料表:

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

如要進一步瞭解如何建立及使用資料表,包括取得資料表資訊、列出資料表及控管資料表資料存取權等,請參閱建立與使用資料表

事前準備

授予身分與存取權管理 (IAM) 角色,讓使用者擁有執行本文中各項工作所需的權限。如要執行工作,必須具備的權限 (如有) 會列在工作的「必要權限」部分。

更新資料表屬性

您可以更新資料表的下列元素:

所需權限

如要取得更新資料表屬性所需的權限,請要求管理員授予您資料表的資料編輯者 (roles/bigquery.dataEditor) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

這個預先定義的角色具備更新表格屬性所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:

所需權限

如要更新資料表屬性,您必須具備下列權限:

  • bigquery.tables.update
  • bigquery.tables.get

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

此外,如果您具備 bigquery.datasets.create 權限,可以更新所建立資料集的資料表屬性。

更新資料表的說明

您可以透過下列方式更新資料表的說明:

  • 使用 Google Cloud 控制台。
  • 使用資料定義語言 (DDL) ALTER TABLE 陳述式。
  • 使用 bq 指令列工具的 bq update 指令。
  • 呼叫 tables.patch API 方法
  • 使用用戶端程式庫。
  • 使用 Gemini in BigQuery 生成說明。

如何更新資料表的說明:

控制台

使用Google Cloud 主控台建立資料表時,無法新增說明。資料表建立完畢之後,您就可以在「Details」(詳細資料) 頁面中新增說明。

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

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

    如果沒有看到左側窗格,請按一下「展開左側窗格」圖示 開啟窗格。

  2. 在「Explorer」窗格中展開專案,點選「Datasets」(資料集),然後選取資料集。

  3. 依序按一下「總覽」>「表格」,然後選取所需表格。

  4. 按一下「詳細資料」分頁標籤,然後點選「編輯詳細資料」

  5. 在「說明」部分新增或編輯說明。

  6. 按一下 [儲存]

SQL

使用 ALTER TABLE SET OPTIONS 陳述式。以下範例會更新名為 mytable 的資料表說明:

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

    前往「BigQuery」

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

    ALTER TABLE mydataset.mytable
      SET OPTIONS (
        description = 'Description of mytable');

  3. 按一下「執行」

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

bq

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 發出含有 --description 旗標的 bq update 指令。如果您要更新非預設專案中的資料表,請使用下列格式將專案 ID 新增至資料集名稱:project_id:dataset

    bq update \
    --description "description" \
    project_id:dataset.table

    更改下列內容:

    • description:置於引號中的資料表說明
    • project_id:專案 ID
    • dataset:含有您要更新資料表的資料集名稱
    • table:要更新的資料表名稱

    範例:

    如要將 mydataset 資料集中的 mytable 資料表說明變更為「Description of mytable」,請輸入下列指令。mydataset 資料集位於預設專案中。

    bq update --description "Description of mytable" mydataset.mytable
    

    如要將 mydataset 資料集中的 mytable 資料表說明變更為「Description of mytable」,請輸入下列指令。mydataset 資料集位於 myotherproject 專案,而非預設專案。

    bq update \
    --description "Description of mytable" \
    myotherproject:mydataset.mytable
    
  3. API

    呼叫 tables.patch 方法,並使用資料表資源中的 description 屬性來更新資料表的說明。由於 tables.update 方法會取代整個資料表資源,因此建議使用 tables.patch 方法。

    Go

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

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

    import (
    	"context"
    	"fmt"
    
    	"cloud.google.com/go/bigquery"
    )
    
    // updateTableDescription demonstrates how to fetch a table's metadata and updates the Description metadata.
    func updateTableDescription(projectID, datasetID, tableID string) error {
    	// projectID := "my-project-id"
    	// datasetID := "mydataset"
    	// tableID := "mytable"
    	ctx := context.Background()
    	client, err := bigquery.NewClient(ctx, projectID)
    	if err != nil {
    		return fmt.Errorf("bigquery.NewClient: %v", err)
    	}
    	defer client.Close()
    
    	tableRef := client.Dataset(datasetID).Table(tableID)
    	meta, err := tableRef.Metadata(ctx)
    	if err != nil {
    		return err
    	}
    	update := bigquery.TableMetadataToUpdate{
    		Description: "Updated description.",
    	}
    	if _, err = tableRef.Update(ctx, update, meta.ETag); err != nil {
    		return err
    	}
    	return nil
    }
    

    Java

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

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

    import com.google.cloud.bigquery.BigQuery;
    import com.google.cloud.bigquery.BigQueryException;
    import com.google.cloud.bigquery.BigQueryOptions;
    import com.google.cloud.bigquery.Table;
    
    public class UpdateTableDescription {
    
      public static void runUpdateTableDescription() {
        // TODO(developer): Replace these variables before running the sample.
        String datasetName = "MY_DATASET_NAME";
        String tableName = "MY_TABLE_NAME";
        String newDescription = "this is the new table description";
        updateTableDescription(datasetName, tableName, newDescription);
      }
    
      public static void updateTableDescription(
          String datasetName, String tableName, String newDescription) {
        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();
    
          Table table = bigquery.getTable(datasetName, tableName);
          bigquery.update(table.toBuilder().setDescription(newDescription).build());
          System.out.println("Table description updated successfully to " + newDescription);
        } catch (BigQueryException e) {
          System.out.println("Table description was not updated \n" + e.toString());
        }
      }
    }

    Python

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

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

    設定 Table.description 屬性並呼叫 Client.update_table(),將更新傳送至 API。
    # from google.cloud import bigquery
    # client = bigquery.Client()
    # project = client.project
    # dataset_ref = bigquery.DatasetReference(project, dataset_id)
    # table_ref = dataset_ref.table('my_table')
    # table = client.get_table(table_ref)  # API request
    
    assert table.description == "Original description."
    table.description = "Updated description."
    
    table = client.update_table(table, ["description"])  # API request
    
    assert table.description == "Updated description."

    Gemini

    您可以使用資料洞察,透過 Gemini in BigQuery 生成資料表說明。資料洞察功能會自動探索、解讀及整理資料。

    如要進一步瞭解資料洞察,包括設定步驟、必要 IAM 角色,以及提升所產生洞察準確度的最佳做法,請參閱「在 BigQuery 中產生資料洞察」。

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

      前往「BigQuery」

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

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

    3. 在「Explorer」窗格中,展開專案和資料集,然後選取資料表。

    4. 在詳細資料面板中,按一下「結構定義」分頁標籤。

    5. 點按「生成」

      Gemini 會生成資料表說明和資料表洞察結果。系統需要幾分鐘的時間才會填入資訊。您可以在表格的「洞察」分頁中查看生成的洞察結果。

    6. 如要編輯及儲存系統產生的表格說明,請按照下列步驟操作:

      1. 按一下「查看資料欄說明」

        系統會顯示目前的資料表說明和生成的說明。

      2. 在「資料表說明」部分,按一下「儲存至詳細資料」

      3. 如要以系統產生的說明取代目前的說明,請按一下「複製建議的說明」

      4. 視需要編輯表格說明,然後按一下「儲存至詳細資料」

        系統會立即更新資料表說明。

      5. 如要關閉「預覽說明」面板,請按一下「關閉」

更新資料表的到期時間

您可以設定資料集層級的預設資料表到期時間,也可以在建立資料表時設定資料表的到期時間。資料表的到期時間通常稱為「存留時間」或 TTL。

資料表過期時,系統會一併刪除資料表及其包含的所有資料。如有需要,您可以在資料集指定的時間旅行視窗內取消刪除過期的資料表,詳情請參閱「還原已刪除的資料表」。

如果您在建立資料表時設定了到期時間,系統將會忽略資料集的資料表預設到期時間。如果您未在資料集層級設定資料表的預設到期時間,也未在建立資料表時設定到期時間,則資料表將永遠不會過期,您必須以手動方式才能刪除

建立資料表後,您可以隨時透過以下方式更新資料表的到期時間:

  • 使用 Google Cloud 控制台。
  • 使用資料定義語言 (DDL) ALTER TABLE 陳述式。
  • 使用 bq 指令列工具的 bq update 指令。
  • 呼叫 tables.patch API 方法
  • 使用用戶端程式庫。

如何更新資料表的到期時間:

控制台

使用Google Cloud 主控台建立資料表時,您無法新增到期時間。建立資料表後,您可以在「Table Details」(資料表詳細資料) 頁面上新增或更新資料表的到期時間。

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

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

  2. 在「Explorer」窗格中展開專案,點選「Datasets」(資料集),然後選取資料集。

  3. 依序按一下「總覽」>「表格」,然後選取所需表格。

  4. 按一下「詳細資料」分頁標籤,然後按一下「編輯詳細資料」

  5. 針對「Expiration time」(到期時間),選取「Specify date」(指定日期)。然後使用日曆小工具選取到期日。

  6. 按一下 [儲存]。已更新的到期時間會顯示在「Table info」(資料表資訊) 區段。

SQL

使用 ALTER TABLE SET OPTIONS 陳述式。以下範例會更新名為 mytable 的資料表到期時間:

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

    前往「BigQuery」

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

    ALTER TABLE mydataset.mytable
      SET OPTIONS (
        -- Sets table expiration to timestamp 2025-02-03 12:34:56
        expiration_timestamp = TIMESTAMP '2025-02-03 12:34:56');

  3. 按一下「執行」

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

bq

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 發出含有 --expiration 旗標的 bq update 指令。如果您要更新非預設專案中的資料表,請使用下列格式將專案 ID 新增至資料集名稱:project_id:dataset

    bq update \
    --expiration integer \
    project_id:dataset.table

    更改下列內容:

    • integer:資料表的預設生命週期 (以秒為單位),最小值是 3600 秒 (1 小時)。到期時間為目前時間加整數值。如果您指定 0,系統就會移除資料表到期時間,讓這個資料表永遠不會過期。沒有到期時間的資料表都必須手動刪除。
    • project_id:您的專案 ID。
    • dataset:含有您要更新資料表的資料集名稱。
    • table:您要更新的資料表名稱。

    範例:

    如要將 mydataset 資料集中的 mytable 資料表到期時間更新為 5 天 (432000 秒),請輸入下列指令。mydataset 資料集位於預設專案中。

    bq update --expiration 432000 mydataset.mytable
    

    如要將 mydataset 資料集中的 mytable 資料表到期時間更新為 5 天 (432000 秒),請輸入下列指令。mydataset 資料集位於 myotherproject 專案,而非預設專案。

    bq update --expiration 432000 myotherproject:mydataset.mytable
    
  3. API

    呼叫 tables.patch 方法並使用資料表資源中的 expirationTime 屬性更新資料表的到期時間 (以毫秒為單位)。由於 tables.update 方法會取代整個資料表資源,因此建議使用 tables.patch 方法。

    Go

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

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

    import (
    	"context"
    	"fmt"
    	"time"
    
    	"cloud.google.com/go/bigquery"
    )
    
    // updateTableExpiration demonstrates setting the table expiration of a table to a specific point in time
    // in the future, at which time it will be deleted.
    func updateTableExpiration(projectID, datasetID, tableID string) error {
    	// projectID := "my-project-id"
    	// datasetID := "mydataset"
    	// tableID := "mytable"
    	ctx := context.Background()
    	client, err := bigquery.NewClient(ctx, projectID)
    	if err != nil {
    		return fmt.Errorf("bigquery.NewClient: %v", err)
    	}
    	defer client.Close()
    
    	tableRef := client.Dataset(datasetID).Table(tableID)
    	meta, err := tableRef.Metadata(ctx)
    	if err != nil {
    		return err
    	}
    	update := bigquery.TableMetadataToUpdate{
    		ExpirationTime: time.Now().Add(time.Duration(5*24) * time.Hour), // table expiration in 5 days.
    	}
    	if _, err = tableRef.Update(ctx, update, meta.ETag); err != nil {
    		return err
    	}
    	return nil
    }
    

    Java

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

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

    import com.google.cloud.bigquery.BigQuery;
    import com.google.cloud.bigquery.BigQueryException;
    import com.google.cloud.bigquery.BigQueryOptions;
    import com.google.cloud.bigquery.Table;
    import java.util.concurrent.TimeUnit;
    
    public class UpdateTableExpiration {
    
      public static void runUpdateTableExpiration() {
        // TODO(developer): Replace these variables before running the sample.
        String datasetName = "MY_DATASET_NAME";
        String tableName = "MY_TABLE_NAME";
        // Update table expiration to one day.
        Long newExpiration = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
        updateTableExpiration(datasetName, tableName, newExpiration);
      }
    
      public static void updateTableExpiration(
          String datasetName, String tableName, Long newExpiration) {
        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();
    
          Table table = bigquery.getTable(datasetName, tableName);
          bigquery.update(table.toBuilder().setExpirationTime(newExpiration).build());
    
          System.out.println("Table expiration updated successfully to " + newExpiration);
        } catch (BigQueryException e) {
          System.out.println("Table expiration was not updated \n" + e.toString());
        }
      }
    }

    Node.js

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

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

    // Import the Google Cloud client library
    const {BigQuery} = require('@google-cloud/bigquery');
    const bigquery = new BigQuery();
    
    async function updateTableExpiration() {
      // Updates a table's expiration.
    
      /**
       * TODO(developer): Uncomment the following lines before running the sample.
       */
      // const datasetId = 'my_dataset', // Existing dataset
      // const tableId = 'my_table', // Existing table
      // const expirationTime = Date.now() + 1000 * 60 * 60 * 24 * 5 // 5 days from current time in ms
    
      // Retreive current table metadata
      const table = bigquery.dataset(datasetId).table(tableId);
      const [metadata] = await table.getMetadata();
    
      // Set new table expiration to 5 days from current time
      metadata.expirationTime = expirationTime.toString();
      const [apiResponse] = await table.setMetadata(metadata);
    
      const newExpirationTime = apiResponse.expirationTime;
      console.log(`${tableId} expiration: ${newExpirationTime}`);
    }

    Python

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

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

    設定 Table.expires 屬性並呼叫 Client.update_table(),將更新傳送至 API。
    # 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
    #
    #     https://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.
    
    import datetime
    
    
    def update_table_expiration(table_id, expiration):
        orig_table_id = table_id
        orig_expiration = expiration
    
        from google.cloud import bigquery
    
        client = bigquery.Client()
    
        # TODO(dev): Change table_id to the full name of the table you want to update.
        table_id = "your-project.your_dataset.your_table_name"
    
        # TODO(dev): Set table to expire for desired days days from now.
        expiration = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(
            days=5
        )
    
        table_id = orig_table_id
        expiration = orig_expiration
    
        table = client.get_table(table_id)  # Make an API request.
        table.expires = expiration
        table = client.update_table(table, ["expires"])  # API request
    
        print(f"Updated {table_id}, expires {table.expires}.")
    

如要更新資料集的預設分區到期時間:

Java

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

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

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 java.util.concurrent.TimeUnit;

// Sample to update partition expiration on a dataset.
public class UpdateDatasetPartitionExpiration {

  public static void main(String[] args) {
    // TODO(developer): Replace these variables before running the sample.
    String datasetName = "MY_DATASET_NAME";
    // Set the default partition expiration (applies to new tables, only) in
    // milliseconds. This example sets the default expiration to 90 days.
    Long newExpiration = TimeUnit.MILLISECONDS.convert(90, TimeUnit.DAYS);
    updateDatasetPartitionExpiration(datasetName, newExpiration);
  }

  public static void updateDatasetPartitionExpiration(String datasetName, Long newExpiration) {
    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();

      Dataset dataset = bigquery.getDataset(datasetName);
      bigquery.update(dataset.toBuilder().setDefaultPartitionExpirationMs(newExpiration).build());
      System.out.println(
          "Dataset default partition expiration updated successfully to " + newExpiration);
    } catch (BigQueryException e) {
      System.out.println("Dataset partition expiration was not updated \n" + e.toString());
    }
  }
}

Python

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

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

# Copyright 2019 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
#
#     https://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.


def update_dataset_default_partition_expiration(dataset_id: str) -> None:

    from google.cloud import bigquery

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

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

    dataset = client.get_dataset(dataset_id)  # Make an API request.

    # Set the default partition expiration (applies to new tables, only) in
    # milliseconds. This example sets the default expiration to 90 days.
    dataset.default_partition_expiration_ms = 90 * 24 * 60 * 60 * 1000

    dataset = client.update_dataset(
        dataset, ["default_partition_expiration_ms"]
    )  # Make an API request.

    print(
        "Updated dataset {}.{} with new default partition expiration {}".format(
            dataset.project, dataset.dataset_id, dataset.default_partition_expiration_ms
        )
    )

更新資料表的捨入模式

您可以使用 ALTER TABLE SET OPTIONS DDL 陳述式,更新資料表的預設捨入模式。以下範例會將 mytable 的預設捨去模式更新為 ROUND_HALF_EVEN

ALTER TABLE mydataset.mytable
SET OPTIONS (
  default_rounding_mode = "ROUND_HALF_EVEN");

NUMERICBIGNUMERIC 欄位新增至資料表時,如果未指定捨入模式,系統會自動將捨入模式設為資料表的預設捨入模式。變更資料表的預設捨入模式不會影響現有欄位的捨入模式。

更新資料表的結構定義

如要進一步瞭解如何更新資料表結構定義,請參閱修改資料表結構定義

重新命名資料表

資料表建立後,您可以使用 ALTER TABLE RENAME TO 陳述式重新命名。以下範例會將 mytable 重新命名為 mynewtable

ALTER TABLE mydataset.mytable
RENAME TO mynewtable;

ALTER TABLE RENAME TO 陳述式會在目的地資料集中重新建立資料表,並使用原始資料表的建立時間戳記。如果您已設定資料集層級的資料表到期時間,如果重新命名的資料表原始建立時間戳記超出到期時間範圍,系統可能會立即刪除該資料表。

重新命名資料表的限制

  • 如要重新命名正在串流資料的表格,請停止串流、提交所有待處理的串流,並等待 BigQuery 指出串流未在使用中。
  • 通常在上次串流作業的 5 小時後,即可重新命名表格,但有時可能需要更久。
  • 系統會保留現有的資料表 ACL 和資料列存取政策,但不會保留在資料表重新命名期間所做的資料表 ACL 和資料列存取政策更新。
  • 您無法同時重新命名資料表,並對該資料表執行 DML 陳述式。
  • 重新命名資料表會移除資料表上的所有Data Catalog 標記 (已淘汰) 和 Dataplex Universal Catalog 構面
  • 重新命名資料表時,系統會捨棄該資料表建立的任何搜尋索引或向量索引。
  • 您無法重新命名外部資料表。

複製資料表

本節說明如何建立資料表的完整副本。如要瞭解其他類型的資料表副本,請參閱資料表副本資料表快照

您可以透過下列方式複製資料表:

  • 使用 Google Cloud 控制台。
  • 使用 bq cp 指令。
  • 使用資料定義語言 (DDL) CREATE TABLE COPY 陳述式。
  • 呼叫 jobs.insert API 方法並設定 copy 工作。
  • 使用用戶端程式庫。

複製資料表的限制

資料表複製工作有下列限制:

  • 表格複製作業開始後就無法停止。表格複製作業會非同步執行,即使取消工作也不會停止。跨區域複製資料表時,您也需要支付資料移轉費用,以及目標區域的儲存空間費用。
  • 當您複製資料表時,目的地資料表的名稱必須遵循您建立資料表時所使用的命名慣例。
  • 資料表複製必須遵循 BigQuery 有關複製工作的限制
  • Google Cloud 主控台一次只能複製一個資料表。您無法覆寫目的地資料集中的現有資料表。目的地資料集中的資料表名稱不得重複。
  • Google Cloud 控制台不支援將多個來源資料表複製到目的地資料表。
  • 使用 API、bq 指令列工具或用戶端程式庫複製多個來源資料表到目標資料表時,所有來源資料表都必須具有相同的結構定義,包括任何分割或叢集。

    某些資料表結構定義更新 (例如捨棄或重新命名資料欄),可能會導致資料表具有看似相同的結構定義,但內部表示法不同。這可能會導致資料表複製工作失敗,並出現 Maximum limit on diverging physical schemas reached 錯誤。在這種情況下,您可以使用 CREATE TABLE LIKE 陳述式,確保來源資料表的結構定義與目的地資料表的結構定義完全一致。

  • 由於基礎儲存空間是動態管理,因此 BigQuery 複製資料表所需的時間可能會因不同執行作業而有顯著差異。

  • 如果目的地資料表的資料欄數多於來源資料表,且額外資料欄有預設值,您就無法複製來源資料表並附加至目的地資料表。但您可以執行 INSERT destination_table SELECT * FROM source_table 來複製資料。

  • 如果複製作業覆寫現有資料表,系統會保留現有資料表的資料表層級存取權。來源資料表的標記不會複製到覆寫的資料表,但現有資料表的標記會保留。不過,跨區域複製資料表時,現有資料表中的標記會遭到移除。

  • 如果複製作業會建立新資料表,則新資料表的資料表層級存取權,取決於新資料表所屬資料集的存取權政策。此外,系統也會將標籤從來源資料表複製到新資料表。

  • 將多個來源資料表複製到目的地資料表時,所有來源資料表都必須具有相同的標記。

必要的角色

如要執行本文中的工作,您需要下列權限。

複製資料表和分區所需的角色

如要取得複製資料表和分區所需的權限,請要求管理員授予您來源和目的地資料集的資料編輯者 (roles/bigquery.dataEditor) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

這個預先定義的角色具備複製表格和資料分割區所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:

所需權限

您必須具備下列權限,才能複製資料表和分區:

  • bigquery.tables.getData 來源和目的地資料集
  • bigquery.tables.get 來源和目的地資料集
  • bigquery.tables.create 目的地資料集
  • bigquery.tables.update 目的地資料集

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

執行複製作業的權限

如要取得執行複製工作所需的權限,請要求管理員授予您來源和目的地資料集的工作使用者 (roles/bigquery.jobUser) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

這個預先定義的角色具備 bigquery.jobs.create權限,可執行複製工作。

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

複製單一來源資料表

您可以透過以下方式複製單一資料表:

  • 使用 Google Cloud 控制台。
  • 使用 bq 指令列工具的 bq cp 指令。
  • 使用資料定義語言 (DDL) CREATE TABLE COPY 陳述式。
  • 呼叫 jobs.insert API 方法、設定 copy 工作,然後指定 sourceTable 屬性。
  • 使用用戶端程式庫。

Google Cloud 控制台和 CREATE TABLE COPY 陳述式在複製工作中,都只支援一個來源資料表和一個目的地資料表。如要將多個來源檔案複製到目的地資料表,請使用 bq 指令列工具或 API。

如要複製單一來源資料表:

控制台

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

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

  2. 在「Explorer」窗格中展開專案,點選「Datasets」(資料集),然後選取資料集。

  3. 依序按一下「總覽」>「表格」,然後選取所需表格。

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

  5. 在「Copy table」(複製資料表) 對話方塊中的「Destination」(目的地) 下方:

    • 在「Project」(專案) 部分,選擇將用來儲存複製資料表的專案。
    • 針對「Dataset」(資料集),選取您要用來儲存複製資料表的資料集。來源與目的地資料集必須位於相同的位置
    • 針對「Table」(資料表),輸入新資料表的名稱。目標資料集中的資料表名稱不得重複。您無法使用 Google Cloud 控制台覆寫目的地資料集中現有的資料表。如要進一步瞭解資料表名稱規定,請參閱「資料表命名」。
  6. 按一下 [Copy] (複製) 即可開始複製工作。

SQL

使用 CREATE TABLE COPY 陳述式,將名為 table1 的資料表複製到名為 table1copy 的新資料表:

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

    前往「BigQuery」

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

    CREATE TABLE myproject.mydataset.table1copy
    COPY myproject.mydataset.table1;

  3. 按一下「執行」

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

bq

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 請發出 bq cp 指令。選用標記可用來控管目的地資料表的寫入配置:

    • -a--append_table:把來源資料表的資料附加到目的地資料集中現有的資料表上。
    • -f--force:覆寫目的地資料集中的現有資料表,此作業不會有確認提示。
    • 如果目的地資料集裡已有資料表,-n--no_clobber 會傳回下列錯誤訊息:Table 'project_id:dataset.table' already exists, skipping.。如未指定 -n,預設行為是提示您選擇是否要取代目的地資料表。
    • --destination_kms_key:由客戶管理的 Cloud KMS 金鑰,可用來為目的地資料表加密。

    本文不示範 --destination_kms_key。詳情請參閱使用 Cloud Key Management Service 金鑰保護資料

    如果來源或目的地資料集位於非預設專案中,請採用下列格式將專案 ID 新增至該資料集名稱:project_id:dataset

    (選用) 提供 --location 旗標,並將值設為您的位置

    bq --location=location cp \
    -a -f -n \
    project_id:dataset.source_table \
    project_id:dataset.destination_table

    更改下列內容:

    • location:位置名稱。--location 是選用旗標。舉例來說,如果您在東京地區使用 BigQuery,就可以將旗標的值設為 asia-northeast1。您可以使用 .bigqueryrc 檔案,設定該位置的預設值。
    • project_id:您的專案 ID。
    • dataset:來源或目的地資料集的名稱。
    • source_table:您要複製的資料表。
    • destination_table:目的地資料集中的資料表名稱。

    範例:

    如要將 mydataset.mytable 資料表複製到 mydataset2.mytable2 資料表,請輸入下列指令。這兩個資料集都在預設專案中。

    bq cp mydataset.mytable mydataset2.mytable2
    

    如要複製 mydataset.mytable 資料表,並覆寫有相同名稱的目的地資料表,請輸入下列指令。來源資料集位於預設專案中。目的地資料集位於 myotherproject 專案中。-f 捷徑可用來在無提示的情況下覆寫目的地資料表。

    bq cp -f \
    mydataset.mytable \
    myotherproject:myotherdataset.mytable

    如要複製 mydataset.mytable 資料表,並在目的地資料集有相同名稱的資料表時傳回錯誤,請輸入下列指令。來源資料集位於預設專案中。目的地資料集位於 myotherproject 專案中。-n 捷徑用於防止名稱相同的資料表遭到覆寫。

    bq cp -n \
    mydataset.mytable \
    myotherproject:myotherdataset.mytable

    如要複製 mydataset.mytable 資料表,並將資料附加到有相同名稱的目的地資料表,請輸入下列指令。來源資料集位於預設專案中。目的地資料集位於 myotherproject 專案中。- a 捷徑是用來附加到目的地資料表。

    bq cp -a mydataset.mytable myotherproject:myotherdataset.mytable
    
  3. API

    您可以呼叫 bigquery.jobs.insert 方法並設定 copy 工作,透過 API 複製現有資料表。在工作資源jobReference 區段中,於 location 屬性指定您的位置。

    您必須在工作設定中指定下列值:

    "copy": {
          "sourceTable": {       // Required
            "projectId": string, // Required
            "datasetId": string, // Required
            "tableId": string    // Required
          },
          "destinationTable": {  // Required
            "projectId": string, // Required
            "datasetId": string, // Required
            "tableId": string    // Required
          },
          "createDisposition": string,  // Optional
          "writeDisposition": string,   // Optional
        },
    

    其中 sourceTable 提供要複製的資料表相關資訊,destinationTable 提供新資料表相關資訊,createDisposition 指定資料表不存在時是否建立資料表,而 writeDisposition 則指定是否覆寫或附加至現有資料表。

    C#

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

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

    
    using Google.Apis.Bigquery.v2.Data;
    using Google.Cloud.BigQuery.V2;
    using System;
    
    public class BigQueryCopyTable
    {
        public void CopyTable(
            string projectId = "your-project-id",
            string destinationDatasetId = "your_dataset_id"
        )
        {
            BigQueryClient client = BigQueryClient.Create(projectId);
            TableReference sourceTableRef = new TableReference()
            {
                TableId = "shakespeare",
                DatasetId = "samples",
                ProjectId = "bigquery-public-data"
            };
            TableReference destinationTableRef = client.GetTableReference(
                destinationDatasetId, "destination_table");
            BigQueryJob job = client.CreateCopyJob(
                sourceTableRef, destinationTableRef)
                .PollUntilCompleted() // Wait for the job to complete.
                .ThrowOnAnyError();
    
            // Retrieve destination table
            BigQueryTable destinationTable = client.GetTable(destinationTableRef);
            Console.WriteLine(
                $"Copied {destinationTable.Resource.NumRows} rows from table "
                + $"{sourceTableRef.DatasetId}.{sourceTableRef.TableId} "
                + $"to {destinationTable.FullyQualifiedId}."
            );
        }
    }

    Go

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

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

    import (
    	"context"
    	"fmt"
    
    	"cloud.google.com/go/bigquery"
    )
    
    // copyTable demonstrates copying a table from a source to a destination, and
    // allowing the copy to overwrite existing data by using truncation.
    func copyTable(projectID, datasetID, srcID, dstID string) error {
    	// projectID := "my-project-id"
    	// datasetID := "mydataset"
    	// srcID := "sourcetable"
    	// dstID := "destinationtable"
    	ctx := context.Background()
    	client, err := bigquery.NewClient(ctx, projectID)
    	if err != nil {
    		return fmt.Errorf("bigquery.NewClient: %v", err)
    	}
    	defer client.Close()
    
    	dataset := client.Dataset(datasetID)
    	copier := dataset.Table(dstID).CopierFrom(dataset.Table(srcID))
    	copier.WriteDisposition = bigquery.WriteTruncate
    	job, err := copier.Run(ctx)
    	if err != nil {
    		return err
    	}
    	status, err := job.Wait(ctx)
    	if err != nil {
    		return err
    	}
    	if err := status.Err(); err != nil {
    		return err
    	}
    	return nil
    }
    

    Java

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

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

    import com.google.cloud.bigquery.BigQuery;
    import com.google.cloud.bigquery.BigQueryException;
    import com.google.cloud.bigquery.BigQueryOptions;
    import com.google.cloud.bigquery.CopyJobConfiguration;
    import com.google.cloud.bigquery.Job;
    import com.google.cloud.bigquery.JobInfo;
    import com.google.cloud.bigquery.TableId;
    
    public class CopyTable {
    
      public static void runCopyTable() {
        // TODO(developer): Replace these variables before running the sample.
        String destinationDatasetName = "MY_DESTINATION_DATASET_NAME";
        String destinationTableId = "MY_DESTINATION_TABLE_NAME";
        String sourceDatasetName = "MY_SOURCE_DATASET_NAME";
        String sourceTableId = "MY_SOURCE_TABLE_NAME";
    
        copyTable(sourceDatasetName, sourceTableId, destinationDatasetName, destinationTableId);
      }
    
      public static void copyTable(
          String sourceDatasetName,
          String sourceTableId,
          String destinationDatasetName,
          String destinationTableId) {
        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();
    
          TableId sourceTable = TableId.of(sourceDatasetName, sourceTableId);
          TableId destinationTable = TableId.of(destinationDatasetName, destinationTableId);
    
          // For more information on CopyJobConfiguration see:
          // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/bigquery/JobConfiguration.html
          CopyJobConfiguration configuration =
              CopyJobConfiguration.newBuilder(destinationTable, sourceTable).build();
    
          // For more information on Job see:
          // https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html
          Job job = bigquery.create(JobInfo.of(configuration));
    
          // Blocks until this job completes its execution, either failing or succeeding.
          Job completedJob = job.waitFor();
          if (completedJob == null) {
            System.out.println("Job not executed since it no longer exists.");
            return;
          } else if (completedJob.getStatus().getError() != null) {
            System.out.println(
                "BigQuery was unable to copy table due to an error: \n" + job.getStatus().getError());
            return;
          }
          System.out.println("Table copied successfully.");
        } catch (BigQueryException | InterruptedException e) {
          System.out.println("Table copying job was interrupted. \n" + e.toString());
        }
      }
    }

    Node.js

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

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

    // Import the Google Cloud client library and create a client
    const {BigQuery} = require('@google-cloud/bigquery');
    const bigquery = new BigQuery();
    
    async function copyTable() {
      // Copies src_dataset:src_table to dest_dataset:dest_table.
    
      /**
       * TODO(developer): Uncomment the following lines before running the sample
       */
      // const srcDatasetId = "my_src_dataset";
      // const srcTableId = "my_src_table";
      // const destDatasetId = "my_dest_dataset";
      // const destTableId = "my_dest_table";
    
      // Copy the table contents into another table
      const [job] = await bigquery
        .dataset(srcDatasetId)
        .table(srcTableId)
        .copy(bigquery.dataset(destDatasetId).table(destTableId));
    
      console.log(`Job ${job.id} completed.`);
    
      // Check the job's status for errors
      const errors = job.status.errors;
      if (errors && errors.length > 0) {
        throw errors;
      }
    }

    PHP

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

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

    use Google\Cloud\BigQuery\BigQueryClient;
    use Google\Cloud\Core\ExponentialBackoff;
    
    /** Uncomment and populate these variables in your code */
    // $projectId = 'The Google project ID';
    // $datasetId = 'The BigQuery dataset ID';
    // $sourceTableId   = 'The BigQuery table ID to copy from';
    // $destinationTableId = 'The BigQuery table ID to copy to';
    
    $bigQuery = new BigQueryClient([
        'projectId' => $projectId,
    ]);
    $dataset = $bigQuery->dataset($datasetId);
    $sourceTable = $dataset->table($sourceTableId);
    $destinationTable = $dataset->table($destinationTableId);
    $copyConfig = $sourceTable->copy($destinationTable);
    $job = $sourceTable->runJob($copyConfig);
    
    // poll the job until it is complete
    $backoff = new ExponentialBackoff(10);
    $backoff->execute(function () use ($job) {
        print('Waiting for job to complete' . PHP_EOL);
        $job->reload();
        if (!$job->isComplete()) {
            throw new Exception('Job has not yet completed', 500);
        }
    });
    // check if the job has errors
    if (isset($job->info()['status']['errorResult'])) {
        $error = $job->info()['status']['errorResult']['message'];
        printf('Error running job: %s' . PHP_EOL, $error);
    } else {
        print('Table copied successfully' . PHP_EOL);
    }

    Python

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

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

    
    from google.cloud import bigquery
    
    # Construct a BigQuery client object.
    client = bigquery.Client()
    
    # TODO(developer): Set source_table_id to the ID of the original table.
    # source_table_id = "your-project.source_dataset.source_table"
    
    # TODO(developer): Set destination_table_id to the ID of the destination table.
    # destination_table_id = "your-project.destination_dataset.destination_table"
    
    job = client.copy_table(source_table_id, destination_table_id)
    job.result()  # Wait for the job to complete.
    
    print("A copy of the table created.")

複製多個來源資料表

您可以透過下列方式,將多個來源資料表複製到目的地資料表:

  • 使用 bq 指令列工具的 bq cp 指令。
  • 呼叫 jobs.insert 方法、設定 copy 工作,然後指定 sourceTables 屬性。
  • 使用用戶端程式庫。

所有來源資料表都必須擁有相同的結構定義和標記,且只能有一個目的地資料表。

來源資料表必須指定為逗號分隔的清單。複製多個來源資料表時,無法使用萬用字元。

如要複製多個來源資料表,請選取下列其中一個選項:

bq

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 發出 bq cp 指令,並以逗號分隔清單的形式包含多個來源資料表。 選用標記可用來控管目的地資料表的寫入配置:

    • -a--append_table:把來源資料表的資料附加到目的地資料集中現有的資料表上。
    • -f--force:覆寫目的地資料集的現有目的地資料表,並且不會提示您確認。
    • 如果目的地資料集裡已有資料表,-n--no_clobber 會傳回下列錯誤訊息:Table 'project_id:dataset.table' already exists, skipping.。如未指定 -n,預設行為是提示您選擇是否要取代目的地資料表。
    • --destination_kms_key:由客戶管理的 Cloud Key Management Service 金鑰,可用來為目的地資料表加密。

    本文不示範 --destination_kms_key。詳情請參閱使用 Cloud Key Management Service 金鑰保護資料

    如果來源或目的地資料集位於非預設專案中,請採用下列格式將專案 ID 新增至該資料集名稱:project_id:dataset

    (選用) 提供 --location 旗標,並將值設為您的位置

    bq --location=location cp \
    -a -f -n \
    project_id:dataset.source_table,project_id:dataset.source_table \
    project_id:dataset.destination_table

    更改下列內容:

    • location:位置名稱。--location 是選用旗標。舉例來說,如果您在東京地區使用 BigQuery,就可以將旗標的值設為 asia-northeast1。您可以使用 .bigqueryrc 檔案,設定該位置的預設值。
    • project_id:您的專案 ID。
    • dataset:來源或目的地資料集的名稱。
    • source_table:您要複製的資料表。
    • destination_table:目的地資料集中的資料表名稱。

    範例:

    如要將 mydataset.mytable 資料表和 mydataset.mytable2 資料表複製到 mydataset2.tablecopy 資料表,請輸入下列指令。這兩個資料集都在預設專案中。

    bq cp \
    mydataset.mytable,mydataset.mytable2 \
    mydataset2.tablecopy

    如要將 mydataset.mytable 資料表和 mydataset.mytable2 資料表複製到 myotherdataset.mytable 資料表,並覆寫有相同名稱的目的地資料表,請輸入下列指令。目的地資料集位於 myotherproject 專案,而非預設專案。-f 快速鍵可用來在無提示的情況下覆寫目的地資料表。

    bq cp -f \
    mydataset.mytable,mydataset.mytable2 \
    myotherproject:myotherdataset.mytable

    如要複製 myproject:mydataset.mytable 資料表和 myproject:mydataset.mytable2 資料表,並在目的地資料集有相同名稱的資料表時傳回錯誤,請輸入下列指令。目的地資料集位於 myotherproject 專案中。-n 捷徑是用來防止有相同名稱的資料表遭到覆寫。

    bq cp -n \
    myproject:mydataset.mytable,myproject:mydataset.mytable2 \
    myotherproject:myotherdataset.mytable

    如要複製 mydataset.mytablemydataset.mytable2 資料表,並將資料附加到有相同名稱的目的地資料表,請輸入下列指令。來源資料集位於預設專案中。目的地資料集位於 myotherproject 專案中。-a 快速鍵是用來附加到目的地資料表。

    bq cp -a \
    mydataset.mytable,mydataset.mytable2 \
    myotherproject:myotherdataset.mytable
  3. API

    如要使用 API 複製多個資料表,請呼叫 jobs.insert 方法、設定資料表 copy 工作,並指定 sourceTables 屬性。

    工作資源jobReference 區段中,於 location 屬性指定您的地區。

    Go

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

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

    import (
    	"context"
    	"fmt"
    
    	"cloud.google.com/go/bigquery"
    )
    
    // copyMultiTable demonstrates using a copy job to copy multiple source tables into a single destination table.
    func copyMultiTable(projectID, srcDatasetID string, srcTableIDs []string, dstDatasetID, dstTableID string) error {
    	// projectID := "my-project-id"
    	// srcDatasetID := "sourcedataset"
    	// srcTableIDs := []string{"table1","table2"}
    	// dstDatasetID = "destinationdataset"
    	// dstTableID = "destinationtable"
    	ctx := context.Background()
    	client, err := bigquery.NewClient(ctx, projectID)
    	if err != nil {
    		return fmt.Errorf("bigquery.NewClient: %v", err)
    	}
    	defer client.Close()
    
    	srcDataset := client.Dataset(srcDatasetID)
    	dstDataset := client.Dataset(dstDatasetID)
    	var tableRefs []*bigquery.Table
    	for _, v := range srcTableIDs {
    		tableRefs = append(tableRefs, srcDataset.Table(v))
    	}
    	copier := dstDataset.Table(dstTableID).CopierFrom(tableRefs...)
    	copier.WriteDisposition = bigquery.WriteTruncate
    	job, err := copier.Run(ctx)
    	if err != nil {
    		return err
    	}
    	status, err := job.Wait(ctx)
    	if err != nil {
    		return err
    	}
    	if err := status.Err(); err != nil {
    		return err
    	}
    	return nil
    }
    

    Java

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

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

    import com.google.cloud.bigquery.BigQuery;
    import com.google.cloud.bigquery.BigQueryException;
    import com.google.cloud.bigquery.BigQueryOptions;
    import com.google.cloud.bigquery.CopyJobConfiguration;
    import com.google.cloud.bigquery.Job;
    import com.google.cloud.bigquery.JobInfo;
    import com.google.cloud.bigquery.TableId;
    import java.util.Arrays;
    
    public class CopyMultipleTables {
    
      public static void runCopyMultipleTables() {
        // TODO(developer): Replace these variables before running the sample.
        String destinationDatasetName = "MY_DATASET_NAME";
        String destinationTableId = "MY_TABLE_NAME";
        copyMultipleTables(destinationDatasetName, destinationTableId);
      }
    
      public static void copyMultipleTables(String destinationDatasetName, String destinationTableId) {
        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();
    
          TableId destinationTable = TableId.of(destinationDatasetName, destinationTableId);
    
          // For more information on CopyJobConfiguration see:
          // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/bigquery/JobConfiguration.html
          CopyJobConfiguration configuration =
              CopyJobConfiguration.newBuilder(
                      destinationTable,
                      Arrays.asList(
                          TableId.of(destinationDatasetName, "table1"),
                          TableId.of(destinationDatasetName, "table2")))
                  .build();
    
          // For more information on Job see:
          // https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html
          Job job = bigquery.create(JobInfo.of(configuration));
    
          // Blocks until this job completes its execution, either failing or succeeding.
          Job completedJob = job.waitFor();
          if (completedJob == null) {
            System.out.println("Job not executed since it no longer exists.");
            return;
          } else if (completedJob.getStatus().getError() != null) {
            System.out.println(
                "BigQuery was unable to copy tables due to an error: \n" + job.getStatus().getError());
            return;
          }
          System.out.println("Table copied successfully.");
        } catch (BigQueryException | InterruptedException e) {
          System.out.println("Table copying job was interrupted. \n" + e.toString());
        }
      }
    }

    Node.js

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

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

    // Import the Google Cloud client library
    const {BigQuery} = require('@google-cloud/bigquery');
    const bigquery = new BigQuery();
    
    async function copyTableMultipleSource() {
      // Copy multiple source tables to a given destination.
    
      /**
       * TODO(developer): Uncomment the following lines before running the sample.
       */
      // const datasetId = "my_dataset";
      // sourceTable = 'my_table';
      // destinationTable = 'testing';
    
      // Create a client
      const dataset = bigquery.dataset(datasetId);
    
      const metadata = {
        createDisposition: 'CREATE_NEVER',
        writeDisposition: 'WRITE_TRUNCATE',
      };
    
      // Create table references
      const table = dataset.table(sourceTable);
      const yourTable = dataset.table(destinationTable);
    
      // Copy table
      const [apiResponse] = await table.copy(yourTable, metadata);
      console.log(apiResponse.configuration.copy);
    }

    Python

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

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

    
    from google.cloud import bigquery
    
    # Construct a BigQuery client object.
    client = bigquery.Client()
    
    # TODO(developer): Set dest_table_id to the ID of the destination table.
    # dest_table_id = "your-project.your_dataset.your_table_name"
    
    # TODO(developer): Set table_ids to the list of the IDs of the original tables.
    # table_ids = ["your-project.your_dataset.your_table_name", ...]
    
    job = client.copy_table(table_ids, dest_table_id)  # Make an API request.
    job.result()  # Wait for the job to complete.
    
    print("The tables {} have been appended to {}".format(table_ids, dest_table_id))

跨區域複製資料表

您可以將資料表、資料表快照資料表副本,從一個 BigQuery 地區或多個地區複製到另一個地區。包括套用客戶管理 Cloud KMS (CMEK) 的任何資料表。

跨區域複製資料表時,系統會根據 BigQuery 定價收取額外資料移轉費用。即使在跨區域複製資料表作業完成前取消,仍會產生額外費用。

如要跨區域複製資料表,請選取下列其中一個選項:

bq

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 執行 bq cp 指令

  3.    bq cp \
       -f -n \
       SOURCE_PROJECT:SOURCE_DATASET.SOURCE_TABLE \
       DESTINATION_PROJECT:DESTINATION_DATASET.DESTINATION_TABLE
       

    更改下列內容:

    • SOURCE_PROJECT:來源專案 ID。如果來源資料集位於非預設專案中,請將專案 ID 新增至來源資料集名稱。

    • DESTINATION_PROJECT:目的地專案 ID。如果目的地資料集位於非預設專案中,請將專案 ID 新增至目的地資料集名稱。

    • SOURCE_DATASET:來源資料集的名稱。

    • DESTINATION_DATASET:目的地資料集的名稱。

    • SOURCE_TABLE:您要複製的資料表。

    • DESTINATION_TABLE:目的地資料集中的資料表名稱。

    以下範例指令會將 us 多區域的 mydataset_us.mytable 資料表複製到 eu 多區域的 mydataset_eu.mytable2 資料表。這兩個資料集都在預設專案中。

    bq cp --sync=false mydataset_us.mytable mydataset_eu.mytable2
    

    如要跨區域將資料表複製到已啟用 CMEK 的目的地資料集,您必須在資料表上啟用 CMEK,並使用資料表所在區域的金鑰。資料表上的 CMEK 不必與目的地資料集使用的 CMEK 相同。以下範例會使用 bq cp 指令,將啟用 CMEK 的資料表複製到目的地資料集。

    bq cp source-project-id:source-dataset-id.source-table-id destination-project-id:destination-dataset-id.destination-table-id
    

    反之,如要將已啟用 CMEK 的資料表跨區域複製到目的地資料集,可以在目的地資料集上啟用 CMEK,並使用目的地資料集區域的金鑰。您也可以在 bq cp 指令中使用 destination_kms_keys 旗標,如下列範例所示:

    bq cp --destination_kms_key=projects/project_id/locations/eu/keyRings/eu_key/cryptoKeys/eu_region mydataset_us.mytable mydataset_eu.mytable2
    

    API

    如要使用 API 跨區域複製資料表,請呼叫 jobs.insert 方法並設定資料表 copy 工作。

    工作資源jobReference 區段中,於 location 屬性指定您的地區。

    C#

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

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

    
    using Google.Apis.Bigquery.v2.Data;
    using Google.Cloud.BigQuery.V2;
    using System;
    
    public class BigQueryCopyTable
    {
        public void CopyTable(
            string projectId = "your-project-id",
            string destinationDatasetId = "your_dataset_id"
        )
        {
            BigQueryClient client = BigQueryClient.Create(projectId);
            TableReference sourceTableRef = new TableReference()
            {
                TableId = "shakespeare",
                DatasetId = "samples",
                ProjectId = "bigquery-public-data"
            };
            TableReference destinationTableRef = client.GetTableReference(
                destinationDatasetId, "destination_table");
            BigQueryJob job = client.CreateCopyJob(
                sourceTableRef, destinationTableRef)
                .PollUntilCompleted() // Wait for the job to complete.
                .ThrowOnAnyError();
    
            // Retrieve destination table
            BigQueryTable destinationTable = client.GetTable(destinationTableRef);
            Console.WriteLine(
                $"Copied {destinationTable.Resource.NumRows} rows from table "
                + $"{sourceTableRef.DatasetId}.{sourceTableRef.TableId} "
                + $"to {destinationTable.FullyQualifiedId}."
            );
        }
    }

    Go

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

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

    import (
    	"context"
    	"fmt"
    
    	"cloud.google.com/go/bigquery"
    )
    
    // copyTable demonstrates copying a table from a source to a destination, and
    // allowing the copy to overwrite existing data by using truncation.
    func copyTable(projectID, datasetID, srcID, dstID string) error {
    	// projectID := "my-project-id"
    	// datasetID := "mydataset"
    	// srcID := "sourcetable"
    	// dstID := "destinationtable"
    	ctx := context.Background()
    	client, err := bigquery.NewClient(ctx, projectID)
    	if err != nil {
    		return fmt.Errorf("bigquery.NewClient: %v", err)
    	}
    	defer client.Close()
    
    	dataset := client.Dataset(datasetID)
    	copier := dataset.Table(dstID).CopierFrom(dataset.Table(srcID))
    	copier.WriteDisposition = bigquery.WriteTruncate
    	job, err := copier.Run(ctx)
    	if err != nil {
    		return err
    	}
    	status, err := job.Wait(ctx)
    	if err != nil {
    		return err
    	}
    	if err := status.Err(); err != nil {
    		return err
    	}
    	return nil
    }
    

    Java

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

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

    import com.google.cloud.bigquery.BigQuery;
    import com.google.cloud.bigquery.BigQueryException;
    import com.google.cloud.bigquery.BigQueryOptions;
    import com.google.cloud.bigquery.CopyJobConfiguration;
    import com.google.cloud.bigquery.Job;
    import com.google.cloud.bigquery.JobInfo;
    import com.google.cloud.bigquery.TableId;
    
    public class CopyTable {
    
      public static void runCopyTable() {
        // TODO(developer): Replace these variables before running the sample.
        String destinationDatasetName = "MY_DESTINATION_DATASET_NAME";
        String destinationTableId = "MY_DESTINATION_TABLE_NAME";
        String sourceDatasetName = "MY_SOURCE_DATASET_NAME";
        String sourceTableId = "MY_SOURCE_TABLE_NAME";
    
        copyTable(sourceDatasetName, sourceTableId, destinationDatasetName, destinationTableId);
      }
    
      public static void copyTable(
          String sourceDatasetName,
          String sourceTableId,
          String destinationDatasetName,
          String destinationTableId) {
        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();
    
          TableId sourceTable = TableId.of(sourceDatasetName, sourceTableId);
          TableId destinationTable = TableId.of(destinationDatasetName, destinationTableId);
    
          // For more information on CopyJobConfiguration see:
          // https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/bigquery/JobConfiguration.html
          CopyJobConfiguration configuration =
              CopyJobConfiguration.newBuilder(destinationTable, sourceTable).build();
    
          // For more information on Job see:
          // https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html
          Job job = bigquery.create(JobInfo.of(configuration));
    
          // Blocks until this job completes its execution, either failing or succeeding.
          Job completedJob = job.waitFor();
          if (completedJob == null) {
            System.out.println("Job not executed since it no longer exists.");
            return;
          } else if (completedJob.getStatus().getError() != null) {
            System.out.println(
                "BigQuery was unable to copy table due to an error: \n" + job.getStatus().getError());
            return;
          }
          System.out.println("Table copied successfully.");
        } catch (BigQueryException | InterruptedException e) {
          System.out.println("Table copying job was interrupted. \n" + e.toString());
        }
      }
    }

    Node.js

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

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

    // Import the Google Cloud client library and create a client
    const {BigQuery} = require('@google-cloud/bigquery');
    const bigquery = new BigQuery();
    
    async function copyTable() {
      // Copies src_dataset:src_table to dest_dataset:dest_table.
    
      /**
       * TODO(developer): Uncomment the following lines before running the sample
       */
      // const srcDatasetId = "my_src_dataset";
      // const srcTableId = "my_src_table";
      // const destDatasetId = "my_dest_dataset";
      // const destTableId = "my_dest_table";
    
      // Copy the table contents into another table
      const [job] = await bigquery
        .dataset(srcDatasetId)
        .table(srcTableId)
        .copy(bigquery.dataset(destDatasetId).table(destTableId));
    
      console.log(`Job ${job.id} completed.`);
    
      // Check the job's status for errors
      const errors = job.status.errors;
      if (errors && errors.length > 0) {
        throw errors;
      }
    }

    PHP

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

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

    use Google\Cloud\BigQuery\BigQueryClient;
    use Google\Cloud\Core\ExponentialBackoff;
    
    /** Uncomment and populate these variables in your code */
    // $projectId = 'The Google project ID';
    // $datasetId = 'The BigQuery dataset ID';
    // $sourceTableId   = 'The BigQuery table ID to copy from';
    // $destinationTableId = 'The BigQuery table ID to copy to';
    
    $bigQuery = new BigQueryClient([
        'projectId' => $projectId,
    ]);
    $dataset = $bigQuery->dataset($datasetId);
    $sourceTable = $dataset->table($sourceTableId);
    $destinationTable = $dataset->table($destinationTableId);
    $copyConfig = $sourceTable->copy($destinationTable);
    $job = $sourceTable->runJob($copyConfig);
    
    // poll the job until it is complete
    $backoff = new ExponentialBackoff(10);
    $backoff->execute(function () use ($job) {
        print('Waiting for job to complete' . PHP_EOL);
        $job->reload();
        if (!$job->isComplete()) {
            throw new Exception('Job has not yet completed', 500);
        }
    });
    // check if the job has errors
    if (isset($job->info()['status']['errorResult'])) {
        $error = $job->info()['status']['errorResult']['message'];
        printf('Error running job: %s' . PHP_EOL, $error);
    } else {
        print('Table copied successfully' . PHP_EOL);
    }

    Python

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

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

    
    from google.cloud import bigquery
    
    # Construct a BigQuery client object.
    client = bigquery.Client()
    
    # TODO(developer): Set source_table_id to the ID of the original table.
    # source_table_id = "your-project.source_dataset.source_table"
    
    # TODO(developer): Set destination_table_id to the ID of the destination table.
    # destination_table_id = "your-project.destination_dataset.destination_table"
    
    job = client.copy_table(source_table_id, destination_table_id)
    job.result()  # Wait for the job to complete.
    
    print("A copy of the table created.")

限制

跨區域複製資料表有下列限制:

  • 您無法使用 Google Cloud 控制台或 TABLE COPY DDL 陳述式複製資料表。
  • 如果來源資料表有任何政策標記,您就無法複製該資料表。
  • 如果來源資料表大於 20 個實體 TiB,就無法複製資料表。如要查看來源資料表的實際大小,請參閱取得資料表的相關資訊。此外,如果來源資料表大於 1 個實體 TiB,跨區域複製時可能需要多次重試才能成功。
  • 您無法複製與資料表相關聯的 IAM 政策。複製完成後,您可以將相同的政策套用至目的地。
  • 如果複製作業會覆寫現有資料表,現有資料表上的標記就會移除。
  • 您無法將多個來源資料表複製到單一目的地資料表。
  • 在附加模式下,您無法複製表格。
  • 時空旅行資訊不會複製到目標區域。
  • 將資料表副本或快照複製到新區域時,系統會建立資料表的完整副本。這會產生額外儲存費用。

查看目前的配額用量

您可以執行 INFORMATION_SCHEMA 查詢,查看特定時間範圍內執行的工作相關中繼資料,瞭解目前查詢、載入、擷取或複製工作的使用情形。您可以將目前用量與配額限制進行比較,判斷特定類型作業的配額用量。下列查詢範例會使用 INFORMATION_SCHEMA.JOBS 檢視表,依專案列出查詢、載入、擷取和複製工作的數量:

SELECT
  sum(case  when job_type="QUERY" then 1 else 0 end) as QRY_CNT,
  sum(case  when job_type="LOAD" then 1 else 0 end) as LOAD_CNT,
  sum(case  when job_type="EXTRACT" then 1 else 0 end) as EXT_CNT,
  sum(case  when job_type="COPY" then 1 else 0 end) as CPY_CNT
FROM `region-REGION_NAME`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE date(creation_time)= CURRENT_DATE()

每個專案每日的複製工作數量上限 (配額錯誤)

如果專案中執行的複製工作數超過每日上限,BigQuery 就會傳回這項錯誤。如要進一步瞭解每日複製作業的限制,請參閱複製作業

錯誤訊息

Your project exceeded quota for copies per project

診斷

如要收集更多有關複製作業來源的資料,可以嘗試下列做法:

  • 如果複製作業位於單一或少數幾個區域,您可以嘗試查詢特定區域的 INFORMATION_SCHEMA.JOBS 表格。例如:

    SELECT
    creation_time, job_id, user_email, destination_table.project_id, destination_table.dataset_id, destination_table.table_id
    FROM `PROJECT_ID`.`region-REGION_NAME`.INFORMATION_SCHEMA.JOBS
    WHERE
    creation_time BETWEEN TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 2 DAY) AND CURRENT_TIMESTAMP()
    AND job_type = "COPY"
    order by creation_time DESC

    您也可以根據想查看的時間範圍調整時間間隔。

  • 如要查看所有地區的所有複製工作,請在 Cloud Logging 中使用下列篩選器:

    resource.type="bigquery_resource"
    protoPayload.methodName="jobservice.insert"
    protoPayload.serviceData.jobInsertRequest.resource.jobConfiguration.tableCopy:*
    

解析度

  • 如果頻繁複製作業的目的是建立資料快照,建議改用資料表快照。相較於複製完整資料表,資料表快照的費用較低,速度也較快。
  • 如要要求增加配額,請與支援團隊銷售團隊聯絡。審查及處理要求可能需要幾天的時間。建議在要求中說明優先順序、用途和專案 ID。

刪除資料表

您可以透過下列方式刪除資料表:

  • 使用 Google Cloud 控制台。
  • 使用資料定義語言 (DDL) DROP TABLE 陳述式。
  • 使用 bq 指令列工具 bq rm 指令。
  • 呼叫 tables.delete API 方法
  • 使用用戶端程式庫。

如要刪除資料集中的所有資料表,請刪除資料集

刪除資料表時,也會刪除資料表中的所有資料。如要讓系統在指定時間後自動刪除資料表,請設定資料集的預設資料表到期時間,或是在建立資料表時設定到期時間。

刪除資料表時,也會刪除與該資料表相關聯的所有權限。重新建立已刪除的資料表時,也必須手動重新設定先前與該資料表相關聯的存取權

必要的角色

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

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

所需權限

如要刪除表格,您必須具備下列權限:

  • bigquery.tables.delete
  • bigquery.tables.get

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

刪除資料表

如何刪除資料表:

控制台

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

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

  2. 在「Explorer」窗格中展開專案,點選「Datasets」(資料集),然後選取資料集。

  3. 依序按一下「總覽」>「表格」,然後選取所需表格。

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

  5. 在對話方塊中輸入 "delete",然後按一下「刪除」確認操作。

SQL

使用 DROP TABLE 陳述式。以下範例會刪除名為 mytable 的資料表:

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

    前往「BigQuery」

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

    DROP TABLE mydataset.mytable;

  3. 按一下「執行」

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

bq

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 請使用 bq rm 指令,搭配 --table 旗標 (或 -t 快速鍵) 來刪除資料表。使用 bq 指令列工具移除資料表時,必須確認該操作。您可以使用 --force 旗標 (或 -f 捷徑) 來略過確認程序。

    如果資料表位於非預設專案的資料集中,請使用下列格式將專案 ID 新增至資料集名稱:project_id:dataset

    bq rm \
    -f \
    -t \
    project_id:dataset.table

    更改下列內容:

    • project_id:專案 ID
    • dataset:含有資料表的資料集名稱
    • table:要刪除的資料表名稱

    範例:

    如要從 mydataset 資料集刪除 mytable 資料表,請輸入下列指令。mydataset 資料集位於預設專案中。

    bq rm -t mydataset.mytable
    

    如要從 mydataset 資料集刪除 mytable 資料表,請輸入下列指令。mydataset 資料集位於 myotherproject 專案,而非預設專案。

    bq rm -t myotherproject:mydataset.mytable
    

    如要從 mydataset 資料集刪除 mytable 資料表,請輸入下列指令。mydataset 資料集位於預設專案中。這個指令使用 -f 捷徑略過確認程序。

    bq rm -f -t mydataset.mytable
    
  3. API

    呼叫 tables.delete API 方法,並使用 tableId 參數指定要刪除的資料表。

    C#

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

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

    
    using Google.Cloud.BigQuery.V2;
    using System;
    
    public class BigQueryDeleteTable
    {
        public void DeleteTable(
            string projectId = "your-project-id",
            string datasetId = "your_dataset_id",
            string tableId = "your_table_id"
        )
        {
            BigQueryClient client = BigQueryClient.Create(projectId);
            client.DeleteTable(datasetId, tableId);
            Console.WriteLine($"Table {tableId} deleted.");
        }
    }

    Go

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

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

    import (
    	"context"
    	"fmt"
    
    	"cloud.google.com/go/bigquery"
    )
    
    // deleteTable demonstrates deletion of a BigQuery table.
    func deleteTable(projectID, datasetID, tableID string) error {
    	// projectID := "my-project-id"
    	// datasetID := "mydataset"
    	// tableID := "mytable"
    	ctx := context.Background()
    	client, err := bigquery.NewClient(ctx, projectID)
    	if err != nil {
    		return fmt.Errorf("bigquery.NewClient: %v", err)
    	}
    	defer client.Close()
    
    	table := client.Dataset(datasetID).Table(tableID)
    	if err := table.Delete(ctx); err != nil {
    		return err
    	}
    	return nil
    }
    

    Java

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

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

    import com.google.cloud.bigquery.BigQuery;
    import com.google.cloud.bigquery.BigQueryException;
    import com.google.cloud.bigquery.BigQueryOptions;
    import com.google.cloud.bigquery.TableId;
    
    public class DeleteTable {
    
      public static void runDeleteTable() {
        // TODO(developer): Replace these variables before running the sample.
        String datasetName = "MY_DATASET_NAME";
        String tableName = "MY_TABLE_NAME";
        deleteTable(datasetName, tableName);
      }
    
      public static void deleteTable(String datasetName, String tableName) {
        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();
          boolean success = bigquery.delete(TableId.of(datasetName, tableName));
          if (success) {
            System.out.println("Table deleted successfully");
          } else {
            System.out.println("Table was not found");
          }
        } catch (BigQueryException e) {
          System.out.println("Table was not deleted. \n" + e.toString());
        }
      }
    }

    Node.js

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

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

    // Import the Google Cloud client library
    const {BigQuery} = require('@google-cloud/bigquery');
    const bigquery = new BigQuery();
    
    async function deleteTable() {
      // Deletes "my_table" from "my_dataset".
    
      /**
       * TODO(developer): Uncomment the following lines before running the sample.
       */
      // const datasetId = "my_dataset";
      // const tableId = "my_table";
    
      // Delete the table
      await bigquery
        .dataset(datasetId)
        .table(tableId)
        .delete();
    
      console.log(`Table ${tableId} deleted.`);
    }

    PHP

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

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

    use Google\Cloud\BigQuery\BigQueryClient;
    
    /** Uncomment and populate these variables in your code */
    // $projectId = 'The Google project ID';
    // $datasetId = 'The BigQuery dataset ID';
    // $tableId = 'The BigQuery table ID';
    
    $bigQuery = new BigQueryClient([
        'projectId' => $projectId,
    ]);
    $dataset = $bigQuery->dataset($datasetId);
    $table = $dataset->table($tableId);
    $table->delete();
    printf('Deleted table %s.%s' . PHP_EOL, $datasetId, $tableId);

    Python

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

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

    
    from google.cloud import bigquery
    
    # Construct a BigQuery client object.
    client = bigquery.Client()
    
    # TODO(developer): Set table_id to the ID of the table to fetch.
    # table_id = 'your-project.your_dataset.your_table'
    
    # If the table does not exist, delete_table raises
    # google.api_core.exceptions.NotFound unless not_found_ok is True.
    client.delete_table(table_id, not_found_ok=True)  # Make an API request.
    print("Deleted table '{}'.".format(table_id))

    Ruby

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

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

    require "google/cloud/bigquery"
    
    def delete_table dataset_id = "my_dataset_id", table_id = "my_table_id"
      bigquery = Google::Cloud::Bigquery.new
      dataset  = bigquery.dataset dataset_id
      table    = dataset.table table_id
    
      table.delete
    
      puts "Table #{table_id} deleted."
    end

還原已刪除的資料表

如要瞭解如何還原或取消刪除資料表,請參閱還原已刪除的資料表

表格安全性

如要控管 BigQuery 資料表的存取權,請參閱「使用 IAM 控管資源存取權」。

後續步驟