Developing with CQL

To use Stargate’s Cassandra Query Language (CQL) API you can use the same CQL syntax, tools and native language drivers that you would with a normal Cassandra deployment.

Popular Cassandra drivers are:

Prerequisites

  • cqlsh (CQL shell) (standalone, as part of Astra DB, or as part of a separate Cassandra installation)

If you haven’t already, create a database using Astra DB.

You can create an Astra DB database using Astra Portal or the DevOps API. Currently, there are no drivers or other APIs that support creating a database.

  • Install cURL, a utility for running JSON, REST, Document, or GraphQL queries on the command line.

Before you get started, set your environment variables to save time developing on your database. There are four environment variables, three of which you will get from the Astra dashboard (database id, region, and keyspace), and one that you must create (token).

  1. In Astra DB, select the database to which you want to connect.

  2. In your Database Dashboard, select Connect.

  3. Select your API.

    If you have multiple regions, select the region you want to connect to from the dropdown menu for instructions.

  4. Follow the steps to get your application token and set up your environment variables. Or if you have an older Astra Classic database, follow the steps in Authentication for classic databases.

CQL console

The Cassandra Query Language SHell (CQLSH) is a command line shell for interacting with your database through Cassandra Query Language (CQL). This tool provides a useful interface for accessing the database and issuing CQL commands.

Embedded Astra DB CQL shell

Each DataStax Astra DB database includes an embedded CQL shell instance. In Astra Portal, navigate to your database and click the CQL Console tab to open a CQLSH instance that is connected to your database. Issue CQL commands directly to your Astra DB database without navigating outside of your browser.

To see the CQL console in a full browser window, use this URL format:

database_id-region.apps.astra.datastax.com/cqlsh

For example, 2f0c6j8b-9790-4276-a16c-0cd7b055344a-us-east1.apps.astra.datastax.com/cqlsh

Once you open the CQL shell in Astra, you will automatically be signed in and connected to your database.

Results

You can now run CQL commands in your embedded CQL shell.

Alternatively, you can connect to your Astra DB database using the standalone version of CQLSH.

Standalone CQL shell

Instead of using the native CQL shell that is included in DataStax Enterprise (DSE), the standalone CQLSH client is a separate, lightweight tool you can use to interact with your database.

Each DataStax Astra DB database includes an embedded CQL shell instance. In Astra Portal, navigate to your database and click the CQL Console tab to open a CQLSH instance that is connected to your database.

Prerequisites

  1. Download CQLSH. Choose the version for Astra DB.

  2. Create an Astra DB database.

  3. Python version with TLS Support. A minimum of 2.7.12. More details here.

Working with secure connect bundle

This page explains how to use the secure connect bundle for Astra DB Classic.

If you want to use the secure connect bundle with Astra DB Serverless, see the Astra DB Serverless documentation.

Downloading secure connect bundle

To get the necessary security credentials and certificates for connecting drivers to your Astra DB database, you’ll need to download the secure connect bundle from the DataStax Astra Portal.

  1. Open your Astra Portal and select your database.

  2. On the Overview page, select Connect.

  3. In the Database Essentials section, click Get Bundle.

  4. In the Secure Connect Bundle Download dialog box, use the Select a region dropdown menu to select the region for which you want to download the bundle. If you have multiple regions, you can download a bundle for each region.

    If you’ve enabled VPC peering, you can also Download External Secure Connect Bundle for use within your VPC peering. VPC peering is only available on Classic databases.

  5. After selecting a region, various options for downloading the bundle appear. To download the bundle to your local computer, click Download Secure Bundle.

    The secure connect bundle downloads as a ZIP file named secure-connect-<database_name>.zip.

    Expiration of download link

    For added security, the download link for the secure connect bundle expires after five minutes. Once the download link expires, you’ll need to repeat the steps above to regenerate a new download link.

    Note that the secure connect bundle itself never expires.

Sharing secure connect bundle

Although teammates can access your Astra DB database, it doesn’t display in their list of available databases under My Databases in Astra Portal.

After you create an Astra DB database, you can grant access to other members of your team by providing them with the database credentials and connection details for your database.

Be careful when sharing connection details. Providing this information to another user grants them access to your Astra DB database and ownership capabilities, such as making modifications to the database.

For security, delete downloaded connection credentials after sending them to your teammate.

Secure connect bundle contents

