Chủ Nhật, 5 tháng 2, 2012

Cassandra Client

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

Install Cassandra on Windows

As I said before you can run from an operating system that Java has a runtime for. So the first and probably most obvious one for a Windows developer, is running Cassandra on Windows. To install Cassandra on windows just follow these steps:

  1. Extract Cassandra to a directory of your choice (I used c:\development\cassandra)
  2. Set the following environment variables
    1. JAVA_HOME (To the directory where you install the JRE, this should not be the bin directory)
    2. CASSANDRA_HOME (To the directory you extracted the files to in step 1)
  3. Modify your Cassandra config file as you like and don’t forget to update the directory locations from a UNIX like path to something on your windows directory (in my example the config file is located at c:\development\cassandra\conf\storage-conf.xml)
  4. Open cmd and run the cassandra.bat file (in my example the batch file is located at c:\development\cassandra\bin\cassandra.bat)
    1
    2
    cd c:\development\cassandra\bin\
    .\cassandra.bat
  5. You can verify that Cassandra is running, by trying to connect to the server. To do this open a new cmd and run the cassandra-cli.bat file from the bin directory.
    1
    2
    3
    cd c:\development\cassandra\bin\
    .\cassandra-cli.bat
    connect localhost/9160

This is easy to get running, but there is some manual process that you have to go through each time to get the server running. In the future when you want to start up the Cassandra database for development, just repeat Step 4.