Escrever exemplos

Este documento mostra exemplos de código que ilustram os vários tipos de solicitações de gravação que você pode enviar ao Bigtable ao usar as bibliotecas de cliente do Cloud Bigtable.

Os exemplos desta página usam mutações SetCell para enviar solicitações de gravação a células não agregadas. Para conferir exemplos de como enviar solicitações de adição para células agregadas, consulte Agrupar seus dados no momento da gravação.

Antes de testar estes exemplos, entenda quando usar ou não usar cada tipo de solicitação de gravação.

A biblioteca de cliente do Python para Bigtable oferece duas APIs, asyncio e uma API síncrona. Se o aplicativo for assíncrono, use asyncio.

Executar uma gravação simples

Os exemplos de código a seguir demonstram como fazer solicitações de gravação simples ao Bigtable. Esse tipo de gravação faz uma solicitação de API MutateRow.

Go

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

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

	"bytes"

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

func writeSimple(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)
	columnFamilyName := "stats_summary"
	timestamp := bigtable.Now()

	mut := bigtable.NewMutation()
	buf := new(bytes.Buffer)
	binary.Write(buf, binary.BigEndian, int64(1))

	mut.Set(columnFamilyName, "connected_cell", timestamp, buf.Bytes())
	mut.Set(columnFamilyName, "connected_wifi", timestamp, buf.Bytes())
	mut.Set(columnFamilyName, "os_build", timestamp, []byte("PQ2A.190405.003"))

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

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

HBase

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.


import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class WriteSimple {

  private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary");

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

    try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
      final Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
      long timestamp = System.currentTimeMillis();
      byte[] one = new byte[]{0, 0, 0, 0, 0, 0, 0, 1};

      String rowKey = "phone#4c410523#20190501";
      Put put = new Put(Bytes.toBytes(rowKey));

      put.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("connected_cell"), timestamp, one);
      put.addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, one);
      put.addColumn(
          COLUMN_FAMILY_NAME,
          Bytes.toBytes("os_build"),
          timestamp,
          Bytes.toBytes("PQ2A.190405.003"));
      table.put(put);

      System.out.printf("Successfully wrote row %s", rowKey);

    } catch (Exception e) {
      System.out.println("Error during WriteSimple: \n" + e.toString());
    }
  }
}

Java

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.


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 com.google.protobuf.ByteString;

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

  public static void writeSimple(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;

      String rowkey = "phone#4c410523#20190501";

      RowMutation rowMutation =
          RowMutation.create(TableId.of(tableId), rowkey)
              .setCell(
                  COLUMN_FAMILY_NAME,
                  ByteString.copyFrom("connected_cell".getBytes()),
                  timestamp,
                  1)
              .setCell(
                  COLUMN_FAMILY_NAME,
                  ByteString.copyFrom("connected_wifi".getBytes()),
                  timestamp,
                  1)
              .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "PQ2A.190405.003");

      dataClient.mutateRow(rowMutation);
      System.out.printf("Successfully wrote row %s", rowkey);

    } catch (Exception e) {
      System.out.println("Error during WriteSimple: \n" + e.toString());
    }
  }
}

Python asyncio

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data import SetCell

async def write_simple(project_id, instance_id, table_id):
    async with BigtableDataClientAsync(project=project_id) as client:
        async with client.get_table(instance_id, table_id) as table:
            family_id = "stats_summary"
            row_key = b"phone#4c410523#20190501"

            cell_mutation = SetCell(family_id, "connected_cell", 1)
            wifi_mutation = SetCell(family_id, "connected_wifi", 1)
            os_mutation = SetCell(family_id, "os_build", "PQ2A.190405.003")

            await table.mutate_row(row_key, cell_mutation)
            await table.mutate_row(row_key, wifi_mutation)
            await table.mutate_row(row_key, os_mutation)

Python

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

import datetime

from google.cloud import bigtable


