Executar gravações em lote

Gravar várias linhas de uma vez. Esse tipo de gravação faz uma solicitação de API MutateRows.

Páginas de documentação que incluem esta amostra de código

Para visualizar o exemplo de código usado em contexto, consulte a seguinte documentação:

Exemplo de código

C#

Para saber como instalar e usar a biblioteca de cliente do Bigtable, consulte Bibliotecas de cliente do Bigtable.


using System;
using Google.Cloud.Bigtable.V2;
using Google.Cloud.Bigtable.Common.V2;

namespace Writes
{
    public class WriteBatch
    {
        /// <summary>
        /// Mutate multiple rows in an existing table and column family. Updates multiple cells within each row.
        ///</summary>
        /// <param name="projectId">Your Google Cloud Project ID.</param>
        /// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param>
        /// <param name="tableId">Your Google Cloud Bigtable table ID.</param>
        public string writeBatch(
            string projectId = "YOUR-PROJECT-ID",
            string instanceId = "YOUR-INSTANCE-ID",
            string tableId = "YOUR-TABLE-ID")
        {
            BigtableClient bigtableClient = BigtableClient.Create();

            TableName tableName = new TableName(projectId, instanceId, tableId);
            BigtableVersion timestamp = new BigtableVersion(DateTime.UtcNow);
            String COLUMN_FAMILY = "stats_summary";

            MutateRowsRequest.Types.Entry mutations1 = Mutations.CreateEntry(new BigtableByteString("tablet#a0b81f74#20190501"),
                Mutations.SetCell(COLUMN_FAMILY, "connected_cell", 1, timestamp),
                Mutations.SetCell(COLUMN_FAMILY, "os_build", "12155.0.0-rc1", timestamp)
            );
            MutateRowsRequest.Types.Entry mutations2 = Mutations.CreateEntry(new BigtableByteString("tablet#a0b81f74#20190502"),
                Mutations.SetCell(COLUMN_FAMILY, "connected_cell", 1, timestamp),
                Mutations.SetCell(COLUMN_FAMILY, "os_build", "12145.0.0-rc6", timestamp)
            );
            MutateRowsRequest.Types.Entry[] entries = {
                    mutations1,
                    mutations2
                };
            MutateRowsResponse mutateRowResponse = bigtableClient.MutateRows(tableName, entries);
            foreach (MutateRowsResponse.Types.Entry entry in mutateRowResponse.Entries)
            {
                if (entry.Status.Code == 0)
                {
                    Console.WriteLine($"Row {entry.Index} written successfully");
                }
                else
                {
                    Console.WriteLine($"\tFailed to write row {entry.Index}");
                    Console.WriteLine(entry.Status.Message);
                    return entry.Status.Message;
                }
            }
            return "Successfully wrote 2 rows";
        }
    }
}

C++

Para saber como instalar e usar a biblioteca de cliente do Bigtable, consulte Bibliotecas de cliente do Bigtable.

namespace cbt = ::google::cloud::bigtable;
[](cbt::Table table) {
  auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
      std::chrono::system_clock::now().time_since_epoch());
  std::string column_family = "stats_summary";

  cbt::BulkMutation bulk;
  bulk.emplace_back(cbt::SingleRowMutation(
      "tablet#a0b81f74#20190501",
      cbt::SetCell(column_family, "connected_cell", timestamp,
                   std::int64_t{1}),
      cbt::SetCell(column_family, "os_build", timestamp, "12155.0.0-rc1")));
  bulk.emplace_back(cbt::SingleRowMutation(
      "tablet#a0b81f74#20190502",
      cbt::SetCell(column_family, "connected_cell", timestamp,
                   std::int64_t{1}),
      cbt::SetCell(column_family, "os_build", timestamp, "12145.0.0-rc6")));

  std::vector<cbt::FailedMutation> failures =
      table.BulkApply(std::move(bulk));
  if (failures.empty()) {
    std::cout << "Successfully wrote 2 rows.\n";
    return;
  }
  std::cerr << "The following mutations failed:\n";
  for (auto const& f : failures) {
    std::cerr << "rowkey[" << f.original_index() << "]=" << f.status()
              << "\n";
  }
}

