2012-10-15 69 views
11

我試圖查詢Cassandra服務器時遇到同樣的問題。我有npm安裝了node-cassandra-client版本0.9.5。node.js node-cassandra-client請求失敗

var Connection = require('cassandra-client').PooledConnection; 

var hosts = ['host1:9160','host2:9160']; 
var cassandra = new Connection({'hosts': hosts, 'keyspace': 'keyspace'}); 
cassandra.on('log', function(level, message, obj) { 
    console.log('log event: %s -- %j', level, message); 
}); 
var cql = "SELECT * FROM columnFamily LIMIT 1"; 

cassandra.execute(cql, function(err, rows) { 
    if(err) console.log("erreur à la requete"); 

    console.log(rows); 
    cassandra.shutdown(function() { 
    console.log("connectoin pool shutdown"); 
    }); 
}) 

看起來連接成功。但我一直遇到以下錯誤:

log event: info -- "connecting host:9160(1)" 
log event: cql -- "SELECT * FROM columnFamily LIMIT 1" 
DEBUG: 
DEBUG: /PATH/cassandra-client/node_modules/thrift/lib/thrift/connection.js:90 
DEBUG:   throw e; 
DEBUG:    
DEBUG:^
DEBUG: TypeError: undefined is not a function 
    at /PATH/node_modules/cassandra-client/lib/driver.js:701:5 
    at /PATH/node_modules/cassandra-client/lib/driver.js:716:15 
    at /PATH/node_modules/cassandra-client/lib/driver.js:453:9 
    at/PATH/node_modules/cassandra-  client/node_modules/thrift/lib/thrift/connection.js:80:11 
    at Object.recv_execute_cql_query (/PATH/node_modules/cassandra-client/lib/gen-nodejs/Cassandra.js:6219:12) 
    at /PATH/node_modules/cassandra-cli ent/node_modules/thrift/lib/thrift/connection.js:83:37 
    at Socket.<anonymous> (/PATH/node_modules/cassandra-client/node_modules/thrift/lib/thrift/transport.js:69:9) 
    at Socket.emit (events.js:67:17) 
    at TCP.onread (net.js:347:14) 
DEBUG: Program node app.js exited with code 1 

無論請求是什麼,我一直有相同的錯誤。

+1

我試着用Helenus代替卡桑德拉 - 客戶端,它似乎運作良好。 – Florent

回答

2

我知道這有點遲,但是當它接受三個參數(查詢,查詢參數列表和回調)時,您將使用兩個參數(查詢和回調)調用cassandra.execute

嘗試改變:

cassandra.execute(cql, function(err, rows) { 
    if(err) console.log("erreur à la requete"); 

    console.log(rows); 
    cassandra.shutdown(function() { 
     console.log("connectoin pool shutdown"); 
    }); 
}) 

cassandra.execute(cql, [], function(err, rows) { 
    if(err) console.log("erreur à la requete"); 

    console.log(rows); 
    cassandra.shutdown(function() { 
     console.log("connectoin pool shutdown"); 
    }); 
}) 
+0

這是沒有必要的。參數是可選的 – Abhi

+1

@Abhi它在版本0.9.5中是強制性的,因爲'execute'直接調用'bind',它又使用第二個參數的'.length'。 [Check for yourelf](https://github.com/racker/node-cassandra-client/blob/v0.9.5/lib/driver.js#L410-L411) – soulcheck