Monday, March 9, 2015

Cassandra Installation and Introduction

Cassandra is a no sql database and known for massive horizontal scaling with replication feature built in. You can install Cassandra from the instruction at http://cassandra.apache.org/download/ . Cassandra can be installed either fetching it from the repositories or a tar package can be downloaded from the above download link. I will proceed with downloading the tar package.

Steps to Install Cassandra:
  • Download the tar file from one of the mirrors.   wget  <Cassandra Mirror location>
  • Untar the file - tar -xzvf apache-cassandra-2.1.3-bin.tar.gz 
  • Go to extracted directory of cassandra.
  • Run Cassandra  sudo bin/cassandra -f  This will run cassandra as a forward process. Running it without -f switch will make to run as daemon.
A very good reference for Cassandra support for various operating systems and languages is at http://planetcassandra.org/cassandra/

cqlsh

Those who come from sql background can quickly relate to this. Cassandra comes with cql (Cassandra Query language). This will smell like SQL. To start the shell of cql issues the following command from cassandra installation directory

bin/cqlsh

You will see the following message being shown and the cql console will come

Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 2.1.3 | CQL spec 3.2.0 | Native protocol v3]
Use HELP for help.
cqlsh>

Cassandra works on the concept of keyspaces. A keyspace is a namespace for a set of data. You can define schemas inside a keyspace.

To make a keyspace issue the following command

create keyspace firstKeySpace with replication = {'class' : 'SimpleStrategy', 'replication_factor': 1};

Also you will have to tell cassandra that you wants to work in the context of this keyspace.

use firstKeySpace;

Now creating a table is pretty similar to SQL table creation.

create table students (student_id int PRIMARY KEY,
                                   first_name text,
                                   last_name text);

Inserting some data

INSERT INTO students(student_id ,  first_name, last_name)
  VALUES (1, 'Ishana', 'Bhatt');
INSERT INTO students(student_id ,  first_name, last_name)
  VALUES (2, 'Ekagra', 'Bhatt');
INSERT INTO students(student_id ,  first_name, last_name)
  VALUES (3, 'Ipsita', 'Bhatt');
INSERT INTO students(student_id ,  first_name, last_name)
  VALUES (4, 'Akshit', 'Bhatt');

Getting the list of inserted data

select * from students;

student_id | first_name | last_name
------------+------------+-----------
          1 |     Ishana |     Bhatt
          2 |     Ekagra |     Bhatt
          4 |     Akshit |     Bhatt
          3 |     Ipsita |     Bhatt

Boringly similar to SQL. 

No comments:

Post a Comment