Use ReadModifyWrite to increment a value and append a string to a value.
namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::StatusOr;
[](cbt::Table table, std::string const& row_key) {
StatusOr<cbt::Row> row = table.ReadModifyWriteRow(
row_key, cbt::ReadModifyWriteRule::IncrementAmount("fam", "counter", 1),
cbt::ReadModifyWriteRule::AppendValue("fam", "list", ";element"));
// As the modify in this example is not idempotent, and this example
// does not attempt to retry if there is a failure, we simply print
// such failures, if any, and otherwise ignore them.
if (!row) {
std::cout << "Failed to append row: " << row.status().message() << "\n";
return;
}
// Print the contents of the row
std::cout << row->row_key() << "\n";
for (auto const& cell : row->cells()) {
std::cout << " " << cell.family_name() << ":"
<< cell.column_qualifier() << " = <";
if (cell.column_qualifier() == "counter") {
// This example uses "counter" to store 64-bit numbers in big-endian
// format, extract them as follows:
std::cout << cell.decode_big_endian_integer<std::int64_t>().value();
} else {
std::cout << cell.value();
}
std::cout << ">\n";
}
}
Use the features to simplify reading big-endian values from Cloud Bigtable.
Use CheckAndMutate to modify a cell if it has a value.
namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::StatusOr;
[](cbt::Table table, std::string const& row_key) {
// Check if the latest value of the flip-flop column is "on".
cbt::Filter predicate = cbt::Filter::Chain(
cbt::Filter::ColumnRangeClosed("fam", "flip-flop", "flip-flop"),
cbt::Filter::Latest(1), cbt::Filter::ValueRegex("on"));
// If the predicate matches, change the latest value to "off", otherwise,
// change the latest value to "on". Modify the "flop-flip" column at the
// same time.
StatusOr<cbt::MutationBranch> branch =
table.CheckAndMutateRow(row_key, std::move(predicate),
{cbt::SetCell("fam", "flip-flop", "off"),
cbt::SetCell("fam", "flop-flip", "on")},
{cbt::SetCell("fam", "flip-flop", "on"),
cbt::SetCell("fam", "flop-flip", "off")});
if (!branch) throw std::move(branch).status();
if (*branch == cbt::MutationBranch::kPredicateMatched) {
std::cout << "The predicate was matched\n";
} else {
std::cout << "The predicate was not matched\n";
}
}
Use CheckAndMutate to modify a cell if another cell exists.
namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::StatusOr;
[](cbt::Table table, std::string const& row_key) {
// Check if the latest value of the "test-column" column is present,
// regardless of its value.
cbt::Filter predicate = cbt::Filter::Chain(
cbt::Filter::ColumnRangeClosed("fam", "test-column", "test-column"),
cbt::Filter::Latest(1));
// If the predicate matches, do nothing, otherwise set the
// "had-test-column" to "false":
StatusOr<cbt::MutationBranch> branch = table.CheckAndMutateRow(
row_key, std::move(predicate), {},
{cbt::SetCell("fam", "had-test-column", "false")});
if (!branch) throw std::move(branch).status();
if (*branch == cbt::MutationBranch::kPredicateMatched) {
std::cout << "The predicate was matched\n";
} else {
std::cout << "The predicate was not matched\n";
}
}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-14 UTC."],[[["\u003cp\u003eThis page provides access to various versions of the Cloud Bigtable C++ Client Library, ranging from version 2.11.0 to the latest release candidate 2.37.0-rc, including links to each version's documentation.\u003c/p\u003e\n"],["\u003cp\u003eThe document provides examples of advanced reading and writing operations on the Cloud Bigtable data client, including how to use \u003ccode\u003eReadModifyWrite\u003c/code\u003e to append or increment values.\u003c/p\u003e\n"],["\u003cp\u003eIt shows how to leverage features to simplify reading big-endian values directly from Cloud Bigtable, enhancing data interpretation.\u003c/p\u003e\n"],["\u003cp\u003eThe content illustrates the use of \u003ccode\u003eCheckAndMutate\u003c/code\u003e for conditional modifications, allowing a cell to be altered only if a specific condition is met, either by the presence or value of another cell.\u003c/p\u003e\n"],["\u003cp\u003eThe necessary steps are provided on how to connect to the Cloud Bigtable data client endpoint, as well as directions on where to find the instructions to run these examples on Github.\u003c/p\u003e\n"]]],[],null,["Version 2.33.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/bigtable/latest/bigtable-samples-data-client)\n- [2.41.0](/cpp/docs/reference/bigtable/2.41.0/bigtable-samples-data-client)\n- [2.40.0](/cpp/docs/reference/bigtable/2.40.0/bigtable-samples-data-client)\n- [2.39.0](/cpp/docs/reference/bigtable/2.39.0/bigtable-samples-data-client)\n- [2.38.0](/cpp/docs/reference/bigtable/2.38.0/bigtable-samples-data-client)\n- [2.37.0](/cpp/docs/reference/bigtable/2.37.0/bigtable-samples-data-client)\n- [2.36.0](/cpp/docs/reference/bigtable/2.36.0/bigtable-samples-data-client)\n- [2.35.0](/cpp/docs/reference/bigtable/2.35.0/bigtable-samples-data-client)\n- [2.34.0](/cpp/docs/reference/bigtable/2.34.0/bigtable-samples-data-client)\n- [2.33.0](/cpp/docs/reference/bigtable/2.33.0/bigtable-samples-data-client)\n- [2.32.0](/cpp/docs/reference/bigtable/2.32.0/bigtable-samples-data-client)\n- [2.31.0](/cpp/docs/reference/bigtable/2.31.0/bigtable-samples-data-client)\n- [2.30.0](/cpp/docs/reference/bigtable/2.30.0/bigtable-samples-data-client)\n- [2.29.0](/cpp/docs/reference/bigtable/2.29.0/bigtable-samples-data-client)\n- [2.28.0](/cpp/docs/reference/bigtable/2.28.0/bigtable-samples-data-client)\n- [2.27.0](/cpp/docs/reference/bigtable/2.27.0/bigtable-samples-data-client)\n- [2.26.0](/cpp/docs/reference/bigtable/2.26.0/bigtable-samples-data-client)\n- [2.25.1](/cpp/docs/reference/bigtable/2.25.1/bigtable-samples-data-client)\n- [2.24.0](/cpp/docs/reference/bigtable/2.24.0/bigtable-samples-data-client)\n- [2.23.0](/cpp/docs/reference/bigtable/2.23.0/bigtable-samples-data-client)\n- [2.22.1](/cpp/docs/reference/bigtable/2.22.1/bigtable-samples-data-client)\n- [2.21.0](/cpp/docs/reference/bigtable/2.21.0/bigtable-samples-data-client)\n- [2.20.0](/cpp/docs/reference/bigtable/2.20.0/bigtable-samples-data-client)\n- [2.19.0](/cpp/docs/reference/bigtable/2.19.0/bigtable-samples-data-client)\n- [2.18.0](/cpp/docs/reference/bigtable/2.18.0/bigtable-samples-data-client)\n- [2.17.0](/cpp/docs/reference/bigtable/2.17.0/bigtable-samples-data-client)\n- [2.16.0](/cpp/docs/reference/bigtable/2.16.0/bigtable-samples-data-client)\n- [2.15.1](/cpp/docs/reference/bigtable/2.15.1/bigtable-samples-data-client)\n- [2.14.0](/cpp/docs/reference/bigtable/2.14.0/bigtable-samples-data-client)\n- [2.13.0](/cpp/docs/reference/bigtable/2.13.0/bigtable-samples-data-client)\n- [2.12.0](/cpp/docs/reference/bigtable/2.12.0/bigtable-samples-data-client)\n- [2.11.0](/cpp/docs/reference/bigtable/2.11.0/bigtable-samples-data-client) \n\nAdvanced Reading and Writing Samples\n====================================\n\nThis example is for advanced reading and writing operations on bigtable data client, that illustrates how to:\n\n- Use ReadModifyWrite to append a string to a value.\n- Use ReadModifyWrite to increment a value.\n- Use the features to simplify reading big-endian values from Cloud Bigtable.\n- Use CheckAndMutate to modify a value only if it exists.\n\n### Run the example\n\nThis example uses the [Cloud Bigtable C++ Client Library](https://googleapis.github.io/google-cloud-cpp) to communicate with Cloud Bigtable.\n\nTo run the example program, follow the [instructions](https://github.com/googleapis/google-cloud-cpp/tree/main/google/cloud/bigtable/examples/) for the example on GitHub.\n\n#### Include the Necessary Headers\n\nThe example uses the following headers: \n\n #include \"google/cloud/bigtable/table.h\"\n\n#### Connect to the Cloud Bigtable data client endpoint.\n\n google::cloud::bigtable::Table table(\n google::cloud::bigtable::MakeDataConnection(),\n google::cloud::bigtable::TableResource(project_id, instance_id,\n table_id));\n\n#### Use ReadModifyWrite to increment a value and append a string to a value.\n\n namespace cbt = ::google::cloud::bigtable;\n using ::google::cloud::StatusOr;\n [](cbt::Table table, std::string const& row_key) {\n StatusOr\u003ccbt::Row\u003e row = table.ReadModifyWriteRow(\n row_key, cbt::ReadModifyWriteRule::IncrementAmount(\"fam\", \"counter\", 1),\n cbt::ReadModifyWriteRule::AppendValue(\"fam\", \"list\", \";element\"));\n\n // As the modify in this example is not idempotent, and this example\n // does not attempt to retry if there is a failure, we simply print\n // such failures, if any, and otherwise ignore them.\n if (!row) {\n std::cout \u003c\u003c \"Failed to append row: \" \u003c\u003c row.status().message() \u003c\u003c \"\\n\";\n return;\n }\n // Print the contents of the row\n std::cout \u003c\u003c row-\u003erow_key() \u003c\u003c \"\\n\";\n for (auto const& cell : row-\u003ecells()) {\n std::cout \u003c\u003c \" \" \u003c\u003c cell.family_name() \u003c\u003c \":\"\n \u003c\u003c cell.column_qualifier() \u003c\u003c \" = \u003c\";\n if (cell.column_qualifier() == \"counter\") {\n // This example uses \"counter\" to store 64-bit numbers in big-endian\n // format, extract them as follows:\n std::cout \u003c\u003c cell.decode_big_endian_integer\u003cstd::int64_t\u003e().value();\n } else {\n std::cout \u003c\u003c cell.value();\n }\n std::cout \u003c\u003c \"\u003e\\n\";\n }\n }\n\n#### Use the features to simplify reading big-endian values from Cloud Bigtable.\n\n namespace cbt = ::google::cloud::bigtable;\n using ::google::cloud::StatusOr;\n [](google::cloud::bigtable::Table table, std::string const& row_key) {\n StatusOr\u003cstd::pair\u003cbool, cbt::Row\u003e\u003e tuple = table.ReadRow(\n row_key, cbt::Filter::ColumnName(\"stats_summary\", \"os_build\"));\n if (!tuple) throw std::move(tuple).status();\n if (!tuple-\u003efirst) {\n std::cout \u003c\u003c \"Row \" \u003c\u003c row_key \u003c\u003c \" not found\\n\";\n return;\n }\n PrintRow(tuple-\u003esecond);\n }\n\n#### Use CheckAndMutate to modify a cell if it has a value.\n\n namespace cbt = ::google::cloud::bigtable;\n using ::google::cloud::StatusOr;\n [](cbt::Table table, std::string const& row_key) {\n // Check if the latest value of the flip-flop column is \"on\".\n cbt::Filter predicate = cbt::Filter::Chain(\n cbt::Filter::ColumnRangeClosed(\"fam\", \"flip-flop\", \"flip-flop\"),\n cbt::Filter::Latest(1), cbt::Filter::ValueRegex(\"on\"));\n // If the predicate matches, change the latest value to \"off\", otherwise,\n // change the latest value to \"on\". Modify the \"flop-flip\" column at the\n // same time.\n StatusOr\u003ccbt::MutationBranch\u003e branch =\n table.CheckAndMutateRow(row_key, std::move(predicate),\n {cbt::SetCell(\"fam\", \"flip-flop\", \"off\"),\n cbt::SetCell(\"fam\", \"flop-flip\", \"on\")},\n {cbt::SetCell(\"fam\", \"flip-flop\", \"on\"),\n cbt::SetCell(\"fam\", \"flop-flip\", \"off\")});\n\n if (!branch) throw std::move(branch).status();\n if (*branch == cbt::MutationBranch::kPredicateMatched) {\n std::cout \u003c\u003c \"The predicate was matched\\n\";\n } else {\n std::cout \u003c\u003c \"The predicate was not matched\\n\";\n }\n }\n\n#### Use CheckAndMutate to modify a cell if another cell exists.\n\n namespace cbt = ::google::cloud::bigtable;\n using ::google::cloud::StatusOr;\n [](cbt::Table table, std::string const& row_key) {\n // Check if the latest value of the \"test-column\" column is present,\n // regardless of its value.\n cbt::Filter predicate = cbt::Filter::Chain(\n cbt::Filter::ColumnRangeClosed(\"fam\", \"test-column\", \"test-column\"),\n cbt::Filter::Latest(1));\n // If the predicate matches, do nothing, otherwise set the\n // \"had-test-column\" to \"false\":\n StatusOr\u003ccbt::MutationBranch\u003e branch = table.CheckAndMutateRow(\n row_key, std::move(predicate), {},\n {cbt::SetCell(\"fam\", \"had-test-column\", \"false\")});\n\n if (!branch) throw std::move(branch).status();\n if (*branch == cbt::MutationBranch::kPredicateMatched) {\n std::cout \u003c\u003c \"The predicate was matched\\n\";\n } else {\n std::cout \u003c\u003c \"The predicate was not matched\\n\";\n }\n }"]]