cqlsh

6.2.1 · active · verified Thu Apr 16

cqlsh is a Python-based command-line client for running CQL commands on an Apache Cassandra cluster. It provides an interactive shell for executing CQL statements, managing data, and administering Cassandra. It is part of the Apache Cassandra project and is actively maintained with releases tied to Cassandra versions. The current PyPI version is 6.2.1.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to invoke the `cqlsh` command-line tool from Python using the `subprocess` module to execute a CQL query. It checks for the Cassandra release version. Ensure a Cassandra cluster is running and accessible (e.g., on localhost:9042) before running this code.

import subprocess
import os

# Ensure Cassandra is running, e.g., via Docker:
# docker run --name my-cassandra -p 9042:9042 -d cassandra:latest

# Execute a simple CQL command using cqlsh via subprocess
try:
    # -e for executing a single command
    # You can specify host/port if not localhost:9042
    # e.g., ['cqlsh', 'your_host_ip', 'your_port', '-e', ...]
    # If authentication is needed: ['cqlsh', '--username', 'user', '--password', 'pass', '-e', ...]
    result = subprocess.run(
        ['cqlsh', '-e', 'SELECT release_version FROM system.local;'],
        capture_output=True,
        text=True,
        check=True # Raise an error for non-zero exit codes
    )
    print("cqlsh Output (Cassandra Release Version):")
    print(result.stdout.strip())
    print("\nSuccessfully connected and executed query.")
except FileNotFoundError:
    print("Error: 'cqlsh' command not found. Make sure cqlsh is installed and in your system PATH.")
except subprocess.CalledProcessError as e:
    print(f"Error executing cqlsh: {e.returncode}")
    print(f"Stderr: {e.stderr.strip()}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →