我正在尝试从Couchbase示例啤酒样本中查询。
此查询在Couchbase浏览器用户界面中运行良好:
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查询产生与直接查询相同的结果吗?
答案 0 :(得分: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"}
答案 1 :(得分: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);
}