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.

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;
namespace cbta = ::google::cloud::bigtable_admin;
using ::google::bigtable::admin::v2::ModifyColumnFamiliesRequest;
using ::google::cloud::StatusOr;
[](cbta::BigtableTableAdminClient admin, std::string const& project_id,
   std::string const& instance_id, std::string const& table_id,
   std::string const& family_name) {
  std::string table_name = cbt::TableName(project_id, instance_id, table_id);

  google::bigtable::admin::v2::GetTableRequest r;

  StatusOr<google::bigtable::admin::v2::Table> schema =

  if (!schema) throw std::move(schema).status();
  auto pos = schema->column_families().find(family_name);
  if (pos == schema->column_families().end()) {
    // Try to create the column family instead:
    ModifyColumnFamiliesRequest::Modification mod;

    auto modified = admin.ModifyColumnFamilies(table_name, {std::move(mod)});
    if (!modified) throw std::move(schema).status();
    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.