cqlsh

Start the CQL interactive terminal.

Start the CQL interactive terminal.

Synopsis 

$ cqlsh [options] [host [port]]
$ python cqlsh [options] [host [port]]

Description 

The Cassandra installation includes the cqlsh utility, a python-based command line client for executing Cassandra Query Language (CQL) commands. The cqlsh command is used on the Linux or Windows command line to start the cqlsh utility. On Windows, the keyword python is used.

You can use cqlsh to execute CQL commands interactively. cqlsh supports tab completion. You can also execute cqlsh commands, such as TRACE.

The cqlsh utility uses the Thrift transport.

Requirements 

By default, Cassandra enables Thrift by configuring start_rpc to true in the cassandra.yaml file. The cqlsh utility uses the Thrift RPC service. Also, firewall configuration to allow access through the Thrift port might be required.

Options 

-C, --color
Always use color output.
--debug
Show additional debugging information.
-f file_name, --file=file_name
Execute commands from FILE, then exit.
-h, --help
Show the online help about these options and exit.
-k keyspace_name
Use the given keyspace. Equivalent to issuing a USE keyspace command immediately after starting cqlsh.
--no-color
Never use color output.
-p password
Authenticate using password. Default = cassandra.
-t transport_factory_name, --transport=transport_factory_name
Use the provided Thrift transport factory function.
-u user_name
Authenticate as user. Default = cassandra.
--version
Show the cqlsh version.

cqlshrc options 

You can create a cqlshrc file. In Cassandra 1.2.9 and later the file resides in the hidden .cassandra directory in your home directory. You configure the cqlshrc file by setting these options in the [authenication], [ui], or [ssl] sections of the file.

[ui] options are:

color
Always use color output.
completekey
Use this key for autocompletion of a cqlsh shell entry. Default is the tab key.
float_precision
Use this many decimal digits of precision. Default = 5.
time_format
Configure the output format of database objects of the timestamp type. For example, a yyyy-mm-dd HH:mm:ssZ formatting produces this timestamp: 2014-01-01 12:00:00GMT. Default = '%Y-%m-%d %H:%M:%S%z'.

[authentication] options are:

keyspace
Use the given keyspace. Equivalent to issuing a USE keyspace command immediately after starting cqlsh.
password
Authenticate using password.
username
Authenticate as user.

[ssl] options are covered in the Cassandra documentation.

Using CQL commands 

On startup, cqlsh shows the name of the cluster, IP address, and the port used for connection to the cqlsh utility. The cqlsh prompt initially is cqlsh>. After you specify a keyspace to use, the prompt includes the name of the keyspace. For example:

$ cqlsh 1.2.3.4 9160 -u jdoe -p mypassword
Connected to trace_consistency at 1.2.3.4:9160.
[cqlsh 4.1.0 | Cassandra 2.0.2-SNAPSHOT | CQL spec 3.1.1 | Thrift protocol 19.38.0]
Use HELP for help.
cqlsh>USE mykeyspace;
cqlsh:mykeyspace>
At the cqlsh prompt, type CQL commands. Use a semicolon to terminate a command. A new line does not terminate a command, so commands can be spread over several lines for clarity.
cqlsh> USE demo_cl;
cqlsh:demo_cl> SELECT * FROM demo_table
           ... WHERE id = 0;
If a command is sent and executed successfully, results are sent to standard output.

The lexical structure of commands, covered earlier in this reference, includes how upper- and lower-case literals are treated in commands, when to use quotation marks in strings, and how to enter exponential notation.

Using files as input 

To execute CQL commands in a file, use the -f option and the path to the file on the operating system command line. Or, after you start cqlsh, use the SOURCE command and the path to the file on the cqlsh command line.

Creating and using a cqlshrc file  

When present, the cqlshrc file can pass default configuration information to cqlsh. A sample file looks like this:
; Sample ~/.cassandra/cqlshrc file.
[authentication]
username = fred
password = !!bang!!
The Cassandra installation includes a cqlshrc.sample file in the conf directory. On Windows, in Command Prompt, create this file by copying the cqlshrc.sample file from the conf directory to the hidden .cassandra folder your user home folder, and renaming it to cqlshrc.

You can use a cqlshrc file to configure SSL encryption instead of overriding the SSL_CERTFILE environmental variables repeatedly. Cassandra internal authentication must be configured before users can use the authentication options.