刪除次數

本文說明如何刪除儲存在 Bigtable 資料表中的資料、討論各種方法的適用時機,並提供範例。閱讀本頁面前,請先熟悉 Bigtable 總覽,並瞭解結構定義設計涉及的概念。

為求一致,本頁說明會提及每種要求類型使用的 API 方法。不過,強烈建議您一律使用其中一個 Bigtable 用戶端程式庫存取 Bigtable API,而非使用 REST 或 RPC。

本頁範例使用的樣本資料,與您可能儲存在 Bigtable 的資料類似。

如要瞭解本頁所述作業的每日使用次數,請參閱「配額與限制」。

Bigtable 如何刪除資料

傳送刪除要求後,系統會將儲存格標示為待刪除,且無法讀取。資料會在壓縮期間 (持續最佳化資料表的背景程序) 移除,最多需要一週。刪除中繼資料可能會導致資料在您傳送刪除要求後的幾天內,占用稍微多一點的空間 (每列幾 KB),直到下次壓縮為止。

即使叢集超過儲存空間上限,且讀取和寫入作業遭到封鎖,您仍可傳送刪除要求。

刪除多個資料列

如要刪除儲存在連續資料列中的大量資料,請使用 dropRowRange。這項作業會刪除由起始和結束資料列或資料列索引鍵前置字串識別的資料列範圍內的所有資料列。

刪除一系列資料列時,您提供的資料列索引鍵值會視為服務資料。如要瞭解服務資料的處理方式,請參閱《Cloud de Confiance 隱私權聲明》。

成功刪除並收到回應後,您就可以放心地將資料寫入相同的資料列範圍。

dropRowRange 作業有下列限制:

  • 您無法從授權檢視表捨棄一系列資料列。
  • 您無法以非同步方式呼叫 dropRowRange 方法。如果您在另一個要求進行時,向資料表傳送 dropRowRange 要求,Bigtable 會傳回 UNAVAILABLE 錯誤,並顯示 A DropRowRange operation is already ongoing 訊息。如要解決錯誤,請再次傳送要求。
  • 如果執行個體使用複製功能,請注意,Bigtable 可能需要較長時間才能完成作業,因為複製延遲時間和 CPU 使用率會增加。如要從使用複製功能的執行個體中刪除資料,請使用 Data API 讀取並刪除資料。

下列程式碼範例說明如何捨棄以資料列索引鍵前置字元 phone#5c10102 開頭的一系列資料列:

Go

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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


import (
	"context"
	"fmt"
	"io"

	admin "cloud.google.com/go/bigtable/admin/apiv2"
	"cloud.google.com/go/bigtable/admin/apiv2/adminpb"
)

func dropRowRange(w io.Writer, projectID, instanceID, tableName string) error {
	ctx := context.Background()
	adminClient, err := admin.NewBigtableTableAdminClient(ctx)
	if err != nil {
		return fmt.Errorf("admin.NewBigtableTableAdminClient: %w", err)
	}
	defer adminClient.Close()

	// Use a specific row key prefix to drop.
	prefix := "phone#4c410523"
	req := &adminpb.DropRowRangeRequest{
		Name:   fmt.Sprintf("projects/%s/instances/%s/tables/%s", projectID, instanceID, tableName),
		Target: &adminpb.DropRowRangeRequest_RowKeyPrefix{RowKeyPrefix: []byte(prefix)},
	}
	if err := adminClient.DropRowRange(ctx, req); err != nil {
		return fmt.Errorf("adminClient.DropRowRange: %w", err)
	}

	fmt.Fprintf(w, "Successfully dropped row range with prefix: %s\n", prefix)
	return nil
}

Java

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

import com.google.bigtable.admin.v2.DropRowRangeRequest;
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClientV2;
import com.google.protobuf.ByteString;
import java.io.IOException;

public class DropRowRangeExample {
  public void dropRowRange(String projectId, String instanceId, String tableId) throws IOException {
    try (BigtableTableAdminClientV2 tableAdminClient = BigtableTableAdminClientV2.create()) {
      DropRowRangeRequest request =
          DropRowRangeRequest.newBuilder()
              .setName("projects/" + projectId + "/instances/" + instanceId + "/tables/" + tableId)
              .setRowKeyPrefix(ByteString.copyFromUtf8("phone#4c410523"))
              .build();
      tableAdminClient.dropRowRange(request);
    }
  }
}

Python

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

def drop_row_range(project_id, instance_id, table_id):
    from google.cloud import bigtable_admin_v2

    client = bigtable_admin_v2.BigtableTableAdminClient()
    table_path = client.table_path(project_id, instance_id, table_id)
    row_key_prefix = b"phone#4c410523"
    client.drop_row_range(
        request={
            "name": table_path,
            "row_key_prefix": row_key_prefix,
        },
        timeout=200,
    )

Node.js

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

await table.deleteRows('phone#5c10102');
await printRows();

使用 Data API 方法刪除資料

如要刪除少量不連續的資料,通常最適合的方法是呼叫 Cloud Bigtable API (Data API)。如果要求刪除的資料量為 MB (而非 GB),請使用這些方法。如要刪除資料欄 (而非資料欄系列) 中的資料,只能使用 Data API。

Data API 方法呼叫 MutateRows 會使用下列三種異動類型之一:

  • DeleteFromColumn
  • DeleteFromFamily
  • DeleteFromRow

使用 Data API 提出的刪除要求是不可分割的:要不就是要求成功,所有資料都刪除;要不就是要求失敗,沒有任何資料移除。

在大多數情況下,請避免使用 CheckAndMutate 方法刪除資料。在極少數情況下,如果您需要同步一致性,可以採用這種方法,但請注意,這種方法會耗用大量資源,且可能會影響效能。

如要使用 MutateRows 刪除資料,請傳送 readRows 要求,並使用篩選器決定要刪除的內容,然後傳送刪除要求。如需可用篩選器的清單,請參閱「篩選器」。

本節中的範例假設您已決定要刪除哪些資料。

刪除資料欄

下列程式碼範例示範如何刪除資料列中某個資料欄的所有儲存格:

Go

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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


import (
	"context"
	"fmt"
	"io"

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

func deleteFromColumn(w io.Writer, projectID, instanceID, tableName string) error {
	ctx := context.Background()
	client, err := bigtable.NewClient(ctx, projectID, instanceID)
	if err != nil {
		return fmt.Errorf("bigtable.NewClient: %w", err)
	}
	defer client.Close()
	tbl := client.Open(tableName)

	// Use a specific row key, column family, and column name that exists in your table.
	// This sample assumes a schema with a "cell_plan" column family.
	rowKey := "phone#4c410523#20190501"
	columnFamilyName := "cell_plan"
	columnName := "data_plan_01gb"
	mut := bigtable.NewMutation()
	mut.DeleteCellsInColumn(columnFamilyName, columnName)

	if err := tbl.Apply(ctx, rowKey, mut); err != nil {
		return fmt.Errorf("tbl.Apply: %w", err)
	}

	fmt.Fprintf(w, "Successfully deleted cells from column %s:%s for row: %s\n", columnFamilyName, columnName, rowKey)
	return nil
}

Java

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.Mutation;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;

public class DeleteFromColumnExample {
  public void deleteFromColumnCells(String projectId, String instanceId, String tableId)
      throws IOException {
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
      Mutation mutation = Mutation.create().deleteCells("cell_plan", "data_plan_01gb");
      dataClient.mutateRow(
          RowMutation.create(TableId.of(tableId), "phone#4c410523#20190501", mutation));
    }
  }
}

Python

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

def delete_from_column(project_id, instance_id, table_id):
    from google.cloud.bigtable.data import (
        BigtableDataClient,
        DeleteRangeFromColumn,
    )

    client = BigtableDataClient(project=project_id)
    table = client.get_table(instance_id, table_id)

    table.mutate_row(
        "phone#4c410523#20190501",
        DeleteRangeFromColumn(family="cell_plan", qualifier=b"data_plan_01gb"),
    )

    table.close()
    client.close()

