Perform batch writes

Write multiple rows at once. This type of write makes a MutateRows API request.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C++

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

C#

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


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

Go

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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
}

Java

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


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

Node.js

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

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

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

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

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

Python

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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.")

Ruby

To learn how to install and use the client library for Bigtable, see Bigtable client libraries.

To authenticate to Bigtable, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

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

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.