Ruby 版 Hello World

此代码示例是一个在 Ruby 上运行的“hello world”应用。说明如何完成以下任务:

  • 设置身份验证
  • 连接到 Bigtable 实例。
  • 新建一个表。
  • 将数据写入表中。
  • 重新读取这些数据。
  • 删除表。

设置身份验证

如需从本地开发环境使用本页面上的 Ruby 示例,请安装并初始化 gcloud CLI,然后使用用户凭据设置应用默认凭据。

  1. 安装 Google Cloud CLI。
  2. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  3. 为您的 Google 账号创建本地身份验证凭据:

    gcloud auth application-default login

如需了解详情,请参阅 为本地开发环境设置身份验证

运行示例

本代码示例使用 Ruby 版 Google Cloud 客户端库适用于 Bigtable 的 Ruby 客户端库 软件包与 Bigtable 进行通信。

要运行此示例程序,请按照 GitHub 上的相应示例说明执行操作。

将 Cloud 客户端库与 Bigtable 搭配使用

示例应用会连接到 Bigtable 并演示一些简单操作。

需要客户端库

该示例需要使用 google/cloud/bigtable,其中提供了 Bigtable 模块。

require "google/cloud/bigtable"

连接到 Bigtable

建立您要在应用中使用的变量,并将“YOUR_PROJECT_ID”替换为有效 Google Cloud 项目的 ID。然后,创建一个新的 Bigtable 对象,您将使用此对象连接到 Bigtable。

# instance_id      = "my-instance"
# table_id         = "my-table"
# column_family    = "cf"
# column_qualifier = "greeting"

bigtable = Google::Cloud::Bigtable.new

创建表

检查您的表是否已存在。如果不存在,请调用 create_table() 方法来创建一个 Table 对象。该表有一个列族,其中保留了每个值的一个版本。

if bigtable.table(instance_id, table_id).exists?
  puts "#{table_id} is already exists."
  exit 0
else
  table = bigtable.create_table instance_id, table_id do |column_families|
    column_families.add(
      column_family,
      Google::Cloud::Bigtable::GcRule.max_versions(1)
    )
  end

  puts "Table #{table_id} created."
end

将行写入表

接下来,使用由问候语组成的字符串数组来为表创建一些新行。对于每条问候语,使用表的 new_mutation_entry() 方法创建一个条目。然后,使用条目的 set_cell() 方法为该条目分配列族、列限定符、问候语和时间戳。最后,使用表的 mutate_row() 方法将该条目写入表中。

puts "Write some greetings to the table #{table_id}"
greetings = ["Hello World!", "Hello Bigtable!", "Hello Ruby!"]

# Insert rows one by one
# Note: To perform multiple mutation on multiple rows use `mutate_rows`.
greetings.each_with_index do |value, i|
  puts " Writing,  Row key: greeting#{i}, Value: #{value}"

  entry = table.new_mutation_entry "greeting#{i}"
  entry.set_cell(
    column_family,
    column_qualifier,
    value,
    timestamp: (Time.now.to_f * 1_000_000).round(-3)
  )

  table.mutate_row entry
end

创建过滤条件

在读取您写入的数据之前,请创建过滤条件,以限制 Bigtable 返回的数据。此过滤条件指示 Bigtable 仅返回每个值的最新版本,即使该表包含尚未被垃圾回收的旧版本。

# Only retrieve the most recent version of the cell.
filter = Google::Cloud::Bigtable::RowFilter.cells_per_column 1

按行键读取行

创建一个行对象,然后调用 read_row() 方法并传入过滤条件,以获取该行中每个值的一个版本。

puts "Reading a single row by row key"
row = table.read_row "greeting0", filter: filter
puts "Row key: #{row.key}, Value: #{row.cells[column_family].first.value}"

扫描所有表行

调用 read_rows() 方法并传入过滤条件,以获取表中的所有行。由于您传入了过滤条件,因此 Bigtable 仅会返回每个值的一个版本。

puts "Reading the entire table"
table.read_rows.each do |row|
  puts "Row key: #{row.key}, Value: #{row.cells[column_family].first.value}"
end

删除表

使用表的 delete() 方法删除表。

puts "Deleting the table #{table_id}"
table.delete

综合应用

以下为不包含注释的完整代码示例。

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def hello_world instance_id, table_id, column_family, column_qualifier
  require "google/cloud/bigtable"

  # instance_id      = "my-instance"
  # table_id         = "my-table"
  # column_family    = "cf"
  # column_qualifier = "greeting"

  bigtable = Google::Cloud::Bigtable.new

  if bigtable.table(instance_id, table_id).exists?
    puts "#{table_id} is already exists."
    exit 0
  else
    table = bigtable.create_table instance_id, table_id do |column_families|
      column_families.add(
        column_family,
        Google::Cloud::Bigtable::GcRule.max_versions(1)
      )
    end

    puts "Table #{table_id} created."
  end

  puts "Write some greetings to the table #{table_id}"
  greetings = ["Hello World!", "Hello Bigtable!", "Hello Ruby!"]

  # Insert rows one by one
  # Note: To perform multiple mutation on multiple rows use `mutate_rows`.
  greetings.each_with_index do |value, i|
    puts " Writing,  Row key: greeting#{i}, Value: #{value}"

    entry = table.new_mutation_entry "greeting#{i}"
    entry.set_cell(
      column_family,
      column_qualifier,
      value,
      timestamp: (Time.now.to_f * 1_000_000).round(-3)
    )

    table.mutate_row entry
  end

  # Only retrieve the most recent version of the cell.
  filter = Google::Cloud::Bigtable::RowFilter.cells_per_column 1

  puts "Reading a single row by row key"
  row = table.read_row "greeting0", filter: filter
  puts "Row key: #{row.key}, Value: #{row.cells[column_family].first.value}"

  puts "Reading the entire table"
  table.read_rows.each do |row|
    puts "Row key: #{row.key}, Value: #{row.cells[column_family].first.value}"
  end

  puts "Deleting the table #{table_id}"
  table.delete
end