簡體   English   中英

node.js卡桑德拉連接

[英]node.js cassandra connecion

這是nodejs程序。 如何返回結果?

executeCQLQuery = function(query, PooledConnection, options) {
  var hosts = [options.host + ":" + options.port];
  var connection_pool = new PooledConnection({
    'hosts': hosts,
    'keyspace': options.keyspace
  });

  connection_pool.execute(query, [], function (err, rows) {
    if (err) {
      console.log(err);
    } else {
      return rows;
    }
    //console.log(rows);
    result = rows;
  });
  console.log(result);
  return result;
}
exports.executeCQLQuery = executeCQLQuery;

您可以像Tamil建議的那樣傳遞回調,而不是在execute函數中進行內聯回調(在此檢查行錯誤),

connection_pool.execute(query, [], callback);

然后,您可以將回調傳遞到導出的函數中

executeCQLQuery = function(query, PooledConnection, options, callback) {
  ...
}

請記住,cassandra節點庫是異步的,就像node.js中的大多數東西一樣。 您需要使用回調函數以節點方式進行操作:

executeCQLQuery = function(query, PooledConnection, options, callback) {
var hosts = [options.host + ":" + options.port];
var connection_pool = new PooledConnection({
  'hosts': hosts,
  'keyspace': options.keyspace
});

connection_pool.execute(query, [], callback);

exports.executeCQLQuery = executeCQLQuery;

然后像這樣使用它:

executeCQLQuery('select foo from bar where key="bam"', PooledConnection, {host:'foo', port:1234}, function(err, rows) {
  // check for err
  // do something useful with rows.
});

另一個建議:使用PooledConnection作為arg沒有意義。 您應該在調用executeCQLQuery之前實例化它。

cassandra-node中的測試用例總是一個很好的起點: http : //code.google.com/a/apache-extras.org/p/cassandra-node/source/browse/test/test_driver.js#280

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM