Single-Node Cassandra Cluster and NodeJS client — demo setup on Ubuntu 14.04 (AWS)

Adrian Barwicki
3 min readMay 13, 2017

--

Cassandra + NodeJS

Introduction

Cassandra, or Apache Cassandra, is a highly scalable open source NoSQL database system being the right choice when you need scalability and high availability without compromising performance.

With this short article I will guide you step-by-step to get started with the Cassandra Database, including:

  • Configuring the EC2-Instance
  • Installing Cassandra Database
  • Setting up authentification strategy
  • Configure Cassandra to be open to the world

Configuring the EC2-Instance

All right. Let’s spin up an EC2-Instance on AWS.

Go to the AWS console -> EC2 and Launch an Instance. In this tutorial, we use Ubuntu Image for the Instance.

In the security group section, we need to open the some following ports for inbound communication that are used by Cassandra Database.

  • 7000 — Internode communication (not used if TLS enabled)
  • 7001 — TLS Internode communication (used if TLS enabled)
  • 9160 — Thrift client API
  • 9042 — CQL native transport port

Installing Cassandra

Once your instance is up and running, connect to it. In order to setup JVM, download Cassandra and install it, I found the following tutorial very helpful: https://www.digitalocean.com/community/tutorials/how-to-install-cassandra-and-run-a-single-node-cluster-on-ubuntu-14-04.

Authentification Setup

In the next step you want to setup the authentification strategy for Cassandra before you accept any traffic to it.

Out of the box, Cassandra provides two authentification modes: AllowAllAuthenticator and PasswordAuthenticator.

AllowAllAuthenticator performs no checks and is the default authentifator .
PasswordAuthenticator relies on username/password pairs to authenticate users. It keeps usernames and hashed passwords in system_auth.credentials table.

We want to switch from AllowAllAuthenticator to PasswordAuthenticator.

Open the cassandra.yaml configuration file:

sudo vim /etc/cassandra/cassandra.yaml

and change the authenticator option to PasswordAuthenticator. By default, the authenticator option is set to AllowAllAuthenticator.

authenticator: PasswordAuthenticator

For more detailed information, refer to http://docs.datastax.com/en/cassandra/2.1/cassandra/security/security_config_native_authenticate_t.html.

Now, we will setup a different user. We access the cqlsh-console with the default Cassandra username and password: cassandra.

cqlsh -u cassandra -p cassandra

and run:

CREATE USER 'admin' WITH PASSWORD 'adminpassword' SUPERUSER;
CREATE USER 'developer' WITH PASSWORD 'devpassword' NOSUPERUSER

For details concerning creating new users (or roles) refer to: http://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlCreateUser.html

Database configuration

Once we setup the authentification and users, we need to also be able to access the Cassandra database. Currently we can only do it from the machine it runs on.

Firstly, we need to find out the private IP of our EC2-Instance. You will find it in the your AWS-Dashboard, by going to EC2 -> running instances and clicking on your instance. Private IP will be visible in the description tab.

Description of EC2-Instance — Private IP

Now open the /etc/cassandra/cassandra.yaml file again, and overwrite the following properties:

listen_address: <yourPrivateIP>
seeds: "127.0.0.1,<yourPrivateIP>"
rpc_address: <yourPrivateIP>

and restart the Cassandra database:

sudo service cassandra restart

NodeJS client example

Ok, now we can create our sample nodeJS application.

We use the cassandra-driver npm package.

const cassandra = require(‘cassandra-driver’);const client = new cassandra.Client({
contactPoints: [ ‘your-public-api’ ],
authProvider: new cassandra.auth
.PlainTextAuthProvider(‘developer’, ‘devpassword’)
});

Note that we use here the public IP of the EC2-Instance, not the private IP that we used before in the Cassandra config.

Finally we can setup the first keyspace and table.

client.execute(`CREATE KEYSPACE IF NOT EXISTS vq_ai_tracking WITH REPLICATION = { ‘class’ : ‘SimpleStrategy’, ‘replication_factor’ : 1 };`)
.then(() =>
client.execute(`CREATE TABLE IF NOT EXISTS vq_ai_tracking.actions(userId uuid primary key);`,
(err, result) => {
console.log(err, result);
}));

Further reading

In this tutorial we setup a single-node Cassandra cluster. If you would like to see learn to work with multi-nodes clusters, check out https://oliverveits.wordpress.com/2016/12/08/cassandra-hello-world-example/.

--

--

Adrian Barwicki
Adrian Barwicki

Written by Adrian Barwicki

Hi, I’m Adrian. This is my private blog and you’ll find here from time to time updates about my travels, works and investments.

No responses yet