Skip to main content

BigtableByteStore

This guide covers how to use Google Cloud Bigtable as a key-value store. Bigtable is a key-value and wide-column store, ideal for fast access to structured, semi-structured, or unstructured data. Open In Colab

Overview

The BigtableByteStore uses Google Cloud Bigtable as a backend for a key-value store. It supports synchronous and asynchronous operations for setting, getting, and deleting key-value pairs.

Integration details

Setup

Prerequisites

To get started, you will need a Google Cloud project with an active Bigtable instance and table.

Installation

The integration is in the langchain-google-bigtable package. The command below also installs langchain-google-vertexai for the embedding cache example.

Set your Google Cloud project

Set your Google Cloud project to use its resources within this notebook. If you don’t know your project ID, you can run gcloud config list or see the support page: Locate the project ID.

Authentication

Authenticate to Google Cloud to access your project resources.

Instantiation

To use BigtableByteStore, we first ensure a table exists and then initialize a BigtableEngine to manage connections.

BigtableEngine

A BigtableEngine object handles the execution context for the store, especially for async operations. It’s recommended to initialize a single engine and reuse it across multiple stores for better performance.

BigtableByteStore

This is the main class for interacting with the key-value store. It provides the methods for setting, getting, and deleting data.

Usage

The store supports both sync (mset, mget) and async (amset, amget) methods. This guide uses the async versions.

Set

Use amset to save key-value pairs to the store.

Get

Use amget to retrieve values. If a key is not found, None is returned for that key.

Delete

Use amdelete to remove keys from the store.

Iterate over keys

Use ayield_keys to iterate over all keys or keys with a specific prefix.

Advanced usage: embedding caching

A common use case for a key-value store is to cache expensive operations like computing text embeddings, which saves time and cost.

As a simple document retriever

This section shows how to create a simple retriever using the Bigtable store. It acts as a document persistence layer, fetching documents that match a query prefix.

API reference

For full details on the BigtableByteStore class, see the source code on GitHub.