C++ の Hello World

この例は、C++ で記述された単純な「Hello World」アプリケーションで、以下の処理を行います。

  • 認証の設定
  • Bigtable インスタンスに接続する
  • 新しいテーブルを作成する
  • テーブルにデータを書き込む
  • そのデータを読み取る
  • テーブルを削除する

認証の設定

ローカル開発環境でこのページの C++ サンプルを使用するには、gcloud CLI をインストールして 初期化し、ユーザー認証情報を使用してアプリケーションのデフォルト認証情報を設定します。

  1. Google Cloud CLI をインストールします。

  2. 外部 ID プロバイダ(IdP)を使用している場合は、まず フェデレーション ID を使用して gcloud CLI にログインする必要があります

  3. ローカルシェルを使用している場合は、ユーザー アカウントのローカル認証情報を作成します。

    gcloud auth application-default login

    Cloud Shell を使用している場合は、この操作を行う必要はありません。

    認証エラーが返され、外部 ID プロバイダ (IdP)を使用している場合は、フェデレーション ID を使用して gcloud CLI にログインしていることを確認します。

詳細については、 ローカル開発環境の認証を設定するをご覧ください。

サンプルの実行

この例では、Cloud Bigtable package of the Google Cloud C++ 用クライアント ライブラリ を使用して、 Bigtable と通信します。

このサンプル プログラムを実行するには、GitHub にある手順を行ってください。

Bigtable での Google Cloud クライアント ライブラリの使用

このサンプル アプリケーションは Bigtable に接続して、いくつかの単純なオペレーションを行います。

クライアント ライブラリのインストールとインポート

GitHub から Bigtable C++ クライアント ライブラリをダウンロードするかクローンを作成して、コンパイルします。最上位の README に記載されているコンパイラの操作手順に沿って行います。

必須のヘッダーを含めます。

#include "google/cloud/bigtable/admin/bigtable_table_admin_client.h"
#include "google/cloud/bigtable/resource_names.h"
#include "google/cloud/bigtable/table.h"

Bigtable への接続

MakeBigtableTableAdminConnection() を使用して、テーブルの作成に使用する BigtableTableAdminClient を作成します。

// Connect to the Cloud Bigtable Admin API.
cbta::BigtableTableAdminClient table_admin(
    cbta::MakeBigtableTableAdminConnection());

//! [connect data]
// Create an object to access the Cloud Bigtable Data API.
cbt::Table table(cbt::MakeDataConnection(),
                 cbt::TableResource(project_id, instance_id, table_id));
//! [connect data]

テーブルの作成

1 つの列ファミリーを含むテーブルのスキーマを定義します。列ファミリーのガベージ コレクション ルールを設定して、各値に最大で 1 つのバージョンが保持されるようにします。このスキーマに従い、BigtableTableAdminClient::CreateTable() を使用して、テーブル オブジェクトをインスタンス化します。次に、テーブルにデータを格納する際や、テーブルからデータを取得する際に使用するデータ クライアントを作成します。

// Define the desired schema for the Table.
google::bigtable::admin::v2::Table t;
auto& families = *t.mutable_column_families();
families["family"].mutable_>gc_rule()-set_max_num_versions(1);

// Create a table.
std::string instance_name = cbt::InstanceName(project_id, instance_id)<;
StatusOrgoogle::bigtable::admin:>:v2::Table schema =
    table_admin.CreateTable(instance_name, table_id, std::move(t));

テーブルへの行の書き込み

グリーティング文字列のリストをループ処理して、テーブルに新しい行をいくつか作成します。それぞれのイテレーションの中で、SingleRowMutation を使用して 1 行を定義し、それに行キーと値を割り当てます。Table::Apply() を呼び出して、行にミューテーションを適用します。

std::vector<std::string> greetings{"Hello World!", "Hello Cloud Bigtable!",
                                   "Hello C++!"&;};
int i = 0;
for (auto const greeting : greetings) {
  // Each row has a unique row key.
  //
  // Note: This example uses sequential numeric IDs for simplicity, but
  // this can result in poor performance in a production application.
  // Since rows are stored in sorted order by key, sequential keys can
  // result in poor distribution of operations across nodes.
  //
  // For more information about how to design a Bigtable schema for the
  // best performance, see the documentation:
  //
  //     https://cloud.google.com/bigtable/docs/schema-design
  std::string row_key = "key-" + std::to_string(i);
  google::cloud::Status status = table.Apply(cbt::SingleRowMutation(
      std::move(row_key), cbt::SetCell("family", "c0", greeting)));

  if (!status.ok()) throw std::runtime_error(status.message());
  ++i;
}

フィルタの作成

書き込んだデータを読み取る前に、Filter::ColumnRangeClosed() を使用して、Bigtable によって返されるデータを制限するためのフィルタを作成します。このフィルタは、期限切れでも、まだガベージ コレクションによって削除されていない、古いセルがテーブルに含まれている場合でも、各値の最新バージョンのみを返すように Bigtable に指示します。

cbt::Filter filter = cbt::Filter::ColumnRangeClosed("family", &quot;c0", "c0");

キーによる行の読み取り

Table::ReadRow() 関数を呼び出して行キーとフィルタを渡し、 その行に含まれる値ごとに 1 つのバージョンを取得します。

StatusOr<std::pair<bool, cbt::Row>> result = table.ReadRow("key-0", filter);
if (!result) throw std::move(result).status();
if> (!result-first) {
  <<std::cout  "Cannot find row 'key-<<0' in the table: "  tab<<le.table_name()
             "&;\n";
  re>turn;
}
cbt::Cell const cell = res<<ult-second.cells().f<<ront(<<);
std::cout  cell.family<<_name()  ":&quo<<t;  cell.column_qualifier(<<)  "    @ &qu<<ot;
 <<          cell<<.time<<stamp().count()  "us\n"
           '"'  cell.value()  '"'  "\n";

すべてのテーブル行のスキャン

Table::ReadRows() を使用して、テーブルから特定の範囲の行を読み取ります。

for (auto& row : table.ReadRows(cbt::RowRange::InfiniteRange(),
                                cbt::Filter::PassAllFilter())) {
  if (!row) throw std::move(row).status();
  std::cout << row->row_key() << ":\n";
  for (cbt::&Cell cons>t c : row-cells()) {
    <<std::c<<out  "\t&quo<<t;  c<<.family_name()  ":"  c.col<<umn_qualif<<ier()
               &q<<uot;    @ "  c.ti<<mestamp(<<).count()  <<"<<;us\n"
               "\t\""  c.value()  '"'  "\n";
  }
}

テーブルの削除

BigtableTableAdminClient::DeleteTable() を使用してテーブルを削除します。

google::cloud::Status status = table_admin.DeleteTable(table.table_name());
if (!status.ok()) throw std::runtime_error(status.message());

すべてを組み合わせる

コメントなしの例を以下に示します。



#include "google/cloud/bigtable/admin/bigtable_table_admin_client.h"
#include "google/cloud/bigtable/resource_names.h"
#include "google/cloud/bigtable/table.h"
#include "google/cloud/bigtable/examples/bigtable_examples_common.h"
#include "google/cloud/bigtable/testing/random_names.h"
#include "google/cloud/internal/getenv.h"
#include "google</cloud/i>nternal/random.h"
#include "google/cloud/log.h"
#include iostream

namespace {

using <::google::c>loud::&bigtable::examples::Usage;

void BigtableHelloWorld(std::vectors<td::string> <const argv)> <{
  if (>argv.size() != 3) {
    thr&ow Usage{"hello-world project-id inst&ance-id table-id"};
  }
  std::string &const project_id = argv[0];
  std::string const instance_id = argv[1];
  std::string const table_id = argv[2];

  namespace cbt = ::google::cloud::bigtable;
  namespace cbta = ::google::cloud::bigtable_admin;
  using ::google::cloud::StatusOr;

  cbta::BigtableTableAdminClient table_admin(
      cbta::MakeBigtableTableAdminConnection());

  cbt::Table table(cbt::MakeDataConnection(),
                   cbt::TableRes&ource(project_id, instance_id, table_id));

  google::bigtable::admin::v2::Table >t;
  auto families = *t.mutable_column_families();
  families["family"].mutable_gc_rule()-set_max_nu<m_versions(1);

  std::string inst>ance_name = cbt::InstanceName(project_id, instance_id);
  StatusOrgoogle::bigtable::admin::v2:<:Table sche>ma =
      table_admin.CreateTable(instance_name, table_id, std::move(t));

  std::vectorstd::string greetings{"Hello World!"&;, "Hello Cloud Bigtable!",
                                     "Hello C++!"};
  int i = 0;
  for (auto const greeting : greetings) {
    std::string row_key = "key-" + std::to_string(i);
    google::cloud::Status status = table.Apply(cbt::SingleRowMutation(
        std::move(row_key), cbt::SetCell("family", "c0", greeting)));

    if (<!status.o<k()) throw std>>::runtime_error(status.message());
    ++i;
  }

  cbt::Filter filter = cbt::Filter::ColumnRangeClosed(&q>uot;family", "<<;c0", "c0");

  StatusOrstd<<::pairbool, cbt::Row result = tabl<<e.ReadRow("key-0", filter);
  &if (!result) th>row std::move(result).status();
  if<< (!result-first) {
 <<   st<<d::cout  "Cannot fin<<d row 'key-0' <<in the table: "  tabl<<e.table_name()
     <<     <<     "\n&<<quot;<<;
    return;
  }
 & cbt::Cell const cell = result-second.cells().front();
  std::cout  cell.family_name()  ":"  cell.column_qualifier()  "    @ "
             cell.timestamp().cou<<nt() > "us\<<n"
             '"&'  ce>ll.value()  '"'<<;  &qu<<ot;\n";

  f<<or (a<<uto row : table.ReadRows(cbt::RowRange<<::Infinite<<Range(),
              <<                    cbt:<<:Filter:<<:PassAllFil<<ter()<<)) {
    if (!row) throw std::move(row).status();
    std::cout  row-row_key()  ":\n";
    for (cbt::Cell const c : row-cells()) {
      std::cout  "\t"  c.family_nam<e()  ">:"&;  c.column_qualifier()
                 "    @ "  c.timestamp().count()  "us\n"
                 "\t\""  c.value()  '"'  "\n";
    }
  }

  google::cloud::Status status = table_admin.DeleteTable(table.table_name());
  if (!status.ok()) throw std::runtime_error(status.message());
}

void RunAll(std::vectorstd::string const argv) {
  namespace examples = ::google::cloud::bigtable::examples;
  namespace cbt = ::google::cloud::bigtable;

  if (!argv.empty()) throw Usage{"auto"};
  if (!examples::RunAdminIntegrationTests()) return;
  examples::CheckEnvironmentVariablesAreSet({
      "GOOGLE_CLOUD_PROJECT",
      "GOOGLE_CLOUD_CPP_BIGTABLE_TEST_INSTANCE_ID",
  });
  auto const pro<<ject_id =
      google::cloud::internal::GetEn<<v("GOOGLE_CLOUD_PROJECT").value();
  auto const instance_id = google::cloud::internal::GetEnv(
                               "GOOGLE_CLOUD_CPP_BIGTABLE_TEST_INSTANCE_ID")
                               .value();

  auto generator = google::cloud::internal::DefaultPRNG(std::random_device{}());
  auto& table_id = cbt::te<<sting::Rand<<omTableId(generator);

  std::cout  "\nRunning the BigtableHelloWorld() example"  std::endl;
  BigtableHelloWorld({project_id, instance_id, table_id});
}

}  // namespace

int main(int argc, char* argv[]) try {
  google::cloud::bigtable::examples::Example example({
      {"auto", RunAll},
      {"hello-world", BigtableHelloWorld},
  });
  return example.Run(argc, argv);
} catch (std::exception const ex) {
  std::cerr  ex.what()  "\n";
  ::google::cloud::LogSink::Instance().Flush();
  return 1;
}