首页 > 解决方案 > 如何使用 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 查询产生与直接查询相同的结果吗?

标签: couchbasecouchbase-java-api

解决方案


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

您的代码对我有用:

    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"}

推荐阅读