使用 node.js 启用 Couchbase N1QL 引擎

Enabling Couchbase N1QL engine with node.js

我正在尝试使用 couchbase npm 包 运行 一些 N1QL 查询。这是我在 运行 编写代码之前采取的步骤:

  1. 在 OSX
  2. 上安装并执行了 Couchbase
  3. 导航到 :8091 并设置 设置凭据以及 beer-sample 存储桶
  4. 已将 N1QL DP4 下载到文件夹
  5. 启用 cbq-engine 像这样: ./cbq-engine -datastore=http://localhost:8091
  6. 创建了 primary index as per the documentation

此时我有 localhost:8091(主要 Couchbase 集群)和 localhost:8093(DP4 引擎)都在运行。

这是我从 node.js 客户端使用的代码,用于尝试 运行 一个简单的 N1QL 查询:

var colors = require('colors'); // for console.log
var couchbase = require('couchbase');
var N1qlQuery = require('couchbase').N1qlQuery; // get query object
var myCluster = new couchbase.Cluster('couchbase://localhost:8091'); // connect to live local cluster
var myBucket = myCluster.openBucket(); // open bucket
myBucket.enableN1ql(['http://localhost:8093/']); // enable n1ql as per documentation (http://docs.couchbase.com/developer/node-2.0/n1ql-queries.html) - I also tried :8091, same result
var query = N1qlQuery.fromString('SELECT * FROM `beer-sample` LIMIT 10');

setTimeout(function() {
  myBucket.query(query, function(err, res) {
    if (err) {
      console.log('query failed'.red, err);
      return;
    }
    console.log('success!', res);
  });
}, 2000); // just in case connecting takes a second or something?

结果如下:

query failed { [Error: failed to connect to bucket] code: 24 }

我似乎已经尝试了所有方法,但找不到任何文档来解决这个问题。怎么回事?

编辑:显然 LCB_LOGLEVEL 环境变量将允许底层 C 库输出一些日志。他们在这里:

LCB_LOGLEVEL=5 node web.js
0ms [I0] {1299} [INFO] (instance - L:372) Version=2.4.0, Changeset=0ac7fc4
0ms [I0] {1299} [INFO] (instance - L:373) Effective connection string: couchbase://127.0.0.1/beer-sample. Bucket=beer-sample
0ms [I0] {1299} [DEBUG] (instance - L:63) Adding host 127.0.0.1:8091 to initial HTTP bootstrap list
0ms [I0] {1299} [DEBUG] (instance - L:63) Adding host 127.0.0.1:11210 to initial CCCP bootstrap list
             Starting...            
