This code sample is a "hello world" application that runs on Ruby. The sample illustrates how to complete the following tasks:
- Connect to a Cloud Bigtable instance.
- Create a new table.
- Write data to the table.
- Read the data back.
- Delete the table.
Running the sample
This code sample uses the Ruby Client for Cloud Bigtable package of the Google Cloud Client Library for Ruby to communicate with Cloud Bigtable.
To run this sample program, follow the instructions for the sample on GitHub.
Using the Cloud Client Library with Cloud Bigtable
The sample application connects to Cloud Bigtable and demonstrates some simple operations.
Requiring the client library
The sample requires google/cloud/bigtable
, which provides the Bigtable
module.
Connecting to Cloud Bigtable
Establish the variables you will use in your application, replacing
"YOUR_PROJECT_ID" with the ID of a valid Google Cloud project. Then
create a new Bigtable
object that you will use to
connect to Cloud Bigtable.
Creating a table
Check to see if your table already exists. If it doesn't, call the
create_table()
method to create a Table
object. The table has a single column family that retains one
version of each value.
Writing rows to a table
Next, use a string array of greetings to create some new rows for the table. For
each greeting, create an entry using the table's new_mutation_entry()
method. Next, use the entry's set_cell()
method to assign the column family, column qualifier, the greeting, and a
timestamp to the entry. Finally, write that entry to the table using the table's
mutate_row()
method.
Creating a filter
Before you read the data that you wrote, create a filter to limit the data that Cloud Bigtable returns. This filter tells Cloud Bigtable to return only the most recent version of each value, even if the table contains older versions that haven't been garbage-collected.
Reading a row by its row key
Create a row object, then call the read_row()
method,
passing in the filter, to get one version of each value in that row.
Scanning all table rows
Call the read_rows()
method, passing in the
filter, to get all of the rows in the table. Because you passed in the filter,
Cloud Bigtable returns only one version of each value.
Deleting a table
Delete the table with the table's delete()
method.
Putting it all together
Here is the full code sample without comments.