Go

Para saber como instalar e usar a biblioteca de cliente do Bigtable, consulte Bibliotecas de cliente do Bigtable.

import (
	"bytes"
	"context"
	"encoding/binary"
	"fmt"
	"io"

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

func writeBatch(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.NewAdminClient: %v", err)
	}
	defer client.Close()
	tbl := client.Open(tableName)
	columnFamilyName := "stats_summary"
	timestamp := bigtable.Now()

	var muts []*bigtable.Mutation

	binary1 := new(bytes.Buffer)
	binary.Write(binary1, binary.BigEndian, int64(1))

	mut := bigtable.NewMutation()
	mut.Set(columnFamilyName, "connected_wifi", timestamp, binary1.Bytes())
	mut.Set(columnFamilyName, "os_build", timestamp, []byte("12155.0.0-rc1"))
	muts = append(muts, mut)

	mut = bigtable.NewMutation()
	mut.Set(columnFamilyName, "connected_wifi", timestamp, binary1.Bytes())
	mut.Set(columnFamilyName, "os_build", timestamp, []byte("12145.0.0-rc6"))
	muts = append(muts, mut)

	rowKeys := []string{"tablet#a0b81f74#20190501", "tablet#a0b81f74#20190502"}
	if _, err := tbl.ApplyBulk(ctx, rowKeys, muts); err != nil {
		return fmt.Errorf("ApplyBulk: %v", err)
	}

	fmt.Fprintf(w, "Successfully wrote 2 rows: %s\n", rowKeys)
	return nil
}

Java

Para saber como instalar e usar a biblioteca de cliente do Bigtable, consulte Bibliotecas de cliente do Bigtable.


import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
import com.google.cloud.bigtable.data.v2.models.Mutation;
import com.google.protobuf.ByteString;

public class WriteBatch {
  private static final String COLUMN_FAMILY_NAME = "stats_summary";

  public static void writeBatch(String projectId, String instanceId, String tableId) {
    // String projectId = "my-project-id";
    // String instanceId = "my-instance-id";
    // String tableId = "mobile-time-series";

    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {
      long timestamp = System.currentTimeMillis() * 1000;

      BulkMutation bulkMutation =
          BulkMutation.create(tableId)
              .add(
                  "tablet#a0b81f74#20190501",
                  Mutation.create()
                      .setCell(
                          COLUMN_FAMILY_NAME,
                          ByteString.copyFrom("connected_wifi".getBytes()),
                          timestamp,
                          1)
                      .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1"))
              .add(
                  "tablet#a0b81f74#20190502",
                  Mutation.create()
                      .setCell(
                          COLUMN_FAMILY_NAME,
                          ByteString.copyFrom("connected_wifi".getBytes()),
                          timestamp,
                          1)
                      .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6"));

      dataClient.bulkMutateRows(bulkMutation);

      System.out.print("Successfully wrote 2 rows");
    } catch (Exception e) {
      System.out.println("Error during WriteBatch: \n" + e.toString());
    }
  }
}

Node.js

Para saber como instalar e usar a biblioteca de cliente do Bigtable, consulte Bibliotecas de cliente do Bigtable.

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const instanceId = 'YOUR_INSTANCE_ID';
// const tableId = 'YOUR_TABLE_ID';

const {Bigtable} = require('@google-cloud/bigtable');

const bigtable = new Bigtable();

async function writeBatch() {
  const instance = bigtable.instance(instanceId);
  const table = instance.table(tableId);

  const timestamp = new Date();
  const rowsToInsert = [
    {
      key: 'tablet#a0b81f74#20190501',
      data: {
        stats_summary: {
          connected_wifi: {
            value: 1,
            timestamp,
          },
          os_build: {
            value: '12155.0.0-rc1',
            timestamp,
          },
        },
      },
    },
    {
      key: 'tablet#a0b81f74#20190502',
      data: {
        stats_summary: {
          connected_wifi: {
            value: 1,
            timestamp,
          },
          os_build: {
            value: '12145.0.0-rc6',
            timestamp,
          },
        },
      },
    },
  ];

  await table.insert(rowsToInsert);

  console.log(
    `Successfully wrote 2 rows: ${rowsToInsert[0].key} and ${rowsToInsert[1].key}`
  );
}

writeBatch();

PHP

Para saber como instalar e usar a biblioteca de cliente do Bigtable, consulte Bibliotecas de cliente do Bigtable.

use Google\Cloud\Bigtable\BigtableClient;
use Google\Cloud\Bigtable\Mutations;

/**
 * Write data in batches in a table
 *
 * @param string $projectId The Google Cloud project ID
 * @param string $instanceId The ID of the Bigtable instance
 * @param string $tableId The ID of the table where the batch data needs to be written
 */
function write_batch(
    string $projectId,
    string $instanceId,
    string $tableId = 'mobile-time-series'
): void {
    // Connect to an existing table with an existing instance.
    $dataClient = new BigtableClient([
        'projectId' => $projectId,
    ]);
    $table = $dataClient->table($instanceId, $tableId);

    $timestampMicros = time() * 1000 * 1000;
    $columnFamilyId = 'stats_summary';
    $mutations = [
        (new Mutations())
            ->upsert($columnFamilyId, 'connected_wifi', 1, $timestampMicros)
            ->upsert($columnFamilyId, 'os_build', '12155.0.0-rc1', $timestampMicros),
        (new Mutations())
            ->upsert($columnFamilyId, 'connected_wifi', 1, $timestampMicros)
            ->upsert($columnFamilyId, 'os_build', '12145.0.0-rc6', $timestampMicros)];

    $table->mutateRows([
        'tablet#a0b81f74#20190501' => $mutations[0],
        'tablet#a0b81f74#20190502' => $mutations[1]
    ]);

    printf('Successfully wrote 2 rows.' . PHP_EOL);
}

Python

Para saber como instalar e usar a biblioteca de cliente do Bigtable, consulte Bibliotecas de cliente do Bigtable.

import datetime

from google.cloud import bigtable

def write_batch(project_id, instance_id, table_id):
    client = bigtable.Client(project=project_id, admin=True)
    instance = client.instance(instance_id)
    table = instance.table(table_id)

    timestamp = datetime.datetime.utcnow()
    column_family_id = "stats_summary"

    rows = [table.direct_row("tablet#a0b81f74#20190501"),
            table.direct_row("tablet#a0b81f74#20190502")]

    rows[0].set_cell(column_family_id,
                     "connected_wifi",
                     1,
                     timestamp)
    rows[0].set_cell(column_family_id,
                     "os_build",
                     "12155.0.0-rc1",
                     timestamp)
    rows[1].set_cell(column_family_id,
                     "connected_wifi",
                     1,
                     timestamp)
    rows[1].set_cell(column_family_id,
                     "os_build",
                     "12145.0.0-rc6",
                     timestamp)

    response = table.mutate_rows(rows)
    for i, status in enumerate(response):
        if status.code != 0:
            print("Error writing row: {}".format(status.message))

    print('Successfully wrote 2 rows.')

Ruby

Para saber como instalar e usar a biblioteca de cliente do Bigtable, consulte Bibliotecas de cliente do Bigtable.

# instance_id = "my-instance"
# table_id    = "my-table"
table = bigtable.table instance_id, table_id
column_family = "stats_summary"
timestamp = (Time.now.to_f * 1_000_000).round(-3)

entries = []
entries << table.new_mutation_entry("tablet#a0b81f74#20190501")
                .set_cell(column_family, "connected_cell", 1, timestamp: timestamp)
                .set_cell(column_family, "os_build", "12155.0.0-rc1", timestamp: timestamp)
entries << table.new_mutation_entry("tablet#a0b81f74#20190502")
                .set_cell(column_family, "connected_cell", 1, timestamp: timestamp)
                .set_cell(column_family, "os_build", "12155.0.0-rc6", timestamp: timestamp)

results = table.mutate_rows entries
puts "Successfully wrote #{results.length} rows"

A seguir

Para pesquisar e filtrar exemplos de código de outros produtos do Google Cloud, consulte o navegador de exemplos do Google Cloud.