Monday, May 18, 2015

Cassandra | Setup

We learnt about Cassandra in previous post. We will setup and run client on an Cassandra cluster(fully distributed) here.


For installation we will use three nodes. We will install fully distributed Cassandra cluster. Here we are using following details for installation (for complete setup):

  • Installation base directory:
      • /home/anishsneh/installs
  • Installation user name:
      • anishsneh
  • Hostnames: 
      • server01 (first node, say with ip address
      • server02 (second node, say with ip address
      • server03 (third node, say with ip address
Note that in Cassandra there is NO SINGLE POINT OF FAILURE, hence all the nodes are equal and there is no MASTER or SLAVE.

  • Install Cassandra
    • Download Apache Cassandra binary from Apache Website.
    • Extract downloaded package to /home/anishsneh/installs, such that we have:
      [anishsneh@server01 installs]$ ls -ltr apache-cassandra-2.1.5/
      total 360
      -rw-r--r--. 1 anishsneh anishsneh   2117 Apr 27 07:33 NOTICE.txt
      -rw-r--r--. 1 anishsneh anishsneh  64431 Apr 27 07:33 NEWS.txt
      -rw-r--r--. 1 anishsneh anishsneh  11609 Apr 27 07:33 LICENSE.txt
      -rw-r--r--. 1 anishsneh anishsneh 245971 Apr 27 07:33 CHANGES.txt
      drwxr-xr-x. 2 anishsneh anishsneh   4096 May 17 15:37 interface
      drwxr-xr-x. 4 anishsneh anishsneh   4096 May 17 15:37 javadoc
      drwxr-xr-x. 3 anishsneh anishsneh   4096 May 17 15:37 lib
      drwxr-xr-x. 3 anishsneh anishsneh   4096 May 17 15:37 pylib
      drwxr-xr-x. 4 anishsneh anishsneh   4096 May 17 15:37 tools
      drwxr-xr-x. 2 anishsneh anishsneh   4096 May 17 15:37 bin
      drwxrwxr-x. 2 anishsneh anishsneh   4096 May 17 15:51 logs
      drwxrwxr-x. 5 anishsneh anishsneh   4096 May 17 15:51 data
      drwxr-xr-x. 3 anishsneh anishsneh   4096 May 17 16:46 conf
    • Repeat above steps for all the three nodes.
  • Configure Cluster 
    • Set CASSANDRA_HOME="/home/anishsneh/installs/apache-cassandra-2.1.5" in ~/.bashrc (or wherever maintaining environment variables), reload profile/bash.
    • On first node edit $CASSANDRA_HOME/conf/cassandra.yaml with following:
      cluster_name: 'HELLO_CLUSTER'
      seeds: ",,"
      Here we are assuming first node has ip address Note that other properties like data_file_directories, commitlog_directory can be changed if needed.
    • On first node make changes to the following properties in the script $CASSANDRA_HOME/conf/ Uncomment/Update
      JVM_OPTS="$JVM_OPTS -Djava.rmi.server.hostname="
      Here we are assuming first node has the ip address
    • Repeat above steps for all the three nodes (with their respective ip addresses)
  • Start/Run Cluster
    • Execute $CASSANDRA_HOME/bin/cassandra on all the three nodes, it will start Cassandra server on all the three nodes and all the three server will join a cluster (as per the information provided in cassandra.yaml)
  • Verify Cluster
    • On one of the nodes go to $CASSANDRA_HOME/bin and execute following command:
      [anishsneh@server01 bin]$ ./nodetool -h server01 status
      Datacenter: datacenter1
      |/ State=Normal/Leaving/Joining/Moving
      --  Address        Load       Tokens  Owns (effective)  Host ID                               Rack
      UN  188.19 KB  256     64.9%             0fc990e2-c257-4dfc-aec0-b151efd634d7  rack1
      UN  187.5 KB   256     67.8%             ba280c97-295c-4056-85f0-3c11594a3676  rack1
      UN  153.47 KB  256     67.3%             3a670717-401c-419a-8b89-73c1426df67b  rack1
      We may execute few more commands like:
      [anishsneh@server01 bin]$ ./nodetool -h server01 version
      ReleaseVersion: 2.1.5
      [anishsneh@server01 bin]$ ./nodetool -h server01 info
      ID                     : 0fc990e2-c257-4dfc-aec0-b151efd634d7
      Gossip active          : true
      Thrift active          : true
      Native Transport active: true
      Load                   : 188.19 KB
      Generation No          : 1431991363
      Uptime (seconds)       : 537
      Heap Memory (MB)       : 84.14 / 484.00
      Off Heap Memory (MB)   : 0.00
      Data Center            : datacenter1
      Rack                   : rack1
      Exceptions             : 0
      Key Cache              : entries 11, size 824 bytes, capacity 24 MB, 21 hits, 38 requests, 0.553 recent hit rate, 14400 save period in seconds
      Row Cache              : entries 0, size 0 bytes, capacity 0 bytes, 0 hits, 0 requests, NaN recent hit rate, 0 save period in seconds
      Counter Cache          : entries 0, size 0 bytes, capacity 12 MB, 0 hits, 0 requests, NaN recent hit rate, 7200 save period in seconds
      Token                  : (invoke with -T/--tokens to see all 256 tokens)

CQLSH Client

Cassandra is shipped with a very useful command line client CQLSH which is a shell for CQL (Cassandra Query Language). It is an interactive command line interface for Cassandra. We will connect to Cassandra cluster using CQLSH here and execute various CRUD operations. CQLSH can be launched using command $CASSANDRA_HOME/bin/cqlsh script on any of the nodes (or where Cassandra is installed):
[anishsneh@server01 bin]$ ./cqlsh server01
Connected to HELLO_CLUSTER at server01:9042.
[cqlsh 5.0.1 | Cassandra 2.1.5 | CQL spec 3.2.0 | Native protocol v3]
Use HELP for help.
cqlsh> CREATE KEYSPACE IF NOT EXISTS demo_keyspace WITH replication={'class' : 'SimpleStrategy', 'replication_factor':1};
Use the created KEYSPACE:
cqlsh> USE demo_keyspace;
cqlsh:demo_keyspace> CREATE TABLE IF NOT EXISTS demo_table(id varchar, login varchar, full_name varchar, country_code varchar, PRIMARY KEY(id));
Describe KEYSPACE:
cqlsh:demo_keyspace> DESCRIBE KEYSPACE demo_keyspace;

CREATE KEYSPACE demo_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}  AND durable_writes = true;

CREATE TABLE demo_keyspace.demo_table (
    id text PRIMARY KEY,
    country_code text,
    full_name text,
    login text
) WITH bloom_filter_fp_chance = 0.01
    AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
    AND comment = ''
    AND compaction = {'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32'}
    AND compression = {'sstable_compression': ''}
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99.0PERCENTILE';

Insert records to COLUMN FAMILY
cqlsh:demo_keyspace> INSERT INTO demo_table(id, login, full_name, country_code) values('USR0000001', 'anishsneh', 'Anish Sneh', 'IN');
cqlsh:demo_keyspace> INSERT INTO demo_table(id, login, full_name, country_code) values('USR0000002', 'rakeshk', 'Rakesh K', 'UK');
cqlsh:demo_keyspace> INSERT INTO demo_table(id, login, full_name, country_code) values('USR0000003', 'ballys', 'Bally S', 'US');
cqlsh:demo_keyspace> INSERT INTO demo_table(id, login, full_name, country_code) values('USR0000004', 'yogeshd', 'Yogesh D', 'US');
Select records from COLUMN FAMILY
cqlsh:demo_keyspace> SELECT * FROM demo_table;

 id         | country_code | full_name  | login
 USR0000001 |           IN | Anish Sneh | anishsneh
 USR0000004 |           US |   Yogesh D |   yogeshd
 USR0000003 |           US |    Bally S |    ballys
 USR0000002 |           UK |   Rakesh K |   rakeshk

(4 rows)
Delete record from COLUMN FAMILY:
cqlsh:demo_keyspace> DELETE FROM demo_table WHERE id = 'USR0000002';
cqlsh:demo_keyspace> SELECT * FROM demo_table;

 id         | country_code | full_name  | login
 USR0000001 |           IN | Anish Sneh | anishsneh
 USR0000004 |           US |   Yogesh D |   yogeshd
 USR0000003 |           US |    Bally S |    ballys

(3 rows)
Update record in COLUMN FAMILY:
cqlsh:demo_keyspace> UPDATE demo_table SET country_code = 'CA' WHERE id = 'USR0000001';
cqlsh:demo_keyspace> SELECT * FROM demo_table;

 id         | country_code | full_name  | login
 USR0000001 |           CA | Anish Sneh | anishsneh
 USR0000004 |           US |   Yogesh D |   yogeshd
 USR0000003 |           US |    Bally S |    ballys

(3 rows)

Cassandra CQL queries can be used with Datastax JDBC driver (Java based high level client), demo programs can be found at anishsneh@git.


  1. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
    Digital Marketing Training in Chennai

    Digital Marketing Training in Bangalore
    Digital Marketing Training in Pune

    1. IEEE Final Year projects Project Center in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. <Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, project projects for cseAngular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

  2. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
    Blueprism online training

    Blue Prism Training in Pune

    Blueprism training in tambaram

  3. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.

    Data Science Training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune

  4. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
    java course in tambaram | java course in velachery

    java course in omr | oracle course in chennai

  5. Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!
    angularjs-Training in pune

    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

  6. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    python training in rajajinagar | Python training in btm | Python training in usa

  7. I’m planning to start my blog soon, but I’m a little lost on everything. Would you suggest starting with a free platform like Word Press or go for a paid option? There are so many choices out there that I’m completely confused. Any suggestions? Thanks a lot.

    AWS Interview Questions And Answers

    Best AWS Training in Chennai | Amazon Web Services Training in Chennai

    Amazon Web Services Training in OMR , Chennai | Best AWS Training in OMR,Chennai

    AWS Training in Chennai |Best Amazon Web Services Training in Chennai

  8. This is really impressive post, I am inspired with your post, do post more blogs like this, I am waiting for your blogs.
    Data Science Course in Chennai

  9. You are doing a great job. I would like to appreciate your work for good accuracy.
    devops training in chennai

  10. This is ansuperior writing service point that doesn't always sink in within the context of the classroom. In the first superior writing service paragraph you either hook the reader's interest or lose it. Of course your teacher, who's getting paid to teach you how to write an good essay, 
    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

  11. This is such a good post. One of the best posts that I\'ve read in my whole life. I am so happy that you chose this day to give me this. Please, continue to give me such valuable posts. Cheers!
    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

  12. It is a great post. Keep sharing such kind of useful information.

    Article submission sites
    Guest posting sites

  13. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Data science training in Electronic City

  14. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any python Related information please visit our website python training institutes in Bangalore page!

  15. Informative post indeed, I’ve being in and out reading posts regularly and I see alot of engaging people sharing things and majority of the shared information is very valuable and so, here’s my fine read.
    click here button ux
    click here button instagram story
    click here clipart
    click here css
    click here call to action

  16. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts.
    Cyber Security Projects for Final Year

    JavaScript Training in Chennai

    Project Centers in Chennai

    JavaScript Training in Chennai

  17. keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
    digital marketing training in bangalore /

  18. I truly welcome this superb post that you have accommodated us. I guarantee this would be valuable for the vast majority of the general population. cursos de ti online

  19. digital marketing courses in hyderabad by 360DigiTMG is the best one with quality training, best support and more than 9,000 Students Enrolled
    digital marketing courses in hyderabad

  20. Nice blog,I understood the topic very clearly,And want to study more like this.
    Data Scientist Course

  21. Attend The Digital Marketing Course Bangalore From ExcelR. Practical Digital Marketing Course Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Digital Marketing Course Bangalore.
    Digital Marketing Course Bangalore

  22. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!

    Digital marketing courses in Bangalore

  23. I believe that your blog will surely help the readers who are really in need of this vital piece of information. Waiting for your updates
    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

  24. Thanks a lot for sharing such a good source with all, i appreciate your efforts taken for the same. I found this worth sharing and must share this with all.

    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery

  25. Thanks For Sharing The information The Information Shared Is Very Valuable Please Keep Updating us The Information shared Is Very Valuable.I wanted to thank for sharing this article and I have bookmarked this page to check out new stuff.

    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

  26. The strategy you have posted on this technology helped me to get into the next level and had lot of information in science course in Hyderabad

  27. I like your post. Everyone should do read this blog. Because this blog is important for all now I will share this post. Thank you so much for share with us.
    DevOps Training in Hyderabad
    DevOps Course in Hyderabad

  28. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    Data science Training in hyderabad!

  29. Very interesting blog. alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know.
    Best Data Science Courses in Hyderabad