def write_simple(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"

    row_key = "phone#4c410523#20190501"

    row = table.direct_row(row_key)
    row.set_cell(column_family_id, "connected_cell", 1, timestamp)
    row.set_cell(column_family_id, "connected_wifi", 1, timestamp)
    row.set_cell(column_family_id, "os_build", "PQ2A.190405.003", timestamp)

    row.commit()

    print("Successfully wrote row {}.".format(row_key))

C#

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.


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

namespace Writes
{
    public class WriteSimpleSample
    {
        /// <summary>
        /// Mutate one row in an existing table and column family. Updates multiple cells within that row using one API call.
        ///</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 WriteSimple(
            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);
            BigtableByteString rowkey = new BigtableByteString("phone#4c410523#20190501");
            BigtableVersion timestamp = new BigtableVersion(DateTime.UtcNow);
            string COLUMN_FAMILY = "stats_summary";

            Mutation[] mutations = {
                    Mutations.SetCell(COLUMN_FAMILY, "connected_cell", 1, timestamp),
                    Mutations.SetCell(COLUMN_FAMILY, "connected_wifi", 1, timestamp),
                    Mutations.SetCell(COLUMN_FAMILY, "os_build", "PQ2A.190405.003", timestamp)
                };
            MutateRowResponse mutateRowResponse = bigtableClient.MutateRow(tableName, rowkey, mutations);
            Console.WriteLine(mutateRowResponse);
            return $"Successfully wrote row {rowkey}";
        }
    }
}

C++

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

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 row_key = "phone#4c410523#20190501";
  cbt::SingleRowMutation mutation(row_key);
  std::string column_family = "stats_summary";

  mutation.emplace_back(cbt::SetCell(column_family, "connected_cell",
                                     timestamp, std::int64_t{1}));
  mutation.emplace_back(cbt::SetCell(column_family, "connected_wifi",
                                     timestamp, std::int64_t{1}));
  mutation.emplace_back(
      cbt::SetCell(column_family, "os_build", timestamp, "PQ2A.190405.003"));
  google::cloud::Status status = table.Apply(std::move(mutation));
  if (!status.ok()) throw std::runtime_error(status.message());
  std::cout << "Successfully wrote row" << row_key << "\n";
}

Node.js

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

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

async function writeSimple() {
  /**
   * TODO(developer): Uncomment these variables before running the sample.
   */
  // const instanceId = 'YOUR_INSTANCE_ID';
  // const tableId = 'YOUR_TABLE_ID';
  const instance = bigtable.instance(instanceId);
  const table = instance.table(tableId);

  const timestamp = new Date();
  const rowToInsert = {
    key: 'phone#4c410523#20190501',
    data: {
      stats_summary: {
        connected_cell: {
          value: 1,
          timestamp,
        },
        connected_wifi: {
          value: 1,
          timestamp,
        },
        os_build: {
          value: 'PQ2A.190405.003',
          timestamp,
        },
      },
    },
  };

  await table.insert(rowToInsert);

  console.log(`Successfully wrote row ${rowToInsert.key}`);
}

writeSimple();

PHP

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

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

/**
 * Write data 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 data needs to be written
 */
function write_simple(
    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_cell', '1', $timestampMicros)
    ->upsert($columnFamilyId, 'connected_wifi', DataUtil::intToByteString(1), $timestampMicros)
    ->upsert($columnFamilyId, 'os_build', 'PQ2A.190405.003', $timestampMicros);

    $table->mutateRow('phone#4c410523#20190501', $mutations);

    printf('Successfully wrote row.' . PHP_EOL);
}

Ruby

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

# 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)

rowkey = "phone#4c410523#20190501"
entry = table.new_mutation_entry(rowkey)
             .set_cell(column_family, "connected_cell", 1, timestamp: timestamp)
             .set_cell(column_family, "connected_wifi", 1, timestamp: timestamp)
             .set_cell(column_family, "os_build", "PQ2A.190405.003", timestamp: timestamp)

table.mutate_row entry
puts "Successfully wrote row #{rowkey}"

Incrementar um valor

Os exemplos de código a seguir demonstram como enviar uma solicitação de gravação que incrementa ou decrementa um valor numérico em uma célula agregada. Esse tipo de gravação faz uma solicitação MutateRow com um tipo de mutação AddToCell. Os exemplos somam uma soma em um grupo de colunas que espera o tipo de entrada Int64.

cbt

