繁体   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