Python asyncio

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

async def delete_from_column(project_id, instance_id, table_id):
    from google.cloud.bigtable.data import (
        BigtableDataClientAsync,
        DeleteRangeFromColumn,
    )

    client = BigtableDataClientAsync(project=project_id)
    table = client.get_table(instance_id, table_id)

    await table.mutate_row(
        "phone#4c410523#20190501",
        DeleteRangeFromColumn(family="cell_plan", qualifier=b"data_plan_01gb"),
    )

    await table.close()
    await client.close()

Node.js

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

await table.mutate({
  key: 'phone#4c410523#20190501',
  method: 'delete',
  data: {
    column: 'cell_plan:data_plan_05gb',
  },
});
await printRows();

從資料欄系列中刪除

下列程式碼範例示範如何從資料列中的資料欄系列刪除儲存格:

Go

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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


import (
	"context"
	"fmt"
	"io"

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

func deleteFromColumnFamily(w io.Writer, projectID, instanceID, tableName string) error {
	ctx := context.Background()
	client, err := bigtable.NewClient(ctx, projectID, instanceID)
	if err != nil {
		return fmt.Errorf("bigtable.NewClient: %w", err)
	}
	defer client.Close()
	tbl := client.Open(tableName)

	// Use a specific row key and column family that exists in your table.
	// This sample assumes a schema with a "stats_summary" column family.
	rowKey := "phone#5c10102#20190501"
	columnFamilyName := "stats_summary"
	mut := bigtable.NewMutation()
	mut.DeleteCellsInFamily(columnFamilyName)

	if err := tbl.Apply(ctx, rowKey, mut); err != nil {
		return fmt.Errorf("tbl.Apply: %w", err)
	}

	fmt.Fprintf(w, "Successfully deleted cells from family %s for row: %s\n", columnFamilyName, rowKey)
	return nil
}

Java

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;

public class DeleteFromColumnFamilyExample {
  public void deleteFromColumnFamily(String projectId, String instanceId, String tableId)
      throws IOException {
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
      dataClient.mutateRow(
          RowMutation.create(TableId.of(tableId), "phone#5c10102#20190501")
              .deleteFamily("stats_summary"));
    }
  }
}

Python

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

def delete_from_column_family(project_id, instance_id, table_id):
    from google.cloud.bigtable.data import BigtableDataClient, DeleteAllFromFamily

    client = BigtableDataClient(project=project_id)
    table = client.get_table(instance_id, table_id)

    table.mutate_row("phone#4c410523#20190501", DeleteAllFromFamily("cell_plan"))

    table.close()
    client.close()

Python asyncio

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

async def delete_from_column_family(project_id, instance_id, table_id):
    from google.cloud.bigtable.data import BigtableDataClientAsync, DeleteAllFromFamily

    client = BigtableDataClientAsync(project=project_id)
    table = client.get_table(instance_id, table_id)

    await table.mutate_row("phone#4c410523#20190501", DeleteAllFromFamily("cell_plan"))

    await table.close()
    await client.close()

Node.js

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

await table.mutate({
  key: 'phone#4c410523#20190501',
  method: 'delete',
  data: {
    column: 'cell_plan',
  },
});
await printRows();

從資料列中刪除

下列程式碼片段示範如何刪除資料列中的所有儲存格:

Go

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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


import (
	"context"
	"fmt"
	"io"

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

func deleteFromRow(w io.Writer, projectID, instanceID, tableName string) error {
	ctx := context.Background()
	client, err := bigtable.NewClient(ctx, projectID, instanceID)
	if err != nil {
		return fmt.Errorf("bigtable.NewClient: %w", err)
	}
	defer client.Close()
	tbl := client.Open(tableName)

	// Use a specific row key that exists in your table.
	rowKey := "phone#4c410523#20190501"
	mut := bigtable.NewMutation()
	mut.DeleteRow()

	if err := tbl.Apply(ctx, rowKey, mut); err != nil {
		return fmt.Errorf("tbl.Apply: %w", err)
	}

	fmt.Fprintf(w, "Successfully deleted row: %s\n", rowKey)
	return nil
}

Java

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.Mutation;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;

public class DeleteFromRowExample {
  public void deleteFromRow(String projectId, String instanceId, String tableId)
      throws IOException {
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
      Mutation mutation = Mutation.create().deleteRow();
      dataClient.mutateRow(
          RowMutation.create(TableId.of(tableId), "phone#4c410523#20190501", mutation));
    }
  }
}

