Sunday, October 31, 2010

Cassandra NoSQL database steps and CRUD

GOOD TUTORAIL:

Cassandra is a highly scalable, eventually consistent, distributed, structured
key-value store.


Project description
-------------------

Cassandra brings together the distributed systems technologies from Dynamo
and the data model from Google's BigTable. Like Dynamo, Cassandra is
eventually consistent. Like BigTable, Cassandra provides a ColumnFamily-based
data model richer than typical key/value systems.

For more information see http://cassandra.apache.org/

Requirements
------------
* Java >= 1.6 (OpenJDK and Sun have been tested)

Getting started
---------------

This short guide will walk you through getting a basic one node cluster up
and running, and demonstrate some simple reads and writes.

* tar -zxvf apache-cassandra-$VERSION.tar.gz
* cd apache-cassandra-$VERSION
* sudo mkdir -p /var/log/cassandra
* sudo chown -R `whoami` /var/log/cassandra
* sudo mkdir -p /var/lib/cassandra
* sudo chown -R `whoami` /var/lib/cassandra

Note: The sample configuration files in conf/ determine the file-system
locations Cassandra uses for logging and data storage. You are free to
change these to suit your own environment and adjust the path names
used here accordingly.

Now that we're ready, let's start it up!

* bin/cassandra -f

Running the startup script with the -f argument will cause Cassandra to
remain in the foreground and log to standard out.

Now let's try to read and write some data using the command line client.

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

The command line client is interactive so if everything worked you should
be sitting in front of a prompt...

Connected to localhost/9160
Welcome to cassandra CLI.

Type 'help' or '?' for help. Type 'quit' or 'exit' to quit.
cassandra>

As the banner says, you can use 'help' or '?' to see what the CLI has to
offer, and 'quit' or 'exit' when you've had enough fun. But lets try
something slightly more interesting...

cassandra> set Keyspace1.Standard2['jsmith']['first'] = 'John'
Value inserted.
cassandra> set Keyspace1.Standard2['jsmith']['last'] = 'Smith'
Value inserted.
cassandra> set Keyspace1.Standard2['jsmith']['age'] = '42'
Value inserted.
cassandra> get Keyspace1.Standard2['jsmith']
(column=age, value=42; timestamp=1249930062801)
(column=first, value=John; timestamp=1249930053103)
(column=last, value=Smith; timestamp=1249930058345)
Returned 3 rows.
cassandra>

If your session looks similar to what's above, congrats, your single node
cluster is operational! But what exactly was all of that? Let's break it
down into pieces and see.

set Keyspace1.Standard2['jsmith']['first'] = 'John'
\ \ \ \ \
\ \ \_ key \ \_ value
\ \ \_ column
\_ keyspace \_ column family

Data stored in Cassandra is associated with a column family (Standard2),
which in turn is associated with a keyspace (Keyspace1). In the example
above, we set the value 'John' in the 'first' column for key 'jsmith'.

For more information on the Cassandra data model be sure to checkout
http://wiki.apache.org/cassandra/DataModel

Wondering where to go from here?