cbt addtocell TABLE_ID ROW_KEY
FAMILY_NAME:COLUMN_QUALIFER=VALUE@TIMESTAMP

Substitua:

  • TABLE_ID: identificador permanente da tabela;
  • ROW_KEY: a chave de linha
  • FAMILY_NAME: o nome do grupo de colunas agregado
  • COLUMN_QUALIFIER: um identificador da coluna
  • VALUE: o valor a ser adicionado à célula
  • TIMESTAMP: um carimbo de data/hora Unix em microssegundos, como 1710868850000000

Neste exemplo, o valor armazenado na coluna week12 é decrementado em 100 na linha device-1:

cbt addtocell mobile-data device-1 updates:week12=-100@1710868850000000

Go

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

import (
	"context"
	"fmt"
	"io"
	"time"

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

func writeAggregate(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)
	columnFamilyName := "view_count"
	viewTimestamp, err := time.Parse(time.RFC3339, "2024-03-13T12:41:34Z")
	if err != nil {
		return err
	}
	hourlyBucket := viewTimestamp.Truncate(time.Hour)

	mut := bigtable.NewMutation()
	mut.AddIntToCell(columnFamilyName, "views", bigtable.Time(hourlyBucket), 1)

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

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

Java

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.


import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
import com.google.common.primitives.Longs;
import com.google.protobuf.ByteString;
import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class WriteAggregate {
  private static final String COUNT_COLUMN_FAMILY_NAME = "view_count";
  private static final long MICROS_PER_MILLI = 1000;

  public static void writeAggregate(String projectId, String instanceId, String tableId) {
    // String projectId = "my-project-id";
    // String instanceId = "my-instance-id";
    // String tableId = "page-view-counter";

    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {

      String rowKey = "page#index.html";
      Instant viewTimestamp = Instant.parse("2024-03-13T12:41:34.123Z");

      // Bucket the views for an hour into a single count, giving us an hourly view count for a
      // given page.
      Instant hourlyBucket = viewTimestamp.truncatedTo(ChronoUnit.HOURS);
      long hourlyBucketMicros = hourlyBucket.toEpochMilli() * MICROS_PER_MILLI;

      RowMutation rowMutation =
          RowMutation.create(tableId, rowKey)
              .addToCell(COUNT_COLUMN_FAMILY_NAME, "views", hourlyBucketMicros, 1);

      dataClient.mutateRow(rowMutation);
      System.out.printf("Successfully wrote row %s", rowKey);

    } catch (Exception e) {
      System.out.println("Error during WriteAggregate: \n" + e.toString());
    }
  }

  public static void mergeAggregate(String projectId, String instanceId, String tableId) {
    // String projectId = "my-project-id";
    // String instanceId = "my-instance-id";
    // String tableId = "page-view-counter";

    try (BigtableDataClient dataClient = BigtableDataClient.create(projectId, instanceId)) {

      String rowKey = "page#index.html";
      Instant viewTimestamp = Instant.parse("2024-03-13T12:41:34.123Z");

      // Bucket the views for an hour into a single count, giving us an hourly view count for a
      // given page.
      Instant hourlyBucket = viewTimestamp.truncatedTo(ChronoUnit.HOURS);
      long hourlyBucketMicros = hourlyBucket.toEpochMilli() * MICROS_PER_MILLI;

      RowMutation rowMutation =
          RowMutation.create(tableId, rowKey)
              .mergeToCell(
                  COUNT_COLUMN_FAMILY_NAME,
                  "views",
                  hourlyBucketMicros,
                  ByteString.copyFrom(Longs.toByteArray(1L)));

      dataClient.mutateRow(rowMutation);
      System.out.printf("Successfully wrote row %s", rowKey);

    } catch (Exception e) {
      System.out.println("Error during mergeAggregate: \n" + e.toString());
    }
  }
}

Gravar um valor condicionalmente

Os exemplos de código a seguir demonstram como enviar uma solicitação de gravação condicional, que verifica uma linha em busca de uma condição e, dependendo do resultado, grava dados nessa linha. Esse tipo de gravação faz uma solicitação de API CheckAndMutateRow.

Go

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

import (
	"context"
	"fmt"
	"io"

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

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

	mut := bigtable.NewMutation()
	mut.Set(columnFamilyName, "os_name", timestamp, []byte("android"))

	filter := bigtable.ChainFilters(
		bigtable.FamilyFilter(columnFamilyName),
		bigtable.ColumnFilter("os_build"),
		bigtable.ValueFilter("PQ2A\\..*"))
	conditionalMutation := bigtable.NewCondMutation(filter, mut, nil)

	rowKey := "phone#4c410523#20190501"
	if err := tbl.Apply(ctx, rowKey, conditionalMutation); err != nil {
		return fmt.Errorf("Apply: %w", err)
	}

	fmt.Fprintln(w, "Successfully updated row's os_name")
	return nil
}

HBase

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.


import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.RowMutations;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;

public class WriteConditionally {

  private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("stats_summary");

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

    try (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
      Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
      long timestamp = System.currentTimeMillis();

      String rowKey = "phone#4c410523#20190501";
      RowMutations mutations = new RowMutations(Bytes.toBytes(rowKey));

      Put put = new Put(Bytes.toBytes(rowKey));
      put.addColumn(
          COLUMN_FAMILY_NAME, Bytes.toBytes("os_name"), timestamp, Bytes.toBytes("android"));
      mutations.add(put);

      table.checkAndMutate(
          Bytes.toBytes(rowKey),
          COLUMN_FAMILY_NAME,
          Bytes.toBytes("os_build"),
          CompareOp.GREATER_OR_EQUAL,
          Bytes.toBytes("PQ2A.190405"),
          mutations);

      System.out.print("Successfully updated row's os_name");

    } catch (Exception e) {
      System.out.println("Error during WriteConditionally: \n" + e.toString());
      e.printStackTrace();
    }
  }
}

