建立您要在应用中使用的变量,并将“YOUR_PROJECT_ID”替换为有效 Google Cloud 项目的 ID。然后,创建一个新的 Bigtable 对象,您将使用此对象连接到 Bigtable。
# These variables are used in the sample code below.# instance_id = "my-instance"# table_id = "my-table"# column_family = "cf"# column_qualifier = "greeting"bigtable=Google::Cloud::Bigtable.newtable_client=bigtable.table_admin_client
# This is the full resource name for the table. Use this name to make admin# calls for the table, such as reading or deleting the resource.table_name=table_client.table_pathproject:bigtable.project_id,instance:instance_id,table:table_idbegin# Attempt to get the table to see if it already existstable_client.get_tablename:table_nameputs"#{table_id} is already exists."exit0rescueGoogle::Cloud::NotFoundError# The table doesn't exist, so let's create it.# The following is the resource name for the table's instance.instance_name=table_client.instance_pathproject:bigtable.project_id,instance:instance_id# This is the configuration of the table's column families.table_config={column_families:{column_family=>{gc_rule:Google::Cloud::Bigtable::Admin::V2::GcRule.max_num_versions(1)}}}# Now call the API to create the table.table_client.create_tableparent:instance_name,table_id:table_id,table:table_configputs"Table #{table_id} created."end
puts"Write some greetings to the table #{table_id}"greetings=["Hello World!","Hello Bigtable!","Hello Ruby!"]# Get a table data object for the new table we created.table=bigtable.tableinstance_id,table_id# Insert rows one by one# Note: To perform multiple mutation on multiple rows use `mutate_rows`.greetings.each_with_indexdo|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_rowentryend
puts"Reading a single row by row key"row=table.read_row"greeting0",filter:filterputs"Row key: #{row.key}, Value: #{row.cells[column_family].first.value}"
puts"Deleting the table #{table_id}"# Call the admin API to delete the table given its full resource path.table_client.delete_tablename:table_name
综合应用
以下为不包含注释的完整代码示例。
# 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.defhello_worldinstance_id,table_id,column_family,column_qualifierrequire"google/cloud/bigtable"# These variables are used in the sample code below.# instance_id = "my-instance"# table_id = "my-table"# column_family = "cf"# column_qualifier = "greeting"bigtable=Google::Cloud::Bigtable.newtable_client=bigtable.table_admin_client# This is the full resource name for the table. Use this name to make admin# calls for the table, such as reading or deleting the resource.table_name=table_client.table_pathproject:bigtable.project_id,instance:instance_id,table:table_idbegin# Attempt to get the table to see if it already existstable_client.get_tablename:table_nameputs"#{table_id} is already exists."exit0rescueGoogle::Cloud::NotFoundError# The table doesn't exist, so let's create it.# The following is the resource name for the table's instance.instance_name=table_client.instance_pathproject:bigtable.project_id,instance:instance_id# This is the configuration of the table's column families.table_config={column_families:{column_family=>{gc_rule:Google::Cloud::Bigtable::Admin::V2::GcRule.max_num_versions(1)}}}# Now call the API to create the table.table_client.create_tableparent:instance_name,table_id:table_id,table:table_configputs"Table #{table_id} created."endputs"Write some greetings to the table #{table_id}"greetings=["Hello World!","Hello Bigtable!","Hello Ruby!"]# Get a table data object for the new table we created.table=bigtable.tableinstance_id,table_id# Insert rows one by one# Note: To perform multiple mutation on multiple rows use `mutate_rows`.greetings.each_with_indexdo|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_rowentryend# Only retrieve the most recent version of the cell.filter=Google::Cloud::Bigtable::RowFilter.cells_per_column1puts"Reading a single row by row key"row=table.read_row"greeting0",filter:filterputs"Row key: #{row.key}, Value: #{row.cells[column_family].first.value}"puts"Reading the entire table"table.read_rows.eachdo|row|puts"Row key: #{row.key}, Value: #{row.cells[column_family].first.value}"endputs"Deleting the table #{table_id}"# Call the admin API to delete the table given its full resource path.table_client.delete_tablename:table_nameend
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-11。"],[[["\u003cp\u003eThis Ruby "hello world" sample demonstrates how to interact with a Bigtable instance, including setting up authentication, creating a table, and writing data.\u003c/p\u003e\n"],["\u003cp\u003eThe sample utilizes the Google Cloud Client Library for Ruby, specifically the \u003ccode\u003egoogle/cloud/bigtable\u003c/code\u003e package, to establish a connection and perform operations on Bigtable.\u003c/p\u003e\n"],["\u003cp\u003eThe code showcases the process of checking for a table's existence, creating it if it doesn't exist, and defining its configuration with a single column family.\u003c/p\u003e\n"],["\u003cp\u003eIt illustrates how to write data to the table using mutation entries and the \u003ccode\u003eset_cell()\u003c/code\u003e method, allowing the assignment of column family, qualifier, value, and timestamp.\u003c/p\u003e\n"],["\u003cp\u003eThe sample shows reading data by a specified row key and the entire table using \u003ccode\u003eread_row()\u003c/code\u003e and \u003ccode\u003eread_rows()\u003c/code\u003e methods, and then deleting the created table.\u003c/p\u003e\n"]]],[],null,[]]