This page describes the Cassandra Adapter and explains how to use and connect Spanner from it.
The Cassandra Adapter is designed to run on the same machine as your application. The adapter exposes an endpoint on localhost that supports the Cassandra Query Language (CQL) wire protocol. It translates the CQL wire protocol into gRPC, the Spanner wire protocol. With this proxy running locally, a Cassandra client can connect to a Spanner database.
You can start the Cassandra Adapter in the following ways:
- In-process with your Go application
- In-process with your Java application
- As a standalone process
- In a Docker container
Before you begin
Before starting the Cassandra Adapter, ensure that you have authenticated
with either a user account or service account on the machine where
Cassandra Adapter will be running. If you are using a service account, you
must know the location of the JSON key file (the credentials file). You set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to specify the credentials
path.
For more information, see:
Connect the Cassandra Adapter to your application
The Cassandra Adapter requires the following information:
- Project name
- Spanner instance name
- Database to connect to
If you use Docker, you need the path for a JSON-formatted credentials file (key file).
Go in-process
For Go applications, you only need to make a one-line change to the cluster initialization file to integrate the Spanner Cassandra Go client. You can then link the Cassandra Adapter to the application directly.
Import the adapter's
spanner
package from Spanner Cassandra Go Client in your Go application.import spanner "github.com/googleapis/go-spanner-cassandra/cassandra/gocql"
Modify your cluster creation code to use
spanner.NewCluster
instead ofgocql.NewCluster
, and provide the Spanner database URI:You can still configure your cluster as usual after connecting to your Spanner database.
Java in-process
If you are using a service account for authentication, ensure that the
GOOGLE_APPLICATION_CREDENTIALS
environment variable is set to the path of the credentials file.For Java applications, you can link the Cassandra Adapter to the application directly by adding
google-cloud-spanner-cassandra
as a dependency to your project.For Maven, add the following:
<dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-spanner-cassandra</artifactId> <version>0.1.0</version> </dependency>
For Gradle, add the following:
dependencies { implementation 'com.google.cloud:google-cloud-spanner-cassandra:0.1.0' }
Modify your
CqlSession
creation code. Instead of usingCqlSessionBuilder
, useSpannerCqlSessionBuilder
and provide the Spanner database URI:
Standalone
Clone the repository:
git clone https://github.com/googleapis/go-spanner-cassandra.git cd go-spanner-cassandra
Run the
cassandra_launcher.go
with the required-db
flag:go run cassandra_launcher.go \ -db "projects/my_project/instances/my_instance/databases/my_database"
Replace
-db
with your Spanner database URI.
Docker
Start the Cassandra Adapter with the following command.
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
docker run -d -p 9042:9042 \
-e GOOGLE_APPLICATION_CREDENTIALS \
-v ${GOOGLE_APPLICATION_CREDENTIALS}:${GOOGLE_APPLICATION_CREDENTIALS}:ro \
gcr.io/cloud-spanner-adapter/cassandra-adapter \
-db DATABASE_URI
The following list contains the most frequently used startup options for the Spanner Cassandra Adapter:
-db <DatabaseUri>
- The Spanner database URI (required). This specifies the
Spanner database that the client connects to. For
example,
projects/YOUR_PROJECT/instances/YOUR_INSTANCE/databases/YOUR_DATABASE
- The Spanner database URI (required). This specifies the
Spanner database that the client connects to. For
example,
-tcp <TCPEndpoint>
- The client proxy listener address. This defines the TCP endpoint where the client listens for incoming Cassandra client connections.
- Default:
- When running in-process inside a Go language application: localhost:9042
- When running as a sidecar proxy: :9042 to bind all network interfaces, suitable for Docker forwarding.
-grpc-channels <NumGrpcChannels>
- The number of gRPC channels to use when connecting to Spanner.
- Default: 4
For example, the following command starts the Cassandra Adapter on port
9042
using the application credentials, and connects the adapter to theprojects/my_project/instances/my_instance/databases/my_database
database:export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json docker run -d -p 9042:9042 \ -e GOOGLE_APPLICATION_CREDENTIALS \ -v ${GOOGLE_APPLICATION_CREDENTIALS}:${GOOGLE_APPLICATION_CREDENTIALS}:ro \ gcr.io/cloud-spanner-adapter/cassandra-adapter \ -db projects/my_project/instances/my_instance/databases/my_database
What's next
- Learn more about the Spanner Cassandra Adapter for Java in the java-spanner-cassandra GitHub repository.
- Learn more about the Spanner Cassandra Adapter for Go in the go-spanner-cassandra GitHub repository.
- See a comparison between Cassandra and Spanner concepts and architecture.
- Learn how to Migrate from Cassandra to Spanner.