cqlsh
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
-
Connection refused
cause The Cassandra cluster is not running or is not accessible on the specified host/port.fixStart your Cassandra cluster. Verify Cassandra's listening address and port (e.g., `netstat -tulnp | grep 9042` on Linux). Use `cqlsh <host> <port>` if Cassandra is not on the default `localhost:9042`. -
cqlsh: command not found
cause The `cqlsh` executable is not in your system's PATH environment variable.fixEnsure `cqlsh` is installed (`pip install cqlsh`). Locate the installation directory for Python scripts (e.g., `~/.local/bin` or `C:\Python\Scripts`) and add it to your system's PATH. Restart your terminal. -
AuthenticationFailed: Failed to authenticate to <ip-address>:9042
cause The Cassandra cluster requires authentication, but `cqlsh` was run without correct credentials, or with no credentials.fixProvide a valid username and password using `cqlsh --username <user> --password <pass>` or configure credentials in your `~/.cqlshrc` file. -
ServerError: code=2000 [Syntax error in CQL query] message="line 1:XX no viable alternative at input '...'"
cause The CQL statement provided to `cqlsh` has a syntax error.fixCarefully review the CQL query for typos, missing keywords, incorrect punctuation, or features not supported by your specific Cassandra version. Consult the Apache Cassandra CQL documentation.
Warnings
- breaking cqlsh version compatibility with Cassandra server versions. Using an cqlsh version significantly different from your Cassandra cluster can lead to missing features, unexpected behavior, or connection errors due to protocol mismatches.
- gotcha Connection refused or NoHostAvailable error. `cqlsh` requires a running Cassandra cluster to connect to. If the cluster is down, inaccessible, or on a different host/port, connection attempts will fail.
- gotcha Authentication failures (`AuthenticationFailed`). If your Cassandra cluster is configured to require authentication, `cqlsh` will fail to connect without valid credentials.
- gotcha `cqlsh` is a command-line tool, not a Python library for direct import. Attempting to `import cqlsh` for programmatic interaction with Cassandra will not work as expected.
Install
-
pip install cqlsh
Imports
- cqlsh functionality
import cqlsh
from cassandra.cluster import Cluster
Quickstart
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}")