107ms [I1] {1299} [INFO] (instance - L:372) Version=2.4.0, Changeset=0ac7fc4
107ms [I1] {1299} [INFO] (instance - L:373) Effective connection string: couchbase://localhost:8091/default. Bucket=default
107ms [I1] {1299} [DEBUG] (instance - L:63) Adding host localhost:8091 to initial CCCP bootstrap list
107ms [I1] {1299} [DEBUG] (confmon - L:89) Preparing providers (this may be called multiple times)
108ms [I1] {1299} [DEBUG] (confmon - L:99) Provider FILE is DISABLED
108ms [I1] {1299} [DEBUG] (confmon - L:96) Provider CCCP is ENABLED
108ms [I1] {1299} [DEBUG] (confmon - L:96) Provider HTTP is ENABLED
108ms [I1] {1299} [DEBUG] (confmon - L:99) Provider MCRAW is DISABLED
108ms [I1] {1299} [TRACE] (confmon - L:292) Start refresh requested
108ms [I1] {1299} [TRACE] (confmon - L:271) Current provider is CCCP
108ms [I1] {1299} [INFO] (cccp - L:118) Requesting connection to node localhost:8091 for CCCP configuration
108ms [I1] {1299} [DEBUG] (lcbio_mgr - L:416) <localhost:8091> (HE=0x103803800) Creating new connection because none are available in the pool
108ms [I1] {1299} [DEBUG] (lcbio_mgr - L:321) <localhost:8091> (HE=0x103803800) Starting connection on I=0x10311dc70
108ms [I1] {1299} [INFO] (connection - L:441) <localhost:8091> (SOCK=0x10311da10) Starting. Timeout=2000000us
110ms [I0] {1299} [DEBUG] (confmon - L:89) Preparing providers (this may be called multiple times)
110ms [I0] {1299} [DEBUG] (confmon - L:99) Provider FILE is DISABLED
110ms [I0] {1299} [DEBUG] (confmon - L:96) Provider CCCP is ENABLED
110ms [I0] {1299} [DEBUG] (confmon - L:96) Provider HTTP is ENABLED
110ms [I0] {1299} [DEBUG] (confmon - L:99) Provider MCRAW is DISABLED
110ms [I0] {1299} [TRACE] (confmon - L:292) Start refresh requested
110ms [I1] {1299} [TRACE] (connection - L:335) <localhost:8091> (SOCK=0x10311da10) Received completion handler. Status=0. errno=0
110ms [I1] {1299} [INFO] (connection - L:116) <localhost:8091> (SOCK=0x10311da10) Connected 
110ms [I1] {1299} [DEBUG] (lcbio_mgr - L:271) <localhost:8091> (HE=0x103803800) Received result for I=0x10311dc70,C=0x10311da10; E=0x0
110ms [I1] {1299} [DEBUG] (lcbio_mgr - L:223) <localhost:8091> (HE=0x103803800) Assigning R=0x10311da50 SOCKET=0x10311da10
110ms [I1] {1299} [DEBUG] (ioctx - L:101) <localhost:8091> (CTX=0x100f012c0,unknown) Pairing with SOCK=0x10311da10
110ms [I0] {1299} [TRACE] (confmon - L:271) Current provider is CCCP
110ms [I0] {1299} [INFO] (cccp - L:118) Requesting connection to node 127.0.0.1:11210 for CCCP configuration
110ms [I0] {1299} [DEBUG] (lcbio_mgr - L:416) <127.0.0.1:11210> (HE=0x10280e600) Creating new connection because none are available in the pool
110ms [I0] {1299} [DEBUG] (lcbio_mgr - L:321) <127.0.0.1:11210> (HE=0x10280e600) Starting connection on I=0x100f01760
110ms [I0] {1299} [INFO] (connection - L:441) <127.0.0.1:11210> (SOCK=0x100f01890) Starting. Timeout=2000000us
110ms [I0] {1299} [TRACE] (connection - L:335) <127.0.0.1:11210> (SOCK=0x100f01890) Received completion handler. Status=0. errno=0
110ms [I0] {1299} [INFO] (connection - L:116) <127.0.0.1:11210> (SOCK=0x100f01890) Connected 
110ms [I0] {1299} [DEBUG] (lcbio_mgr - L:271) <127.0.0.1:11210> (HE=0x10280e600) Received result for I=0x100f01760,C=0x100f01890; E=0x0
110ms [I0] {1299} [DEBUG] (lcbio_mgr - L:223) <127.0.0.1:11210> (HE=0x10280e600) Assigning R=0x100f01540 SOCKET=0x100f01890
110ms [I0] {1299} [DEBUG] (ioctx - L:101) <127.0.0.1:11210> (CTX=0x100d00ca0,unknown) Pairing with SOCK=0x100f01890
110ms [I0] {1299} [DEBUG] (ioctx - L:151) <127.0.0.1:11210> (CTX=0x100d00ca0,sasl) Destroying. PND=0,ENT=1,SORC=1
110ms [I0] {1299} [DEBUG] (ioctx - L:101) <127.0.0.1:11210> (CTX=0x10311dda0,unknown) Pairing with SOCK=0x100f01890
110ms [I0] {1299} [DEBUG] (ioctx - L:151) <127.0.0.1:11210> (CTX=0x10311dda0,bc_cccp) Destroying. PND=0,ENT=1,SORC=1
110ms [I0] {1299} [INFO] (lcbio_mgr - L:491) <127.0.0.1:11210> (HE=0x10280e600) Placing socket back into the pool. I=0x100f01760,C=0x100f01890
111ms [I0] {1299} [INFO] (confmon - L:174) Setting new configuration. Received via CCCP
111ms [I0] {1299} [DEBUG] (bootstrap - L:55) Instance configured!
2112ms [I1] {1299} [ERROR] (negotiation - L:178) <localhost:8091> (SASLREQ=0x100f01180) Error: 0x17, Negotiation timed out
2112ms [I1] {1299} [ERROR] (cccp - L:133) <NOHOST:NOPORT> Got I/O Error=0x17
2112ms [I1] {1299} [INFO] (confmon - L:202) Provider 'CCCP' failed
2112ms [I1] {1299} [DEBUG] (confmon - L:236) Will try next provider in 0us
2112ms [I1] {1299} [DEBUG] (ioctx - L:151) <localhost:8091> (CTX=0x100f012c0,sasl) Destroying. PND=1,ENT=0,SORC=1
2112ms [I1] {1299} [TRACE] (confmon - L:271) Current provider is HTTP
2112ms [I1] {1299} [TRACE] (htconfig - L:395) Starting HTTP Configuration Provider 0x103875a00
2112ms [I1] {1299} [ERROR] (htconfig - L:400) Not scheduling HTTP provider since no nodes have been configured for HTTP bootstrap
2112ms [I1] {1299} [INFO] (confmon - L:202) Provider 'HTTP' failed
2112ms [I1] {1299} [TRACE] (confmon - L:226) Maximum provider reached. Resetting index
2112ms [I1] {1299} [ERROR] (bootstrap - L:100) Failed to bootstrap client=0x10311bb00. Code=0xa, Message=No more bootstrap providers remain
query failed { [Error: failed to connect to bucket] code: 24 }
10112ms [I0] {1299} [DEBUG] (lcbio_mgr - L:464) <127.0.0.1:11210> (HE=0x10280e600) Idle connection expired

couchbase://localhost:8091 是错误的。您应该 指定服务器的初始端口,除非服务器正在侦听备用端口(并且服务器已配置为这样做)。在这种情况下,如果备用端口是 REST API,您将指定 http:// 方案,如果备用端口是数据(memcached)端口,则指定 couchbase://

在内部,客户端可能会按顺序连接到不同的端点类型(尝试更可取的类型到不太可取的类型)。

而不是

myBucket.enableN1ql(['http://1.1.1.1:8093/'])

myBucket.enableN1ql(['1.1.1.1:8093'])

它对我有用。