* The wiki (http://wiki.apache.org/cassandra/) is the
best source for additional information.
* Join us in #cassandra on irc.freenode.net and ask questions.
* Subscribe to the Users mailing list by sending a mail to
user-subscribe@cassandra.apache.org

HANDS ON PRACTICE:


[root@godtamil softwares]# cd apache-cassandra-0.6.6
[root@godtamil apache-cassandra-0.6.6]# bin/cassandra-cli --host localhost --port 9160
Connected to: "Test Cluster" on localhost/9160
Welcome to cassandra CLI.

Type 'help' or '?' for help. Type 'quit' or 'exit' to quit.
cassandra> ?
List of all CLI commands:
? Same as help.
help Display this help.
connect / Connect to thrift service.
describe keyspace Describe keyspace.
exit Exit CLI.
quit Exit CLI.
show config file Display contents of config file.
show cluster name Display cluster name.
show keyspaces Show list of keyspaces.
show api version Show server API version.
get .[''] Get a slice of columns.
get .[''][''] Get a slice of sub columns.
get .[''][''] Get a column value.
get .[''][''][''] Get a sub column value.
set .[''][''] = '' Set a column.
set .[''][''][''] = '' Set a sub column.
del .[''] Delete record.
del .[''][''] Delete column.
del .[''][''][''] Delete sub column.
count .[''] Count columns in record.
count .[''][''] Count columns in a super column.
cassandra> show api version
2.2.0
cassandra> show keyspaces
Keyspace1
system


cassandra> set Keyspace1.Standard1.['murugar'].['first']='lord';

cassandra> set Keyspace1.Standard1.['murugar'].['first']=['lord'];

cassandra> set Keyspace1.Standard1.['murugar']['first']=['lord'];
line 1:23 extraneous input '.' expecting '['
line 1:45 extraneous input '[' expecting StringLiteral
Value inserted.




=> (column=6669727374, value=lord, timestamp=1288333757977000)
Returned 1 results.
cassandra> get Keyspace1.Standard1

cassandra> get Keyspace1.Standard1['murugar']
=> (column=6669727374, value=lord, timestamp=1288333757977000)
Returned 1 results.
cassandra> get Keyspace1.S


cassandra> get Keyspace1.Standard2['murugar']
Returned 0 results.

cassandra> get Keyspace1.Standard1['murugar']
=> (column=6669727374, value=lord, timestamp=1288333757977000)
Returned 1 results.


cassandra> get Keyspace1.Standard1['murugar']
=> (column=6669727374, value=lord, timestamp=1288334054007000)
Returned 1 results.
cassandra> set Keyspace1.Standard1['murugar']['first']='lord'
Value inserted.

cassandra> get Keyspace1.Standard1['murugar']
=> (column=6669727374, value=lord, timestamp=1288334104678000)
Returned 1 results.


Type 'help' or '?' for help. Type 'quit' or 'exit' to quit.
cassandra> set Keyspace1.S

set Keyspace1.Standard1 set Keyspace1.Standard2 set Keyspace1.StandardByUUID1 set Keyspace1.Super1 set Keyspace1.Super2
cassandra> set Keyspace1.Standard2['murugar']['first']='lord'
Value inserted.
cassandra> set Keyspace1.S

set Keyspace1.Standard1 set Keyspace1.Standard2 set Keyspace1.StandardByUUID1 set Keyspace1.Super1 set Keyspace1.Super2
cassandra> set Keyspace1.Standard

set Keyspace1.Standard1 set Keyspace1.Standard2 set Keyspace1.StandardByUUID1
cassandra> set Keyspace1.Standard2['murugar']['last']='alzhakar'
Value inserted.
set Keyspace1.Standard2['murugar']['age']='ageless'
Value inserted.
cassandra> get Keyspace1.Standard2
line 0:-1 mismatched input '' expecting '['
cassandra> get Keyspace1.Standard2['murugar']
=> (column=last, value=alzhakar, timestamp=1288334425977000)
=> (column=first, value=lord, timestamp=1288334370929000)
=> (column=age, value=ageless, timestamp=1288334457607000)
Returned 3 results.
cset Keyspace1.Standard2['selva']['first']='selva' ' '
Value inserted.
cassandra> set Keyspace1.Standard2['selva']['last']='kumar'
Value inserted.
cassandra> set Keyspace1.Standard2['selva']['age']='20'
Value inserted.
cassandra> get Keyspace1.Standard2['selva']
=> (column=last, value=kumar, timestamp=1288334652697000)
=> (column=first, value=selva, timestamp=1288334637395000)
=> (column=age, value=20, timestamp=1288334691443000)
Returned 3 results.
cassandra> get Keyspace1.Standard2['selva']['first']
=> (column=first, value=selva, timestamp=1288334637395000)
cassandra> get Keyspace1.Standard2['selva']['age']
=> (column=age, value=20, timestamp=1288334691443000)
cassandra> del Keyspace1.S

DELETION:
del Keyspace1.Standard1 del Keyspace1.Standard2 del Keyspace1.StandardByUUID1 del Keyspace1.Super1 del Keyspace1.Super2
cassandra> del Keyspace1.Standard['selva']['like']
No such column family: Standard
cassandra> del Keyspace1.Standard2['selva']['like']
column removed.
cassandra> del Keyspace1.Standard2['selva']
row removed.
cassandra> get Keyspace1.Standard2['selva']
Returned 0 results.
cassandra> get Keyspace1.Standard2['mugrugar']
Returned 0 results.
cassandra> get Keyspace1.Standard2['murugar']
=> (column=last, value=alzhakar, timestamp=1288334425977000)
=> (column=first, value=lord, timestamp=1288334370929000)
=> (column=age, value=ageless, timestamp=1288334457607000)
Returned 3 results.

SUPER COLUMN:

cassandra> set Keyspace1.Standard2['selva']['post1']['content']='cloudcomputing'
supercolumn parameter is invalid for standard CF Standard2
cassandra> set Keyspace1.Super1['selva']['post1']['content']='cloudcomputing'
Value inserted.
cassandra> get Keyspace1.Super1['selva']['post1']['content']
=> (column=636f6e74656e74, value=cloudcomputing, timestamp=1288335271397000)
cassandra> set Keyspace1.Super2['selva']['post1']['content']='NoSql'
Value inserted.
cassandra> get Keyspace1.Super2['selva']['post1']['content']
=> (column=content, value=NoSql, timestamp=1288335348079000)
cassandra> del Keyspace1.Super2['selva']['post1']['content']
column removed.
cassandra> get Keyspace1.Super2['selva']['post1']['content']
Exception null
cassandra> set Keyspace1.Super1['selva']['post1']['content']='cloudcomputing'
Value inserted.
cassandra> set Keyspace1.Super2['selva']['post1']['content']='NoSql'
Value inserted.
cassandra> get Keyspace1.Super2['selva']['post1']['content']
=> (column=content, value=NoSql, timestamp=1288335511929000)
cassandra> get Keyspace1.Super2['post1']['selva']['content']='cloudcomputing'
line 1:49 missing EOF at '='
Exception null
cassandra> set Keyspace1.Super2['post1']['selva']['content']='cloudcomputing'
Value inserted.
cassandra> set Keyspace1.Super2['post2']['selva']['content']='NoSql'
Value inserted.
cassandra> get Keyspace1.Super2['post2']['selva']['content']
=> (column=content, value=NoSql, timestamp=1288335606237000)
cassandra> get Keyspace1.Super2['post1']['selva']['content']
=> (column=content, value=cloudcomputing, timestamp=1288335587702000)
cassandra> get Keyspace1.Super2['selva']['content']
Returned 0 results.
cassandra> get Keyspace1.Super2['post1']['selva']
=> (column=content, value=cloudcomputing, timestamp=1288335587702000)
Returned 1 results.
cassandra> get Keyspace1.Super2['post1']
=> (super_column=selva,
(column=content, value=cloudcomputing, timestamp=1288335587702000))
Returned 1 results.
cassandra> set Keyspace1.Super2['selva']['post1']['content']='cloud computing'
Value inserted.
cassandra> set Keyspace1.Super2['selva']['post2']['content']='nosql'
Value inserted.
cassandra> get Keyspace1.Super2['selva']['post2']['content']
=> (column=content, value=nosql, timestamp=1288336013459000)
cassandra> get Keyspace1.Super2['selva']['post1']['content']
=> (column=content, value=cloud computing, timestamp=1288335996832000)
cassandra> get Keyspace1.Super2['selva']['post1']
=> (column=content, value=cloud computing, timestamp=1288335996832000)
Returned 1 results.
cassandra> get Keyspace1.Super2['selva']
=> (super_column=post2,
(column=content, value=nosql, timestamp=1288336013459000))
=> (super_column=post1,
(column=content, value=cloud computing, timestamp=1288335996832000))
Returned 2 results.
cassandra> count Keyspace1.S

count Keyspace1.Standard1 count Keyspace1.Standard2 count Keyspace1.StandardByUUID1 count Keyspace1.Super1 count Keyspace1.Super2
cassandra> count Keyspace1.Standard2['murugar']
3 columns
cassandra> count Keyspace1.Standard2['selva']
0 columns
cassandra> set Keyspace1.Standard2['selva']['first']='selva'
Value inserted.
cassandra> set Keyspace1.Standard2['selva']['last']='ku mar'
Value inserted.
cassandra> get Keyspace1.Standard2['selva']
=> (column=last, value=ku mar, timestamp=1288336154687000)
=> (column=first, value=selva, timestamp=1288336142197000)
Returned 2 results.
cassandra> count Keyspace1.Standard2['selva']
2 columns


COUNTING:

cassandra> count Keyspace1.Super2.['selva']['post1']
line 1:22 extraneous input '.' expecting '['
1 columns
cassandra> count Keyspace1.Super2['selva']['post1']
1 columns
cassandra> count Keyspace1.Super2['selva']['post2']
1 columns
cassandra> count Keyspace1.Super2['selva']
2 columns


more doubt means contact me:selvawithedu@gmail.com (knowledge should spread over the world)
REFERENCES:

1.http://www.klunde.net/tag/cassandra/
2.http://blog.nowvu.com/2010/08/19/cassandra-and-php-become-friendly-with-phpcassa/
3.http://www.sodeso.nl/?p=80