Python

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

def delete_from_row(project_id, instance_id, table_id):
    from google.cloud.bigtable.data import BigtableDataClient, DeleteAllFromRow

    client = BigtableDataClient(project=project_id)
    table = client.get_table(instance_id, table_id)

    table.mutate_row("phone#4c410523#20190501", DeleteAllFromRow())

    table.close()
    client.close()

Python asyncio

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

async def delete_from_row(project_id, instance_id, table_id):
    from google.cloud.bigtable.data import BigtableDataClientAsync, DeleteAllFromRow

    client = BigtableDataClientAsync(project=project_id)
    table = client.get_table(instance_id, table_id)

    await table.mutate_row("phone#4c410523#20190501", DeleteAllFromRow())

    await table.close()
    await client.close()

Node.js

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

const row = table.row('phone#4c410523#20190501');
await row.delete();
await printRows();

透過串流和批次處理刪除

如要刪除大量資料,通常最好的做法是串流及批次處理刪除要求。如果您的資料保留需求比垃圾收集政策允許的更精細,這種策略就非常實用。

如果應用程式是以 Java 編寫,您可以在傳送批次刪除作業至 Bigtable 時,啟用批次寫入流量控管功能。詳情請參閱「批次寫入流量控制」和「啟用批次寫入流量控制」。

下列程式碼範例會啟動資料串流 (讀取資料列)、批次處理資料,然後逐一檢查批次,並刪除 cell_plan 資料欄系列中 data_plan_01gb1 資料欄的所有儲存格:

Go

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

// Copyright 2025 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.

package deletes


import (
	"context"
	"fmt"
	"io"

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

// streamingAndBatching starts a stream of data (reading rows), batches them, and then goes through the batch and deletes all the cells in column
func streamingAndBatching(w io.Writer, projectID, instanceID string, tableName string) error {
	// projectID := "my-project-id"
	// instanceID := "my-instance-id"
	// tableName := "mobile-time-series"

	ctx := context.Background()
	client, err := bigtable.NewClient(ctx, projectID, instanceID)
	if err != nil {
		return fmt.Errorf("bigtable.NewClient: %w", err)
	}
	defer client.Close()
	tbl := client.Open(tableName)

	// Slices to hold the row keys and the corresponding mutations.
	var rowKeys []string
	var mutations []*bigtable.Mutation

	// Read all rows from the table.
	err = tbl.ReadRows(ctx, bigtable.InfiniteRange(""), func(row bigtable.Row) bool {
		// For each row, create a mutation to delete the specified cell.
		mut := bigtable.NewMutation()
		mut.DeleteCellsInColumn("cell_plan", "data_plan_01gb")

		// Append the row key and mutation to the slices.
		rowKeys = append(rowKeys, row.Key())
		mutations = append(mutations, mut)

		// Continue processing rows.
		return true
	})
	if err != nil {
		return fmt.Errorf("tbl.ReadRows: %w", err)
	}

	if len(mutations) == 0 {
		return nil
	}
	// If there are mutations to apply, apply them in a single bulk request.
	// ApplyBulk returns a slice of errors, one for each mutation.
	var errs []error
	if errs, err = tbl.ApplyBulk(ctx, rowKeys, mutations); err != nil {
		return fmt.Errorf("tbl.ApplyBulk: %w", err)
	}
	if errs != nil {
		// Log any individual errors that occurred during the bulk operation.
		var errorCount int
		for _, individualErr := range errs {
			if individualErr != nil {
				fmt.Fprintf(w, "Error applying mutation: %v\n", individualErr)
				errorCount++
			}
		}
		if errorCount > 0 {
			return fmt.Errorf("encountered %d error(s) out of %d mutations", errorCount, len(errs))
		}
	}

	fmt.Fprintf(w, "Successfully deleted cells from all rows")
	return nil
}

Java

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

import com.google.api.gax.batching.Batcher;
import com.google.api.gax.rpc.ServerStream;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
import com.google.cloud.bigtable.data.v2.models.TableId;
import java.io.IOException;

public class BatchDeleteExample {
  public void batchDelete(String projectId, String instanceId, String tableId)
      throws InterruptedException, IOException {
    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
      try (Batcher<RowMutationEntry, Void> batcher =
          dataClient.newBulkMutationBatcher(TableId.of(tableId))) {
        ServerStream<Row> rows = dataClient.readRows(Query.create(TableId.of(tableId)));
        for (Row row : rows) {
          batcher.add(
              RowMutationEntry.create(row.getKey()).deleteCells("cell_plan", "data_plan_05gb"));
        }
        // Blocks until mutations are applied on all submitted row entries.
        batcher.flush();
      }
    }
  }
}

