【问题标题】:How to create a keyspace in cassandra using node cassandra-client如何使用节点 cassandra-client 在 cassandra 中创建密钥空间
【发布时间】:2016-02-05 05:25:56
【问题描述】:

我正在尝试使用以下代码使用 nodejs cassandra-client 创建一个键空间-

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 创建密钥空间的解决方案。什么是ksDef?如何定义ksDef?参考--url=https://github.com/racker/node-cassandra-client

谢谢 苏布拉

【问题讨论】:

    标签: cassandra


    【解决方案1】:

    它在 nodejs-driver github repo 上的examples 之一中得到了展示:

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

    【讨论】:

      【解决方案2】:

      在 node.js 中使用 cassandra-client 我们无法创建密钥空间。因为首先要与 cassandra 密钥空间建立连接是必要的。因此,您只需使用现有的键空间之一获取连接并运行一个简单的查询来创建键空间。

      例子:

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

      【讨论】:

      • 感谢 Mahindra !!!现在我可以使用 nodejs cassandra-client 创建 KeySpace,但是如何在不连接到现有 keyspace 的情况下创建 keyspace。我的意思是动态创建 keyspace。
      【解决方案3】:

      您需要在客户端之外创建一个 KeySpace。键空间就像 MySQL 中的数据库。

      使用cqlsh命令行:

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

      【讨论】:

        猜你喜欢
        • 2016-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-22
        • 2015-11-22
        • 2017-07-24
        • 2019-02-23
        • 2015-11-02
        相关资源
        最近更新 更多