Getting Started with Apache Cassandra and Node.js

Intro

Write Up

Requirements

To follow this tutorial, you should already have a running Cassandra instance. You should have the demo keyspace and schema still set up, we will be referring to it below.

Setup

This walkthrough will be using the Datastax Node.js Driver. If you have Node Package Manager (npm) installed, you should be able to install the Datastax Node.js driver just by running this from the command line 

npm install cassandra-driver

For this tutorial, we are also using the async module (see here). While this is not absolutely necessary, it let’s us control the flow of the program. Thanks to @LukeTillman for this tip. To install from the command line:

npm install async

Try it Out

For this demo, we’re going to be creating a simple console application.  Open a text editor and create a Node.js file. First we need to connect to our cluster and the “demo” keyspace.

var cassandra = require('cassandra-driver');
var async = require('async');
 //Connect to the cluster
var client = new cassandra.Client({contactPoints: ['127.0.0.1'], keyspace: 'demo'});

Now that you are connected to the “demo” keyspace, let’s insert a user into the “users” table:

// Read users and print to console
   function (callback) {
       client.execute("SELECT lastname, age, city, email, firstname FROM users WHERE lastname='Jones'", function (err, result) {
           if (!err){
               if ( result.rows.length > 0 ) {
                   var user = result.rows[0];
                   console.log("name = %s, age = %d", user.firstname, user.age);
               } else {
                   console.log("No results");
               }
           }
 
           // Run next function in series
           callback(err, null);
       });
   },

Since it’s Bob’s birthday, we are going to update his age:

// Update Bob's age
   function (callback) {
       client.execute("UPDATE users SET age = 36 WHERE lastname = 'Jones'", function (err, result) {
           // Run next function in series
           callback(err, null);
       });
   },
   // Read users and print to the console
   function (callback) {
       client.execute("SELECT firstname, age FROM users where lastname = 'Jones'", function (err, result) {
           var user = result.rows[0];
           console.log("name = %s, age = %d", user.firstname, user.age);
 
           // Run next function in series
           callback(err, null);
       });
   },

Now let’s delete Bob from the table. Then we can print out all the rows. You’ll notice that Bob’s information no longer comes back after being deleted (others might, if you have inserted users previously):

 // Delete Bob
   function (callback) {
       client.execute("DELETE FROM users WHERE lastname = 'Jones'", function (err, result) {
           if (!err) {
               console.log("Deleted");
           }
 
           // Run next function in series
           callback(err, null);
       });
   },
   // Read users and print to the console
   function (callback) {
       client.execute("SELECT * FROM users WHERE lastname='Jones'", function (err, result) {
           if ( result.rows.length > 0 ) {
               var user = result.rows[0];
               console.log("name = %s, age = %d", user.firstname, user.age);
           } else {
               console.log("No records");
           }
 
           // Run next function in series
           callback(err, null);
       });
   }

It’s a good idea to close the connection once you are done:

], function (err, results) {
   // All finished, quit
   process.exit();
});

The output should give you something like this:

name = Bob, age = 35
name = Bob, age = 36
Deleted
No records

CQL is very similar to SQL, in many cases the same syntax will work.  This makes querying for data very straightforward if you have a background with relational databases.

You have just managed to connect to a Cassandra cluster and perform queries against a live (local) database. Hopefully this demonstrates just how easy it is to use Cassandra using the Datastax Node.js driver.  A Gist of the complete console application for this sample is available on GitHub.

Start Learning with DS201: Cassandra Core Concepts

More Resources

• Check out the Datastax Node.js driver on GitHub

• Read up more on CQL.

No Exercises.
No FAQs.
No resources.
Comments are closed.