Java

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.


import static com.google.cloud.bigtable.data.v2.models.Filters.FILTERS;

import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation;
import com.google.cloud.bigtable.data.v2.models.Filters.Filter;
import com.google.cloud.bigtable.data.v2.models.Mutation;
import com.google.cloud.bigtable.data.v2.models.TableId;

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

  public static void writeConditionally(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;

      String rowkey = "phone#4c410523#20190501";

      Mutation mutation =
          Mutation.create().setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "android");

      Filter filter =
          FILTERS
              .chain()
              .filter(FILTERS.family().exactMatch(COLUMN_FAMILY_NAME))
              .filter(FILTERS.qualifier().exactMatch("os_build"))
              .filter(FILTERS.value().regex("PQ2A\\..*"));

      ConditionalRowMutation conditionalRowMutation =
          ConditionalRowMutation.create(TableId.of(tableId), rowkey)
              .condition(filter)
              .then(mutation);

      boolean success = dataClient.checkAndMutateRow(conditionalRowMutation);

      System.out.printf("Successfully updated row's os_name: %b", success);

    } catch (Exception e) {
      System.out.println("Error during WriteConditionally: \n" + e.toString());
      e.printStackTrace();
    }
  }
}

Python asyncio

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data import row_filters
from google.cloud.bigtable.data import SetCell

async def write_conditional(project_id, instance_id, table_id):
    async with BigtableDataClientAsync(project=project_id) as client:
        async with client.get_table(instance_id, table_id) as table:
            family_id = "stats_summary"
            row_key = "phone#4c410523#20190501"

            row_filter = row_filters.RowFilterChain(
                filters=[
                    row_filters.FamilyNameRegexFilter(family_id),
                    row_filters.ColumnQualifierRegexFilter("os_build"),
                    row_filters.ValueRegexFilter("PQ2A\\..*"),
                ]
            )

            if_true = SetCell(family_id, "os_name", "android")
            result = await table.check_and_mutate_row(
                row_key,
                row_filter,
                true_case_mutations=if_true,
                false_case_mutations=None,
            )
            if result is True:
                print("The row os_name was set to android")

Python

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

import datetime

from google.cloud import bigtable
from google.cloud.bigtable import row_filters


