Get or create a column family

Autocreate a column family if it does not exist.

Code sample


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

namespace cbt = ::google::cloud::bigtable;
using ::google::cloud::StatusOr;
[](cbt::TableAdmin admin, std::string const& table_id,
   std::string const& family_name) {
  StatusOr<google::bigtable::admin::v2::Table> schema =
      admin.GetTable(table_id, cbt::TableAdmin::FULL);

  if (!schema) throw std::runtime_error(schema.status().message());
  auto pos = schema->column_families().find(family_name);
  if (pos == schema->column_families().end()) {
    // Try to create the column family instead:
    auto modified = admin.ModifyColumnFamilies(
            family_name, cbt::GcRule::MaxAge(std::chrono::hours(5 * 24)))});

    if (!modified) throw std::runtime_error(schema.status().message());
    schema = *std::move(modified);
    pos = schema->column_families().find(family_name);

  if (pos == schema->column_families().end()) {
    throw std::runtime_error("GetOrCreateFamily failed");

  google::bigtable::admin::v2::ColumnFamily family = pos->second;
  std::cout << "Column family name: " << pos->first
            << "\nColumn family details: " << family.DebugString() << "\n";

What's next

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