Magnus
Magnus

Reputation: 11396

How to query Couchbase using Java API

I'm trying to query from the Couchbase example beer-sample.

This query works fine in the Couchbase browser UI:

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

Results:

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

But when I transplant the query into java, I get very peculiar results. (Yes, I know I'm opening/closing connection in the wrong place, just doing this for quick exploration of Couchbase syntax/features).

Java code:

@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);
}

Results:

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

Can someone explain how to make the java query produce the same results as the direct query?

Upvotes: 2

Views: 321

Answers (2)

Magnus
Magnus

Reputation: 11396

For some reason, changing this

answer.add(row.value());

to this

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

fixed it for me. No idea why, given that the original version apparently works fine for other people here.

Complete solution for reference:

@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);
}

Upvotes: 1

deniswsrosa
deniswsrosa

Reputation: 2460

Try to create a new user and add all privileges to it (just to be sure that you are not hitting any security restriction).

Your code works for me:

    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();

Output:

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

Upvotes: 2

Related Questions