如何使用Node Cassandra客户端创建Cassandra中的Keyspace

3

我正在尝试使用Node.js cassandra-client创建一个keyspace,使用以下代码-

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.
  }
});

但是我遇到了“ksDef未定义”的错误,请告诉我如何使用cassandra-client创建keyspace。什么是ksDef?如何定义ksDef?参考链接:https://github.com/racker/node-cassandra-client。谢谢,Subhra。
3个回答

5

这在nodejs-driver的github仓库中的一个示例中有所体现:

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);
  });
});

3

在 node.js 中使用 cassandra-client 时,我们不能创建 keyspace。因为要先与 Cassandra 建立连接,keysapce 是必需的。因此,您可以使用现有 keyspace 中的一个连接,并运行一个简单的查询来创建新的 keyspace。

例如:

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{
    }
});

1
谢谢Mahindra!现在我可以使用nodejs cassandra-client创建Keyspace了,但是如何在不连接到现有Keyspace的情况下创建Keyspace呢?我的意思是动态创建Keyspace。 - undefined

-1
你需要在客户端之外创建一个KeySpace。KeySpace类似于MySQL中的数据库。
使用命令行:
$ cqlsh

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接