Tabelle abrufen oder erstellen

Rufen Sie die Tabellendetails ab, wenn diese vorhanden sind, oder erstellen Sie automatisch eine Tabelle, falls sie noch nicht vorhanden ist.

Codebeispiel

C++

Informationen zum Installieren und Verwenden der Clientbibliothek für Bigtable finden Sie unter Bigtable-Clientbibliotheken.

Richten Sie Standardanmeldedaten für Anwendungen ein, um sich bei Bigtable zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.

namespace cbt = ::google::cloud::bigtable;
namespace cbta = ::google::cloud::bigtable_admin;
using ::google::cloud::StatusOr;
[](cbta::BigtableTableAdminClient admin, std::string const& project_id,
   std::string const& instance_id, std::string const& table_id) {
  std::string instance_name = cbt::InstanceName(project_id, instance_id);
  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> table = admin.GetTable(r);
  if (!table &&
      table.status().code() == google::cloud::StatusCode::kNotFound) {
    // The table does not exist, try to create the table.
    table = admin.CreateTable(instance_name, table_id, {});
    if (!table) throw std::move(table).status();
    // 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(std::move(r));
  }
  if (!table) throw std::move(table).status();
  std::cout << "Table metadata: " << table->DebugString() << "\n";
}

Nächste Schritte

Informationen zum Suchen und Filtern von Codebeispielen für andere Google Cloud-Produkte finden Sie im Google Cloud-Beispielbrowser.