Nodejs Cassandra客户端[node-cassandra-client]

时间:2012-01-31 07:43:58

标签: node.js cassandra

我尝试使用node-cassandra-client

通过nodejs连接到我的cassandra集群[版本1.0.6]

这是我的示例脚本

var Connection = require('cassandra-client').Connection;
var con = new Connection({host:'x.x.x.x', port:9160, keyspace:'Stats', timeout:10000});
console.log(con);
con.execute('UPDATE TestCF ?=? WHERE key=?', ['cola', '1', '20120132'], function(err) {
    if (err) {
        console.log("Failed");
    } else {
        console.log("success");
    }
});

执行脚本

    The "sys" module is now called "util". It should have a similar interface.
    node-cassandra-client.driver: connecting x.x.x.x:9160 {}
    { validators: {},
      client: null,
      connectionInfo: 
       { host: 'x.x.x.x',
         port: 9160,
         keyspace: 'Stats',
         timeout: 10000 },
      timeout: 10000 }

    node.js:201
            throw e; // process.nextTick error, or 'error' event on first tick
                  ^
    TypeError: Cannot call method 'execute_cql_query' of null
        at [object Object].execute (/home/tamil/workspace/TestProjects/node-cass/node_modules/cassandra-client/lib/driver.js:367:17)
        at Object.<anonymous> (/home/tamil/workspace/TestProjects/node-cass/index.js:5:5)
        at Module._compile (module.js:432:26)
        at Object..js (module.js:450:10)
        at Module.load (module.js:351:31)
        at Function._load (module.js:310:12)
        at Array.0 (module.js:470:10)
        at EventEmitter._tickCallback (node.js:192:40)

我的nodetool统计信息

Address         DC          Rack        Status State   Load            Owns    Token                                       
x.x.x.x         datacenter1 rack1       Up     Normal  1.03 MB         100.00% 0     

错误原因是什么?并需要一些帮助来解决这个问题

2 个答案:

答案 0 :(得分:12)

您的连接中的客户端为null。你需要先连接()。这是一个例子(忽略所有错误):

var con = new Connection({host:'x.x.x.x', port:9160, keyspace:'Stats', timeout:10000});
con.connect(function(err) {
  assert.ifError(err);
  con.execute('SELECT COUNT(*) FROM TestCF', [], function(err, rows) {
    con.close(function(err) {
      // you're done now.
    });
  });
});

我建议使用async来处理connct,query,close等的排序。

答案 1 :(得分:2)

看起来像node.js thrift驱动程序存在某种问题。我对Helenus有更多的好运。