取得或建立資料欄系列

如果資料欄系列不存在,則自動建立。

程式碼範例

C++

如要瞭解如何安裝及使用 Bigtable 的用戶端程式庫,請參閱這篇文章

如要向 Bigtable 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

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;
  r.set_name(table_name);
  r.set_view(google::bigtable::admin::v2::Table::FULL);

  StatusOr<google::bigtable::admin::v2::Table> schema =
      admin.GetTable(std::move(r));

  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;
    mod.set_id(family_name);
    mod.mutable_create()->mutable_gc_rule()->set_max_num_versions(5);

    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";
}

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽器