How to install, configure and use Cassandra NoSQL database

2000px-Cassandra_logo.svg

 

Introduction

Cassandra or Apache Cassandra is an open source distributed database management system designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure. Cassandra offers robust support for clusters spanning multiple datacenters, with asynchronous masterless replication allowing low latency operations for all clients.

Cassandra also places a high value on performance.

Cassandra’s data model is a partitioned row store with tunable consistency. Rows are organized into tables; the first component of a table’s primary key is the partition key; within a partition, rows are clustered by the remaining columns of the key. Other columns may be indexed separately from the primary key.

Tables may be created, dropped, and altered at runtime without blocking updates and queries.

Cassandra does not support joins or subqueries. Rather, Cassandra emphasizes denormalization through features like collections.

Install Java JDK

sudo apt-get install openjdk-7-jdk

To make it the default Java VM on your VPS, add these lines to the /etc/profile file:

sudo nano /etc/profile

JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
JRE_HOME=/usr/lib/jvm/java-7-openjdk-amd64
PATH=$PATH:$HOME/bin:$JRE_HOME/bin
export JAVA_HOME
export JRE_HOME
export PATH

After you’ve done this, press CTRL+O to save and CTRL+X to exit

 

1

 

Check the Environment Variable has been set or not

echo $JAVA_HOME

If the Environment Variable not set, Please set it manually by below command

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64

Again Check Environment variable has been set or not

echo $JAVA_HOME

Next, run the following lines so when the command “java” is called, it’s done so by the right VM.

sudo update-alternatives –install “/usr/bin/java” “java” “/usr/lib/jvm/java-7-openjdk-amd64/bin/java” 1
sudo update-alternatives –install “/usr/bin/javac” “javac” “/usr/lib/jvm/java-7-openjdk-amd64/bin/javac” 1
sudo update-alternatives –install “/usr/bin/javaws” “javaws” “/usr/lib/jvm/java-7-openjdk-amd64/bin/javaws” 1
sudo update-alternatives –set java /usr/lib/jvm/java-7-openjdk-amd64/bin/java
sudo update-alternatives –set javac /usr/lib/jvm/java-7-openjdk-amd64/bin/javac
sudo update-alternatives –set javaws /usr/lib/jvm/java-7-openjdk-amd64/bin/javaws
You should now reboot so everything is updated:

sudo reboot

Installing Cassandra

To install Cassandra, download the binary files from the website, unpack them and move it to your personal folder:

mkdir /root/temp

cd /root/temp/

wget http://www.us.apache.org/dist/cassandra/2.1.5/apache-cassandra-2.1.5-bin.tar.gz

tar -xzvf apache-cassandra-2.1.5-bin.tar.gz

mv apache-cassandra-2.1.5 cassandra

Next, make sure that the folders Cassandra accesses, such as the log folder, exists and that Cassandra has the right to write on it:

sudo mkdir /var/lib/cassandra
sudo mkdir /var/log/cassandra
sudo chown -R $USER:$GROUP /var/lib/cassandra
sudo chown -R $USER:$GROUP /var/log/cassandra

Now set Cassandra’s variables by running:

export CASSANDRA_HOME=/root/temp/cassandra
export PATH=$PATH:$CASSANDRA_HOME/bin

Running Cassandra

To run a single-node test cluster of Cassandra, you aren’t going to need to change anything on the cassandra.yaml file. Simply run:

cd /root/temp/cassandra/bin

./cassandra

and then run:

./cassandra-cli

and if it says “Connected to: ‘Test Cluster'” as it does on the image below, you are now running your single-node cluster.

2

 

Starting cqlsh

Start cqlsh using the command cqlsh as shown below. It gives the Cassandra cqlsh prompt as output.

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

The HELP command displays a synopsis and a brief description of all cqlsh commands. Given below is the usage of help command.

cqlsh> help

Documented shell commands:
===========================
CAPTURE COPY DESCRIBE EXPAND PAGING SOURCE
CONSISTENCY DESC EXIT HELP SHOW TRACING.

CQL help topics:
================
ALTER CREATE_TABLE_OPTIONS SELECT
ALTER_ADD CREATE_TABLE_TYPES SELECT_COLUMNFAMILY
ALTER_ALTER CREATE_USER SELECT_EXPR
ALTER_DROP DELETE SELECT_LIMIT
ALTER_RENAME DELETE_COLUMNS SELECT_TABLE

Create Keyspace

CREATE KEYSPACE “KeySpace Name”
WITH replication = {‘class’: ‘Strategy name’, ‘replication_factor’ : ‘No.Of replicas’};

Create Tables

CREATE TABLE tablename(
column1 name datatype PRIMARYKEY,
column2 name data type,
column3 name data type.
)

Create Index

CREATE INDEX <identifier> ON <tablename>

560 Comments

Add a Comment

Your email address will not be published. Required fields are marked *