Python

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

def streaming_and_batching(project_id, instance_id, table_id):
    from google.cloud.bigtable.data import (
        BigtableDataClient,
        DeleteRangeFromColumn,
        ReadRowsQuery,
        RowMutationEntry,
    )

    client = BigtableDataClient(project=project_id)
    table = client.get_table(instance_id, table_id)

    with table.mutations_batcher() as batcher:
        for row in table.read_rows_stream(ReadRowsQuery(limit=10)):
            batcher.append(
                RowMutationEntry(
                    row.row_key,
                    DeleteRangeFromColumn(
                        family="cell_plan", qualifier=b"data_plan_01gb"
                    ),
                )
            )

    table.close()
    client.close()

Python asyncio

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

async def streaming_and_batching(project_id, instance_id, table_id):
    from google.cloud.bigtable.data import (
        BigtableDataClientAsync,
        DeleteRangeFromColumn,
        ReadRowsQuery,
        RowMutationEntry,
    )

    client = BigtableDataClientAsync(project=project_id)
    table = client.get_table(instance_id, table_id)

    async with table.mutations_batcher() as batcher:
        async for row in await table.read_rows_stream(ReadRowsQuery(limit=10)):
            await batcher.append(
                RowMutationEntry(
                    row.row_key,
                    DeleteRangeFromColumn(
                        family="cell_plan", qualifier=b"data_plan_01gb"
                    ),
                )
            )

    await table.close()
    await client.close()

Node.js

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱「Bigtable 用戶端程式庫」。

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

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

const rows = (await table.getRows({limit: 2}))[0];
const entries = rows.map(row => {
  return {
    key: row.id,
    method: 'delete',
    data: {
      column: 'cell_plan:data_plan_05gb',
    },
  };
});
await table.mutate(entries);
await printRows();

刪除授權檢視表中的資料

您可以傳送刪除要求至授權檢視表,刪除資料表資料。您必須使用下列其中一種方式:

  • gcloud CLI
  • Java 適用的 Bigtable 用戶端

從授權檢視表刪除資料時,除了資料表 ID 之外,您還須提供授權檢視表 ID。

您可以從授權檢視表刪除的資料,受限於授權檢視表定義。您只能刪除授權檢視表中的資料。如果您嘗試刪除授權檢視表定義以外的資料,或受下列規則限制的資料,系統會傳回 PERMISSION_DENIED 錯誤:

  • 不支援使用 Admin API 中的 DropRowRange 刪除授權檢視表中的一組資料列。
  • 不支援從資料列中刪除。
  • 只要資料列位於授權檢視表中,即可從資料欄中刪除。
  • 只有在指定的資料欄系列已設定為允許授權檢視表中的所有資料欄限定詞前置字串 (qualifier_prefixes="") 時,才能從資料欄系列中刪除。

舉例來說,如果您嘗試從指定資料列中刪除資料,而該資料列包含基礎資料表中的資料欄,但這些資料欄不在授權檢視表中,則要求會失敗。

後續步驟