This code sample is a "hello world" application that runs on Ruby. The sample illustrates how to complete the following tasks:

  • Set up authentication
  • Connect to a Bigtable instance.
  • Create a new table.
  • Write data to the table.
  • Read the data back.
  • Delete the table.

Set up authentication

To use the Ruby samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.

  1. Install the Google Cloud CLI.

  2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  3. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

For more information, see Set up authentication for a local development environment.

Setting up your environment

In order to run this sample, you need to have Ruby installed.

1. Install Ruby

To check the minimum required Ruby version for the client library, see required_ruby_version in the google-cloud-bigtable.gemspec file.

Setting up Ruby on a Compute Engine VM If you want to run the sample on a Compute Engine VM, follow these instructions to set up Ruby on a Debian or Ubuntu VM.
  1. In the Google Cloud console, go to the VM instances page.

    Go to VM instances

  2. If you don't have a Linux VM that uses the Debian or Ubuntu operating system, create one and connect to it. For more information, see Quickstart using a Linux VM.

  3. In the terminal window for your VM, update your package lists:

    sudo apt update
    
  4. Install Ruby, Ruby development tools, and build-essential, which are required by the client library:

    sudo apt install -y ruby ruby-dev build-essential
    
  5. Verify that the installed Ruby version meets the minimum requirement specified in the google-cloud-bigtable.gemspec file:

    ruby --version
    

2. Install the client library

To install the client library:

  1. Initialize bundler to create a Gemfile in your project's root directory:

    bundle init
    
  2. Add google-cloud-bigtable to your Gemfile and install it:

    bundle add google-cloud-bigtable
    

For more information about the client library, see the google-cloud-bigtable README.

Running the sample

This code sample uses the Ruby client library for Bigtable package of the Google Cloud Client Library for Ruby to communicate with Bigtable.

To run this sample program, follow the instructions for the sample on GitHub.

Using the Cloud Client Library with Bigtable

The sample application connects to Bigtable and demonstrates some simple operations.

Requiring the client library

The sample requires google/cloud/bigtable, which provides the Bigtable module.

Connecting to 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 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 Bigtable returns. This filter tells 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, 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.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-03-30 UTC.