Differences between revisions 44 and 45
Revision 44 as of 2013-11-13 01:09:15
Size: 3442
Editor: GehrigKunz
Comment: statcounter
Revision 45 as of 2013-11-13 18:21:59
Size: 4059
Comment: start nudging people to cqlsh
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Cassandra ships with a very basic interactive command line interface. Using the CLI you can connect to remote nodes in the cluster to create or update your schema and set and retrieve records. == Use cqlsh Instead ==
The Cassandra CLI is a holdover from the pre-CQL Thrift [[API]]. You should migrate to [[http://www.datastax.com/documentation/cql/3.1/webhelp/index.html#cql/cql_reference/about_cql_ref_c.html|cqlsh]], which supports everything you could do from the CLI. How does your Thrift data map to CQL? See these three articles:
Line 3: Line 4:
The examples below have been tested with Cassandra 1.0.6. For instructions for previous versions of Cassandra see one of: [[CassandraCli07]], [[CassandraCli06]].  * [[http://www.datastax.com/dev/blog/cql3-for-cassandra-experts|CQL for Cassandra experts]]
 * [[http://www.datastax.com/dev/blog/thrift-to-cql3|A thrift to CQL upgrade guide]]
 * [[http://www.datastax.com/dev/blog/does-cql-support-dynamic-columns-wide-rows|Does CQL support dynamic columns and wide rows?]]

The CLI will be formally deprecated starting with Cassandra 2.1 and removed in 3.0.

== Older versions of the CLI ==

The examples below have been tested with Cassandra 1.0.6. For instructions for previous versions of Cassandra see one of: CassandraCli07, CassandraCli06.
Line 8: Line 17:
You can start the CLI using the {{{bin/cassandra-cli}}} script in your Cassandra installation ({{{bin\cassandra-cli.bat}}} on windows). If you are evaluating a local cassandra node then be sure that it has been correctly configured and successfully started before starting the CLI.  You can start the CLI using the {{{bin/cassandra-cli}}} script in your Cassandra installation ({{{bin\cassandra-cli.bat}}} on windows). If you are evaluating a local cassandra node then be sure that it has been correctly configured and successfully started before starting the CLI.
Line 11: Line 20:
Line 16: Line 26:
You must then specify a system to connect to:
Line 17: Line 28:
You must then specify a system to connect to:
Line 21: Line 31:
Line 24: Line 33:
Line 27: Line 37:
Line 30: Line 39:
Line 33: Line 43:
Line 36: Line 45:
Line 39: Line 49:
For the later examples to work you must also update the schema using the following command. This will set the return type for the first and last name to make them human readable. It will also add and index for the age field so that you filter your gets using the Users name field.
Line 40: Line 51:
For the later examples to work you must also update the schema using the following command. This will set the return type for the first and last name to make them human readable. It will also add and index for the age field so that you filter your gets using the Users name field.
Line 43: Line 53:
 column_metadata =
 [
 {column_name: first, validation_class: UTF8Type},
 {column_name: last, validation_class: UTF8Type},
 {column_name: age, validation_class: UTF8Type, index_type: KEYS}
 ];
        column_metadata =
        [
        {column_name: first, validation_class: UTF8Type},
        {column_name: last, validation_class: UTF8Type},
        {column_name: age, validation_class: UTF8Type, index_type: KEYS}
        ];
Line 50: Line 60:
Line 55: Line 64:
Line 58: Line 68:
and then we add our data.
Line 59: Line 70:
and then we add our data.
Line 65: Line 75:
Line 69: Line 78:
Line 71: Line 81:
Line 74: Line 85:
Line 77: Line 87:
Line 80: Line 91:
Line 87: Line 97:
Line 92: Line 101:
Line 97: Line 105:
Line 102: Line 109:

Use cqlsh Instead

The Cassandra CLI is a holdover from the pre-CQL Thrift API. You should migrate to cqlsh, which supports everything you could do from the CLI. How does your Thrift data map to CQL? See these three articles:

The CLI will be formally deprecated starting with Cassandra 2.1 and removed in 3.0.

Older versions of the CLI

The examples below have been tested with Cassandra 1.0.6. For instructions for previous versions of Cassandra see one of: CassandraCli07, CassandraCli06.

You may also be interested in the DataStax documentation which has excellent coverage on this topic.

Starting the CLI

You can start the CLI using the bin/cassandra-cli script in your Cassandra installation (bin\cassandra-cli.bat on windows). If you are evaluating a local cassandra node then be sure that it has been correctly configured and successfully started before starting the CLI.

If successful you will see output similar to this:

Welcome to cassandra CLI.

Type 'help;' or '?' for help. Type 'quit;' or 'exit;' to quit.

You must then specify a system to connect to:

connect localhost/9160;

Creating a Keyspace

We first create a keyspace to run our examples in.

create keyspace Twissandra;

Selecting the keyspace to user

We must then select our example keyspace as our new context before we can run any queries.

use Twissandra;

To Create A Column

We can then create a column to play with.

create column family User with comparator = UTF8Type;

For the later examples to work you must also update the schema using the following command. This will set the return type for the first and last name to make them human readable. It will also add and index for the age field so that you filter your gets using the Users name field.

update column family User with
        column_metadata =
        [
        {column_name: first, validation_class: UTF8Type},
        {column_name: last, validation_class: UTF8Type},
        {column_name: age, validation_class: UTF8Type, index_type: KEYS}
        ];

To Add Data

To add data we want to into our new column we must first specify our default key type otherwise we would have to specify it for each key using the format [utf8('keyname')] this is probably advisable if you have mixed key types but makes simple cases harder to read.

So we run the command below, which will last the length of you cli session. On quitting and restarting we must run it again.

assume User keys as utf8;

and then we add our data.

set User['jsmith']['first'] = 'John';
set User['jsmith']['last'] = 'Smith';
set User['jsmith']['age'] = '38';

If you get the error like this cannot parse 'John' as hex bytes, then it likely you either haven't set your default key type or you haven't updated your schema as in the create column example.

The set command uses API#insert

To Update Data

If we need to update a value we simply set it again.

set User['jsmith']['first'] = 'Jack';

To Get Data

Now let's read back the jsmith row to see what it contains:

get User['jsmith'];

The get command uses API#get_slice

To Query Data

get User where age = '12';

For help

help;

To Quit

quit;

To Execute Script

bin/cassandra-cli -host localhost -port 9160 -f script.txt

stats

CassandraCli (last edited 2013-11-13 18:21:59 by JonathanEllis)