def write_conditional(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"

    row_key = "phone#4c410523#20190501"

    row_filter = row_filters.RowFilterChain(
        filters=[
            row_filters.FamilyNameRegexFilter(column_family_id),
            row_filters.ColumnQualifierRegexFilter("os_build"),
            row_filters.ValueRegexFilter("PQ2A\\..*"),
        ]
    )
    row = table.conditional_row(row_key, filter_=row_filter)
    row.set_cell(column_family_id, "os_name", "android", timestamp)
    row.commit()

    print("Successfully updated row's os_name.")

C#

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.


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

namespace Writes
{
    public class WriteConditionalSample
    {
        /// <summary>
        /// Check if a row has a certain value then mutate the row if it does.
        ///</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 WriteConditional(
            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);
            BigtableByteString rowkey = new BigtableByteString("phone#4c410523#20190501");
            BigtableVersion timestamp = new BigtableVersion(DateTime.UtcNow);
            string COLUMN_FAMILY = "stats_summary";

            CheckAndMutateRowResponse checkAndMutateRowResponse = bigtableClient.CheckAndMutateRow(
                tableName,
                rowkey,
                RowFilters.Chain(
                    RowFilters.FamilyNameExact(COLUMN_FAMILY),
                    RowFilters.ColumnQualifierExact("os_build"),
                    RowFilters.ValueRegex("PQ2A\\..*")),
                Mutations.SetCell(COLUMN_FAMILY, "os_name", "android", timestamp));

            return $"Successfully updated row's os_name: {checkAndMutateRowResponse.PredicateMatched}";
        }
    }
}

C++

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

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 row_key = "phone#4c410523#20190501";
  cbt::SingleRowMutation mutation(row_key);
  std::string column_family = "stats_summary";
  cbt::Filter predicate = cbt::Filter::Chain(
      cbt::Filter::ColumnName(column_family, "os_build"),
      cbt::Filter::Latest(1), cbt::Filter::ValueRegex("PQ2A\\..*"));

  google::cloud::StatusOr<cbt::MutationBranch> branch =
      table.CheckAndMutateRow(
          row_key, std::move(predicate),
          {cbt::SetCell(column_family, "os_name", timestamp, "android")}, {});

  if (!branch) throw std::move(branch).status();
  if (*branch == cbt::MutationBranch::kPredicateMatched) {
    std::cout << "Successfully updated row\n";
  } else {
    std::cout << "The predicate was not matched\n";
  }
}

Node.js

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

/**
 * 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 writeConditionally() {
  const instance = bigtable.instance(instanceId);
  const table = instance.table(tableId);

  const timestamp = new Date();
  const row = table.row('phone#4c410523#20190501');
  const filter = [
    {
      column: 'os_build',
      value: {
        start: 'PQ2A',
        end: 'PQ2A',
      },
    },
  ];

  const config = {
    onMatch: [
      {
        method: 'insert',
        data: {
          stats_summary: {
            os_name: 'android',
            timestamp,
          },
        },
      },
    ],
  };

  await row.filter(filter, config);

  console.log("Successfully updated row's os_name");
}

writeConditionally();

PHP

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

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

/**
 * Write data conditionally 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 data needs to be written
 */
function write_conditionally(
    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, 'os_name', 'android', $timestampMicros);
    $predicateFilter = Filter::chain()
    ->addFilter(Filter::family()->exactMatch($columnFamilyId))
    ->addFilter(Filter::qualifier()->exactMatch('os_build'))
    ->addFilter(Filter::value()->regex('PQ2A.*'));
    $options = ['predicateFilter' => $predicateFilter, 'trueMutations' => $mutations];

    $table->checkAndMutateRow('phone#4c410523#20190501', $options);

    printf('Successfully updated row\'s os_name' . PHP_EOL);
}

Ruby

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

# 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)

rowkey = "phone#4c410523#20190501"
predicate_filter = Google::Cloud::Bigtable::RowFilter.chain
                                                     .family(column_family)
                                                     .qualifier("os_build")
                                                     .value("PQ2A\\..*")

on_match_mutations = Google::Cloud::Bigtable::MutationEntry.new
on_match_mutations.set_cell(
  column_family,
  "os_name",
  "android",
  timestamp: timestamp
)

response = table.check_and_mutate_row(
  rowkey,
  predicate_filter,
  on_match: on_match_mutations
)

puts "Successfully updated row's os_name: #{response}"

Executar gravações em lote

Os exemplos de código a seguir demonstram como fazer solicitações de gravação em lote ao Bigtable. Esse tipo de gravação faz uma solicitação de API MutateRows.

Go

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

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: %w", 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: %w", err)
	}

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

HBase

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.


import com.google.cloud.bigtable.hbase.BigtableConfiguration;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class WriteBatch {

  private static final byte[] COLUMN_FAMILY_NAME = Bytes.toBytes("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 (Connection connection = BigtableConfiguration.connect(projectId, instanceId)) {
      final Table table = connection.getTable(TableName.valueOf(Bytes.toBytes(tableId)));
      long timestamp = System.currentTimeMillis();
      byte[] one = new byte[]{0, 0, 0, 0, 0, 0, 0, 1};

      List<Put> puts = new ArrayList<Put>();
      puts.add(new Put(Bytes.toBytes("tablet#a0b81f74#20190501")));
      puts.add(new Put(Bytes.toBytes("tablet#a0b81f74#20190502")));

      puts.get(0).addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, one);
      puts.get(0)
          .addColumn(
              COLUMN_FAMILY_NAME,
              Bytes.toBytes("os_build"),
              timestamp,
              Bytes.toBytes("12155.0.0-rc1"));

      puts.get(1).addColumn(COLUMN_FAMILY_NAME, Bytes.toBytes("connected_wifi"), timestamp, one);
      puts.get(1)
          .addColumn(
              COLUMN_FAMILY_NAME,
              Bytes.toBytes("os_build"),
              timestamp,
              Bytes.toBytes("12145.0.0-rc6"));

      table.put(puts);

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

Java

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.


import com.google.api.core.ApiFuture;
import com.google.api.gax.batching.Batcher;
import com.google.api.gax.batching.BatchingException;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
import com.google.cloud.bigtable.data.v2.models.TableId;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

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)) {
      List<ApiFuture<Void>> batchFutures = new ArrayList<>();
      try (Batcher<RowMutationEntry, Void> batcher =
          dataClient.newBulkMutationBatcher(TableId.of(tableId))) {
        long timestamp = System.currentTimeMillis() * 1000;
        batchFutures.add(
            batcher.add(
                RowMutationEntry.create("tablet#a0b81f74#20190501")
                    .setCell(
                        COLUMN_FAMILY_NAME, ByteString.copyFromUtf8("connected_wifi"), timestamp, 1)
                    .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1")));
        batchFutures.add(
            batcher.add(
                RowMutationEntry.create("tablet#a0b81f74#20190502")
                    .setCell(
                        COLUMN_FAMILY_NAME, ByteString.copyFromUtf8("connected_wifi"), timestamp, 1)
                    .setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6")));

        // Blocks until mutations are applied on all submitted row entries.
        // flush will be called automatically when a batch is full.
        batcher.flush();
        // Before batcher is closed, all remaining (if any) mutations are applied.
      } catch (BatchingException batchingException) {
        System.out.println(
            "At least one entry failed to apply. Summary of the errors: \n" + batchingException);
        // get individual entry error details
        for (ApiFuture<Void> future : batchFutures) {
          try {
            future.get();
          } catch (ExecutionException entryException) {
            System.out.println("Entry failure: " + entryException.getCause());
          } catch (InterruptedException e) {
            // handle interrupted exception
          }
        }
      }
      System.out.println("Successfully wrote 2 rows");
    } catch (Exception e) {
      System.out.println("Error during WriteBatch: \n" + e);
    }
  }
}

Python asyncio

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

from google.cloud.bigtable.data import BigtableDataClientAsync
from google.cloud.bigtable.data.mutations import SetCell
from google.cloud.bigtable.data.mutations import RowMutationEntry
from google.cloud.bigtable.data.exceptions import MutationsExceptionGroup

async def write_batch(project_id, instance_id, table_id):
    async with BigtableDataClientAsync(project=project_id) as client:
        async with client.get_table(instance_id, table_id) as table:
            family_id = "stats_summary"
            try:
                async with table.mutations_batcher() as batcher:
                    mutation_list = [
                        SetCell(family_id, "connected_cell", 1),
                        SetCell(family_id, "connected_wifi", 1),
                        SetCell(family_id, "os_build", "12155.0.0-rc1"),
                    ]
                    # awaiting the batcher.append method adds the RowMutationEntry
                    # to the batcher's queue to be written in the next flush.
                    await batcher.append(
                        RowMutationEntry("tablet#a0b81f74#20190501", mutation_list)
                    )
                    await batcher.append(
                        RowMutationEntry("tablet#a0b81f74#20190502", mutation_list)
                    )
            except MutationsExceptionGroup as e:
                # MutationsExceptionGroup contains a FailedMutationEntryError for
                # each mutation that failed.
                for sub_exception in e.exceptions:
                    failed_entry: RowMutationEntry = sub_exception.entry
                    cause: Exception = sub_exception.__cause__
                    print(
                        f"Failed mutation: {failed_entry.row_key} with error: {cause!r}"
                    )

Python

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

import datetime

from google.cloud import bigtable
from google.cloud.bigtable.batcher import MutationsBatcher


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)

    with MutationsBatcher(table=table) as batcher:
        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)

        batcher.mutate_rows(rows)

    print("Successfully wrote 2 rows.")

C#

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.


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

namespace Writes
{
    public class WriteBatchSample
    {
        /// <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.

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

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";
  }
}

Node.js

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

/**
 * 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.

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

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);
}

Ruby

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

# 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"

Ativar o controle de fluxo de gravação em lote

Os snippets de código abaixo demonstram como ativar o controle de fluxo de gravação em lote ao enviar gravações em lote para o Bigtable. Esse recurso está disponível no Conector Bigtable Beam (BigtableIO) e no Conector Bigtable HBase Beam (CloudBigtableIO). Para conferir o exemplo completo, incluindo as instruções de importação, clique em e em Ver no GitHub.

BigtableIO

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

mutations.apply(
    String.format("Write data to table %s via BigtableIO", options.getBigtableTableId()),
    BigtableIO.write()
        .withProjectId(options.getProject())
        .withInstanceId(options.getBigtableInstanceId())
        .withTableId(options.getBigtableTableId())
        .withFlowControl(true) // This enables batch write flow control
);

CloudBigtableIO

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

mutations.apply(
    String.format("Write data to table %s via CloudBigtableIO", options.getBigtableTableId()),
    CloudBigtableIO.writeToTable(
        new CloudBigtableTableConfiguration.Builder()
            .withProjectId(options.getProject())
            .withInstanceId(options.getBigtableInstanceId())
            .withTableId(options.getBigtableTableId())
            .withConfiguration(
                BigtableOptionsFactory.BIGTABLE_ENABLE_BULK_MUTATION_FLOW_CONTROL, "true")
            .build()));

Escrever em uma visualização autorizada

O exemplo a seguir mostra como enviar uma solicitação de gravação para uma visualização autorizada. A sintaxe é semelhante à gravação em uma tabela, exceto que você também precisa fornecer o ID de visualização autorizado.

Java

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

Para autenticar no Bigtable, configure o Application Default Credentials. Para mais informações, acesse Configurar a autenticação para bibliotecas de cliente.

try {
  System.out.println("\nWriting to authorized view");
  String[] names = {"World", "Bigtable", "Java"};
  for (int i = 0; i < names.length; i++) {
    String greeting = "Hello " + names[i] + "!";
    RowMutation rowMutation =
        RowMutation.create(AuthorizedViewId.of(tableId, authorizedViewId), ROW_KEY_PREFIX + i)
            .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_NAME, names[i])
            .setCell(COLUMN_FAMILY, COLUMN_QUALIFIER_GREETING, greeting);
    dataClient.mutateRow(rowMutation);
    System.out.println(greeting);
  }
} catch (Exception e) {
  if (e instanceof NotFoundException) {
    System.err.println("Failed to write to non-existent authorized view: " + e.getMessage());
  } else if (e instanceof PermissionDeniedException) {
    System.err.println(
        "Failed to apply mutations outside of the authorized view: " + e.getMessage());
  }
}

A seguir