Accessing Cassandra through NodeJS

Below are the steps to enable web access of Cassandra DB
--------------------------------------------------------

1. install Cassandra DB from apache cassandra
2. install nodejs
3. install the required modules
   a) http       - npm install http
   b) cassandra  - npm install cassandra-driver
   c) express    - npm install express
   d) body-parser - npm install bodyParser
4. create a nodejs file

   E.g. myserver.js

5. now add following lines

   a) first need to list the required modules

      var express = require('express');
      var bodyParser = require('body-parser');
      var cassandra = require('cassandra-driver');

      Note: Put actual path of the driver if not in current directory. E.g
            'D:\\Node-modules\\express').  \\ is required

   b) declare a cassandra client and connect to cluster

      var client = new cassandra.client({contactPoints:['127.0.0.1']});
      client.connect(function(err, result) {     //connect to node
         console.log('Connected');
      });

      Note-1: 127.0.0.1 can also be replaced by the actual IP address of the
              cassandra node - could be the seed node
      Note-2: the keyspace to be connected to can also be specified
              E.g. '127.0.0.1',keyspace: 'demo'

   c) Declare the variables

      var app = express();
      app.use(bodyParser.json());
      app.set('json spaces', 2);

   d) get the information

      - node information

        app.get('/metadata', function(req, res) {
            client.execute('use demo;', function(err, result) {
              if (!err) {
                   //do nothing
               }
            })
            res.send(client.hosts.slice(0).map(function (node) {
              return { address : node.address, rack : node.rack, datacenter : node.datacenter, keyspace:              
                             client.keyspace }
            }));
        });

     - list of keyspaces

       app.get('/keyspaces', function(req, res) {
           client.execute('select keyspace_name from system_schema.keyspaces;', function(err, result) {
              if(!err) {
                  res.json(result);
              }
           })
       });

     - list of tables in keyspaces

       app.get('/keyspaces/tables', function(req, res) {
           client.execute('select keyspace_name, table_name from system_schema.tables;', function(err, result) {
              if(!err) {
                  res.json(result);
              }
           })
       });

     e) start the server

        var server = app.listen(3000, function(req, response) {
            console.log("Hello World!");
        });  

        Note: the port number could be any number. 3000 is shown as example

6. run the js file - node myserver.js

7. Connect to the server - http://<ip-ofnode-where-js-running>:3000

   a) to get metadata          - http://<ip>:3000/metadata
   b) to get list of keyspaces - http://<ip>:3000/keyspaces
   c) to get list of tables    - http://<ip>:3000/keyspaces/tables
 

8. more APIs can be added as needed.

Comments

  1. Contact to Cassandra Technical Support to Solve Cassandra Error Message 395
    The slip-up 395 demonstrates that "Affiliation Refused" on a very basic level this kind of affiliation denied screw up happens when Kong can't talk with Cassandra. Guarantee, if you didn't present the Cassandra at that point at first acquaint it and utilize kongdb.org with course of action a testing Cassandra event. In any case, in most by far of the cases, we have seen that, resulting to using the kongdb from kongdb.org, customers standing up to a comparable issue. Everything considered, we recommend them to pick exceedingly capable help to decide this kind of issue. We supposing hopefully and world-class reinforce concerning Cassandra Support, our Cassandra Database Support, and Apache Cassandra Support can without quite a bit of a stretch fix your hiccups and influence you to bumble free.
    For More Info: https://cognegicsystems.com/
    Contact Number: 1-800-450-8670
    Email Address- info@cognegicsystems.com
    Company’s Address- 507 Copper Square Drive Bethel Connecticut (USA) 06801

    ReplyDelete

Post a Comment

Popular posts from this blog

Amazon Scenario Questions

Using ssh keys to login to VMs

Accessing NATted VMs from Host machine using Port Forwarding