Get or create a table

Get table details if it exists, or autocreate a table if it does not exist.

Code sample

C++

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) {
  StatusOr<google::bigtable::admin::v2::Table> table =
      admin.GetTable(table_id, cbt::TableAdmin::FULL);
  if (!table &&
      table.status().code() == google::cloud::StatusCode::kNotFound) {
    // The table does not exist, try to create the table.
    table = admin.CreateTable(
        table_id,
        cbt::TableConfig({{"fam", cbt::GcRule::MaxNumVersions(10)}}, {}));
    if (!table) throw std::runtime_error(table.status().message());
    // The schema returned by a `CreateTable()` request does not include all
    // the metadata for a table, we need to explicitly request the rest:
    table = admin.GetTable(table_id, cbt::TableAdmin::FULL);
  }
  if (!table) throw std::runtime_error(table.status().message());
  std::cout << "Table metadata: " << table->DebugString() << "\n";
}

What's next

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