couchbase - 如何使用 Java API 查询 Couchbase
问题描述
我正在尝试从 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 查询产生与直接查询相同的结果吗?
解决方案
尝试创建一个新用户并为其添加所有权限(只是为了确保您没有遇到任何安全限制)。
您的代码对我有用:
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"}
推荐阅读
- node.js - Google V2 Node.js 上的操作 - 尝试使用 conversation.data 但它没有按预期工作
- ubuntu - 无法在 Ubuntu 18.04 LTS 上安装 Parallels Tools
- ios - 为什么我的设备显示与模拟器不同的自动布局?
- airflow - 如果任何任务失败,如何将 Airflow DAG 运行标记为失败?
- scala - 为什么这个柯里化函数的函数定义在 scala 中不起作用?
- matlab - 区域增长方法不适用于应用了直方图均衡的图像
- c# - aspx.cs 文件中的保存方法需要哪些参数才能让 Ajax 调用该方法?
- php - 获取所有项目按特定列值分组
- javascript - javascript为什么2个不同的对象引用/使用相同的数组?
- jquery - 引导日期时间选择器