Como gravar dados

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

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

Como executar uma gravação simples

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

Go

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

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

HBase


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


import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.RowMutation;
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";
      ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1});

      RowMutation rowMutation =
          RowMutation.create(tableId, rowKey)
              .setCell(
                  COLUMN_FAMILY_NAME,
                  ByteString.copyFrom("connected_cell".getBytes()),
                  timestamp,
                  one)
              .setCell(
                  COLUMN_FAMILY_NAME,
                  ByteString.copyFrom("connected_wifi".getBytes()),
                  timestamp,
                  one)
              .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

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


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

namespace Writes
{
    public class WriteSimple
    {
        /// <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++

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

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

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

  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


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

/** Uncomment and populate these variables in your code */
// $project_id = 'The Google project ID';
// $instance_id = 'The Bigtable instance ID';
// $table_id = 'mobile-time-series';

// Connect to an existing table with an existing instance.
$dataClient = new BigtableClient([
    'projectId' => $project_id,
]);
$table = $dataClient->table($instance_id, $table_id);

$timestamp = time() * 1000;
$columnFamilyId = 'stats_summary';
$mutations = (new Mutations())
    ->upsert($columnFamilyId, "connected_cell", 1, $timestamp)
    ->upsert($columnFamilyId, "connected_wifi", DataUtil::intToByteString(1), $timestamp)
    ->upsert($columnFamilyId, "os_build", "PQ2A.190405.003", $timestamp);

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

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

Ruby

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

Como incrementar um valor existente

Os exemplos de código a seguir demonstram como enviar uma solicitação de gravação que incrementa um valor numérico existente. Esse tipo de gravação faz uma solicitação de API ReadModifyWriteRow.

Go

import (
	"context"
	"fmt"
	"io"

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

func writeIncrement(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)
	}
	tbl := client.Open(tableName)
	columnFamilyName := "stats_summary"

	increment := bigtable.NewReadModifyWrite()
	increment.Increment(columnFamilyName, "connected_wifi", -1)

	rowKey := "phone#4c410523#20190501"
	if _, err := tbl.ApplyReadModifyWrite(ctx, rowKey, increment); err != nil {
		return fmt.Errorf("ApplyReadModifyWrite: %v", err)
	}

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

HBase


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.Table;
import org.apache.hadoop.hbase.util.Bytes;

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

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

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

      table.incrementColumnValue(
          Bytes.toBytes(rowKey), COLUMN_FAMILY_NAME, Bytes.toBytes("connected_cell"), -1);

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

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

Java


import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.models.ReadModifyWriteRow;
import com.google.cloud.bigtable.data.v2.models.Row;
import java.nio.charset.Charset;

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

  public static void writeIncrement(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)) {
      // Get an existing row that has a cell with an incrementable value. A value can be incremented
      // if it is encoded as a 64-bit big-endian signed integer.
      String rowKey = "phone#4c410523#20190501";
      ReadModifyWriteRow mutation =
          ReadModifyWriteRow.create(tableId, rowKey)
              .increment(COLUMN_FAMILY_NAME, "connected_cell", -1);
      Row success = dataClient.readModifyWriteRow(mutation);

      System.out.printf(
          "Successfully updated row %s", success.getKey().toString(Charset.defaultCharset()));
    } catch (Exception e) {
      System.out.println("Error during WriteIncrement: \n" + e.toString());
    }
  }
}

Python

from google.cloud import bigtable

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

    column_family_id = "stats_summary"

    row_key = "phone#4c410523#20190501"
    row = table.row(row_key, append=True)

    # Decrement the connected_wifi value by 1.
    row.increment_cell_value(column_family_id, "connected_wifi", -1)
    row.commit()

    print('Successfully updated row {}.'.format(row_key))

C#


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

namespace Writes
{
    public class WriteIncrement
    {
        /// <summary>
        /// Increments a cell value in a row with an existing value at that cell.
        ///</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 writeIncrement(
            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");
            String COLUMN_FAMILY = "stats_summary";

            // Increment the value of stats_summary:connected_wifi by -1 (change 1 to 0 to show it's disconnected)
            ReadModifyWriteRowResponse readModifyWriteRowResponse = bigtableClient.ReadModifyWriteRow(
                tableName,
                rowkey,
                ReadModifyWriteRules.Increment(COLUMN_FAMILY, "connected_wifi", -1));
            return $"Successfully updated row {readModifyWriteRowResponse.Row.Key}";
        }
    }
}

C++