The Secure Connect Bundle (SCB) contains the following files:

File Contents

ca.crt

DataStax’s Certificate Authority public certificate

cert

A certificate, unique to the specific SCB

key

A private key, unique to the specific SCB

cert.pfx

A PFX formatted archive containing the certificate and the private key

config.json

A configuration file with information on how to securely connect to the Astra DB instance associated with the SCB

cqlshrc

A CQLSH profile containing CQL shell session settings

identity.jks

A Java keystore file containing the aforementioned cert & key files

trustStore.jks

A Java keystore file containing the aforementioned ca.crt

Procedure

  1. Change to the directory where you downloaded the CQLSH tarball and then extract it:

$ cd /<download_directory>
$ tar -xvf cqlsh-astra-<date>-bin.tar.gz
  • date - The date that the tarball was released, such as 20210304.

  1. From the directory where you extracted the CQLSH tarball, run the cqlsh script from the command line:

$ cd /cqlsh-astra
$ ./bin/cqlsh -u **clientID** -p **clientSecret** -b /path/to/secure-connect**database_name**.zip

Use the -b option to specify the location of the secure connect bundle that you downloaded for your database. Pass the Client Id and Client Secret for your database role using the -u and -p options. For more, see Manage application tokens.

  • username - Client ID for the database role.

  • password - Client secret for the specified Client ID.

  • database_name - Name of the Astra DB database to which you want to connect. Include the absolute path to the secure connect bundle for the database, such as /Users/myuser/Downloads/secure-connect-database123.zip.

Configure the cqlshrc file If you do not want to pass the secure connect bundle on the command line every time, set up the location in your cqlshrc file.

In the [authentication] section of the cqlshrc file in ~/.cassandra, include the Client ID and Client Secret from your application token. Configure the [connection] section of the cqlshrc file in ~/.cassandra to include the path to your secure connect bundle.

[authentication]
username = clientID
password = clientSecret

[connection]
secure_connect_bundle = **/path/to/**secure-connect-**database_name**.zip

Results

You are connected directly to your database using CQLSH.

[cqlsh 6.8.0 | DSE 6.8.0.77 | CQL spec 3.4.5 | DSE protocol v2]
Use HELP for help.
username@cqlsh>

Switch to your KEYSPACE, where you can issue CQL commands to create tables, insert data, and manage database resources:

USE keyspace_name;

An alternative way to connect to the CQL shell is to use the Astra CLI. This option downloads and configures CQL shell for you, including downloading Secure Connect Bundle and setting up the token to the Astra account. For more on installing and getting started, see Astra CLI documentation.

Interact with CQL

You can now add and retrieve schema and data using CQL.

Create a table

CREATE TABLE users_keyspace.users (
    firstname text,
    lastname text,
    email text,
    "favorite color" text,
    PRIMARY KEY (firstname, lastname)
) WITH CLUSTERING ORDER BY (lastname ASC);

Insert data

INSERT INTO users_keyspace.users (
  firstname,
  lastname,
  email,
  "favorite color"
) VALUES (
  'Mookie',
  'Betts',
  'mookie.betts@gmail.com',
  'blue'
);

Retrieve data

To select all rows in the table:

SELECT * FROM users_keyspace.users;

To select a row using the primary key:

SELECT * FROM users_keyspace.users WHERE firstname = 'Mookie' AND lastname = 'Betts';

Update data

Update the email address:

UPDATE users_keyspace.users SET email = 'mookie.betts-new-email@gmail.com' WHERE firstname = 'Mookie' AND lastname = 'Betts';

Verify that the row has the updated email address:

SELECT * FROM users_keyspace.users WHERE firstname = 'Mookie';

Delete data

Delete the row using the primary key:

DELETE FROM users_keyspace.users WHERE firstname = 'Mookie' AND lastname = 'Betts';

Using the CQL API with Cassandra drivers

Astra DB Classic can be used with any of the Cassandra drivers.

For example, to connect to Astra DB Classic running on your local machine using the Python driver, replacing the IP address with an appropriate address:

from cassandra.cluster import Cluster

cluster = Cluster(['127.0.0.1'])

session = cluster.connect()

session.set_keyspace('users_keyspace')

rows = session.execute('SELECT firstname, lastname, email FROM users')
for user_row in rows:
    print(user_row.firstname, user_row.lastname, user_row.email)

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com