Tuesday, October 5, 2010

How to run Cassandra and basics with Cassandra

Once you have setup Cassandra on your machine , this is how you ll need to proceed to get started with how Cassandra works .



Assuming you have installed Cassandra to /opt

Starting cassandra : /opt/cassandra/bin/cassandra

Now to get into the command mode:

/opt/cassandra

bin/cassandra-cli --host localhost --port 9160


Creating a keyspace :

<keyspace Name="Prototype">
<columnfamily Name="master" CompareWith="UTF8Type" />
<replicaplacementstrategy>org.apache.cassandra.locator.RackUnawareStrategy</ReplicaPlacementStrategy>
<replicationfactor>1</ReplicationFactor>
<endpointsnitch>org.apache.cassandra.locator.EndPointSnitch</EndPointSnitch>
</Keyspace>

KeySpace - compare it to database (Prorotype)
ColumFamily - compare it with a table (master)


Setting a value :

set Prototype.master['1']['name'] = 'Arun'
set Prototype.master['1']['age'] = '22'

setting value -> keyspace.columnfamily['rowKey']['colum']='value'

Rowkey is like the primary key , For eg: 1 is the primary key here , it will contain all properties of Arun
as different columns name, age etc...


Retrieval of data :

get Prototype.master['1']['name']
=> (column=name, value=Arun, timestamp=1286265373093000)

This will get only the value in the name colum of rowKey 1


get Prototype.master['1']

This will get you all the columns for the rowkey '1'

count Prototype.master['1']

will get the count of columns availble for rowkey '1'


FYI :------------------ Row keys are case sensitive ------------------

count Prototype.master['2']

will give you 0 columns exist , don't worry it will not throw a no column exist error.


But this is not the case for columns :

get Prototype.master['1']['friendlist']
Exception null

column friendlist does not exist , and it throws a exception (------------Something to be careful about--------------------)

No comments:

Post a Comment