This example is for advanced reading and writing operations on bigtable data client, that illustrates how to:
- Use ReadModifyWrite to append a string to a value.
- Use ReadModifyWrite to increment a value.
- Use the features to simplify reading big-endian values from Cloud Bigtable.
- Use CheckAndMutate to modify a value only if it exists.
Run the example
This example uses the Cloud Bigtable C++ Client Library to communicate with Cloud Bigtable.
To run the example program, follow the instructions for the example on GitHub.
Include the Necessary Headers
The example uses the following headers:
#include "google/cloud/bigtable/table.h"
Connect to the Cloud Bigtable data client endpoint.
table_id));
This class identifies a Cloud Bigtable Table.
Definition: table_resource.h:44
The main interface to interact with data in a Cloud Bigtable table.
Definition: table.h:166
std::shared_ptr< DataConnection > MakeDataConnection(Options options={})
Returns a DataConnection object that can be used for interacting with the Cloud Bigtable Data API.
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"));
if (!row) {
std::cout << "Failed to append row: " << row.status().message() << "\n";
return;
}
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") {
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.
namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::StatusOr;
StatusOr<std::pair<bool, cbt::Row>> tuple = table.
ReadRow(
row_key, cbt::Filter::ColumnName("stats_summary", "os_build"));
if (!tuple) throw std::move(tuple).status();
if (!tuple->first) {
std::cout << "Row " << row_key << " not found\n";
return;
}
PrintRow(tuple->second);
}
StatusOr< std::pair< bool, Row > > ReadRow(std::string row_key, Filter filter, Options opts={})
Read and return a single row from the table.
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) {
cbt::Filter predicate = cbt::Filter::Chain(
cbt::Filter::ColumnRangeClosed("fam", "flip-flop", "flip-flop"),
cbt::Filter::Latest(1), cbt::Filter::ValueRegex("on"));
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) {
cbt::Filter predicate = cbt::Filter::Chain(
cbt::Filter::ColumnRangeClosed("fam", "test-column", "test-column"),
cbt::Filter::Latest(1));
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";
}
}