使用 mutate 插入行

使用 mutate 插入行。

代码示例

C++

如需了解如何安装和使用 Bigtable 的客户端库,请参阅 Bigtable 客户端库

如需向 Bigtable 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

namespace cbt = ::google::cloud::bigtable;
[](cbt::Table table) {
  // Write several rows in a single operation, each row has some trivial data.
  cbt::BulkMutation bulk;
  for (int i = 0; i != 5000; ++i) {
    // 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
    char buf[32];
    snprintf(buf, sizeof(buf), "key-%06d", i);
    cbt::SingleRowMutation mutation(buf);
    mutation.emplace_back(
        cbt::SetCell("fam", "col0", "value0-" + std::to_string(i)));
    mutation.emplace_back(
        cbt::SetCell("fam", "col1", "value1-" + std::to_string(i)));
    bulk.emplace_back(std::move(mutation));
  }
  std::vector<cbt::FailedMutation> failures =
      table.BulkApply(std::move(bulk));
  if (failures.empty()) {
    std::cout << "All mutations applied successfully\n";
    return;
  }
  // By default, the `table` object uses the `SafeIdempotentMutationPolicy`
  // which does not retry if any of the mutations fails and are
  // not-idempotent. In this example we simply print such failures, if any,
  // and ignore them otherwise.
  std::cerr << "The following mutations failed and were not retried:\n";
  for (auto const& f : failures) {
    std::cerr << "index[" << f.original_index() << "]=" << f.status() << "\n";
  }
}

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器