namespace cbt = google::cloud::bigtable;
[](cbt::Table table) {
  std::string row_key = "phone#4c410523#20190501";
  std::string column_family = "stats_summary";

  google::cloud::StatusOr<cbt::Row> row = table.ReadModifyWriteRow(
      row_key, cbt::ReadModifyWriteRule::IncrementAmount(
                   column_family, "connected_wifi", -1));

  if (!row) {
    throw std::runtime_error(row.status().message());
  }
  std::cout << "Successfully updated row" << row->row_key() << "\n";
}

Node.js

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

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

const bigtable = Bigtable();

async function writeIncrement() {
  const instance = bigtable.instance(instanceId);
  const table = instance.table('mobile-time-series');

  const row = table.row('phone#4c410523#20190501');
  await row.increment('stats_summary:connected_wifi', -1);

  console.log(`Successfully updated row ${row}`);
}

writeIncrement();

PHP


use Google\Cloud\Bigtable\BigtableClient;
use Google\Cloud\Bigtable\ReadModifyWriteRowRules;

/** Uncomment and populate these variables in your code */
// $project_id = 'The Google project ID';
// $instance_id = 'The Bigtable instance ID';
// $table_id = 'mobile-time-series';

// Connect to an existing table with an existing instance.
$dataClient = new BigtableClient([
    'projectId' => $project_id,
]);
$table = $dataClient->table($instance_id, $table_id);

$timestamp = time() * 1000;
$columnFamilyId = 'stats_summary';

$rules = (new ReadModifyWriteRowRules)->increment($columnFamilyId, 'connected_wifi', -1);
$row = $table->readModifyWriteRow('phone#4c410523#20190501', $rules);

printf('Successfully updated row.' . PHP_EOL);

Ruby

table = bigtable.table instance_id, table_id
column_family = "stats_summary"

rowkey = "phone#4c410523#20190501"
decrement_rule = table.new_read_modify_write_rule(column_family, "connected_wifi")
                      .increment(-1)

row = table.read_modify_write_row rowkey, decrement_rule
puts "Successfully updated row #{row.key}"

Como 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

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

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

HBase


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


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;

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, 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

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


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

namespace Writes
{
    public class WriteConditional
    {
        /// <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++

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::runtime_error(branch.status().message());
  }
  if (*branch == cbt::MutationBranch::kPredicateMatched) {
    std::cout << "Successfully updated row\n";
  } else {
    std::cout << "The predicate was not matched\n";
  }
}

Node.js

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

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

const bigtable = Bigtable();

async function writeConditionally() {
  const instance = bigtable.instance(instanceId);
  const table = instance.table('mobile-time-series');

  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


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

/** Uncomment and populate these variables in your code */
// $project_id = 'The Google project ID';
// $instance_id = 'The Bigtable instance ID';
// $table_id = 'mobile-time-series';

// Connect to an existing table with an existing instance.
$dataClient = new BigtableClient([
    'projectId' => $project_id,
]);
$table = $dataClient->table($instance_id, $table_id);

$timestamp = time() * 1000;
$columnFamilyId = 'stats_summary';

$mutations = (new Mutations())->upsert($columnFamilyId, "os_name", "android", $timestamp);
$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

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

Como executar gravações em lote

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

Go

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

HBase


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


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;
      ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1});

      BulkMutation bulkMutation =
          BulkMutation.create(tableId)
              .add(
                  "tablet#a0b81f74#20190501",
                  Mutation.create()
                      .setCell(
                          COLUMN_FAMILY_NAME,
                          ByteString.copyFrom("connected_wifi".getBytes()),
                          timestamp,
                          one)
                      .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,
                          one)
                      .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());
    }
  }
}

Python

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.row("tablet#a0b81f74#20190501"),
            table.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.')

C#


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++

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

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

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

const bigtable = Bigtable();

async function writeBatch() {
  const instance = bigtable.instance(instanceId);
  const table = instance.table('mobile-time-series');

  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


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

/** Uncomment and populate these variables in your code */
// $project_id = 'The Google project ID';
// $instance_id = 'The Bigtable instance ID';
// $table_id = 'mobile-time-series';

// Connect to an existing table with an existing instance.
$dataClient = new BigtableClient([
    'projectId' => $project_id,
]);
$table = $dataClient->table($instance_id, $table_id);

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

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

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

Ruby

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

Esta página foi útil? Conte sua opinião sobre:

Enviar comentários sobre…

Documentação do Cloud Bigtable