如何通过Java客户端查询Couchbase中的特定键? [英] how to query on particular keys in couchbase through java client?

查看:49
本文介绍了如何通过Java客户端查询Couchbase中的特定键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CouchBase的新手.我只有几个简单的文档:

I am new to CouchBase. I have few simple documents as :

1. {
  "docType": "DeviceData",
  "id": "57 0F 75 00 C8 0B@2013-06-26 23:59:38.979",
  "time": 1372271378979,
  "group": "London"
}

2. {
  "docType": "DeviceData",
  "id": "57 0F 75 00 C8 0B@2013-06-27 10:02:46.197",
  "time": 1372307566197,
  "group": "Bristol"
}

3. {
  "docType": "DeviceData",
  "id": "57 0F 75 00 C8 0B@2013-06-27 10:03:36.4",
  "time": 1372307616400,
  "group": "Bristol"
}

我需要查询团体和时间.例如,我想获取组= Bristol,时间从1372307616100到1372307616500的所有文档.因此,我应该获取3个文档中的2个.

I have requirement to query on group and time. for example I want to fetch all the documents with group = Bristol and time from 1372307616100 to 1372307616500. So i deal I should get the 2 of the 3 docs.

所以我将视图创建为:

function (doc, meta) {
  if(doc.docType == "DeviceData")
  emit([doc.group, doc.time], doc);
}

然后在Java代码中按如下所示设置查询:

And in the java code setting the query as below:

String str = "[\"Bristol\", 1372307566100]";
        String end = "[\"Bristol\", 1372307616500]";
        query.setRange(str, end);
        List<DeviceData> deviceData = cbStore.getView("getDevices", DeviceData.class, query);

但是得到零个文档.

请让我知道我在做什么错?需要帮助谢谢.

Please let me know is there anything wrong I am doing? need help thanks.

* 我尝试像下面一样使用复杂键,但是没有运气.

* I tried using complexkeys as well like below but no luck.

ComplexKey startKey = ComplexKey.of("Bristol", "1372307566100");
ComplexKey endKey = ComplexKey.of("Bristol", "1372307616500");

推荐答案

问题在于对象中的时间很长,而不是字符串:

The problem is that in your objects time is a long, not a String:

query.setStale(Stale.FALSE);
long firstParameter=1372307566197l;
long secondParameter=1372307616400l;
//["Bristol",1372307566197]
ComplexKey startKey = ComplexKey.of("Bristol", firstParameter);
//["Bristol",1372307616400]
ComplexKey endKey = ComplexKey.of("Bristol",  secondParameter);
query.setRange(startKey, endKey);
ViewResponse result = client.query(view, query);
Iterator<ViewRow> iter = result.iterator();
while(iter.hasNext()) {
    ViewRow row = iter.next();      
    System.out.println( row.getId()); // ID of the document
    System.out.println(row.getKey()); // Key of the view row
    System.out.println(row.getValue()); // Value of the view row
    System.out.println(row.getDocument()); // Full document if included
    }

这篇关于如何通过Java客户端查询Couchbase中的特定键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