Subhra Ranjan
Subhra Ranjan

Reputation: 113

How to create a keyspace in cassandra using node cassandra-client

I am trying to create a keyspace using nodejs cassandra-client using the below code-

var System = require('cassandra-client').System;
var sys = new System('127.0.0.1:9160');
sys.addKeyspace(ksDef, function(err) {
  if (err) {
    // there was a problem creating the keyspace.
  } else {
    // keyspace was successfully created.
  }
});

but I am getting error "ksDef is undefined".Please give me solution to create keyspace using cassandra-client. What is ksDef?How to define ksDef? Ref--url=https://github.com/racker/node-cassandra-client

Thanks Subhra

Upvotes: 3

Views: 3283

Answers (3)

Pavel Pichrt
Pavel Pichrt

Reputation: 81

It's demonstrated in one of the examples on nodejs-driver github repo:

var cassandraDriver = require('cassandra-driver');
var client = new cassandraDriver.Client({
  contactPoints: ['localhost:9042']
});

client.connect(function(e) {
  var query;
  query = "CREATE KEYSPACE IF NOT EXISTS examples WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3' }";
  return client.execute(query, function(e, res) {
    return console.log(e, res);
  });
});

Upvotes: 5

elado
elado

Reputation: 8730

You need to create a KeySpace outside of the client. A keyspace is like a database in MySQL.

Use cqlsh command line:

$ cqlsh

CREATE KEYSPACE yourKeyspaceName
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

Upvotes: -1

Mahendra
Mahendra

Reputation: 311

Using cassandra-client in node.js we can't create keyspace. Because to get a connection first with cassandra keyspace is neccessary. So for you just get the connection using one of your already existing keyspace and run a simple query to create keyspace.

Example:

var connectionOptions = {
   host: 'localhost',
   port: 9160,
   keyspace: 'mahendradb',
   use_bigints: true
 };
var con = new Connection(connectionOptions);
con.connect(function(err) {
    if (!err) {
        var query = "create keyspace keyspace1 with strategy_options:replication_factor = '1' and strategy_class = 'SimpleStrategy'";
        con.execute(query, [],function(err) {
        if (!err) {
            console.log("new keyspace created");
        }
        else{
            console.log("error in keyspace creation");
        }
        });
    }
    else{
    }
});

Upvotes: 3

Related Questions