2

我正在尝试从 Couchbase 示例啤酒样本中进行查询。

此查询在 Couchbase 浏览器 UI 中运行良好:

select category, style from `beer-sample` where style like 'Imperial%'

结果:

[
  {
    "category": "North American Ale",
    "style": "Imperial or Double India Pale Ale"
  },
...
]

但是当我将查询移植到java中时,我得到了非常奇特的结果。(是的,我知道我在错误的地方打开/关闭连接,只是为了快速探索 Couchbase 语法/功能)。

Java代码:

@RequestMapping("/hellocouchbase")
public ResponseEntity<List<JsonObject>> metrics() {

    Cluster cluster = CouchbaseCluster.create();
    cluster.authenticate(username, passwd);

    Bucket bucket = cluster.openBucket("beer-sample");

    N1qlQueryResult result = bucket.query(N1qlQuery.simple("select category, style from `beer-sample` where style like 'Imperial%'"));

    List<N1qlQueryRow> results = result.allRows();      
    List<JsonObject> answer = new ArrayList<>(results.size());

    for(N1qlQueryRow row:results) {
        answer.add(row.value());
    }

    cluster.disconnect();       
    return ResponseEntity.status(200).body(answer);
}

结果:

[
{"cryptoManager":null,"empty":false,"names":["style","category"]},{"cryptoManager":null,"empty":false,"names":["style","category"]},
...
]

有人可以解释如何使 java 查询产生与直接查询相同的结果吗?

4

2 回答 2

2

尝试创建一个新用户并为其添加所有权限(只是为了确保您没有遇到任何安全限制)。

您的代码对我有用:

    Cluster cluster = CouchbaseCluster.create();
    cluster.authenticate("test", "couchbase"); //user and password that I created

    Bucket bucket = cluster.openBucket("beer-sample");

    N1qlQueryResult result = bucket.query(N1qlQuery.simple("select category, style from `beer-sample` where style like 'Imperial%'"));

    List<N1qlQueryRow> results = result.allRows();
    List<JsonObject> answer = new ArrayList<>(results.size());

    for(N1qlQueryRow row:results) {
        answer.add(row.value());
        System.out.println(row);
    }

    cluster.disconnect();

输出:

{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
...
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double Red Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
{"category":"North American Ale","style":"Imperial or Double India Pale Ale"}
于 2018-10-16T08:44:53.237 回答
1

出于某种原因,改变这个

answer.add(row.value());

对此

answer.add(row.value().toMap());

为我修好了。不知道为什么,因为原始版本显然适用于这里的其他人。

完整解决方案供参考:

@RequestMapping("/hellocouchbase")
public ResponseEntity<List<Map<String,Object>>> metrics() {

    Cluster cluster = CouchbaseCluster.create();
    cluster.authenticate(username, passwd);

    Bucket bucket = cluster.openBucket("beer-sample");

    N1qlQueryResult result = bucket.query(N1qlQuery.simple("select category, style from `beer-sample` where style like 'Imperial%'"));

    List<N1qlQueryRow> results = result.allRows();      
    List<Map<String,Object>> answer = new ArrayList<>(results.size());

    for(N1qlQueryRow row:results) {
        answer.add(row.value().toMap());
    }

    cluster.disconnect();       
    return ResponseEntity.status(200).body(answer);
}
于 2018-10-17T14:55:33.637 回答