如何通过java客户端查询couchbase中的特定键?

Gur*_*lki 1 java couchbase

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

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"
}
Run Code Online (Sandbox Code Playgroud)

我要求查询组和时间.例如,我想用group = Bristol获取所有文件,时间从1372307616100到1372307616500.所以我处理我应该得到3个文档中的2个.

所以我创建了视图:

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

并在java代码中设置查询如下:

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

但获得零文件.

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

*编辑: 我尝试使用复杂的键,如下,但没有运气.

ComplexKey startKey = ComplexKey.of("Bristol", "1372307566100");
ComplexKey endKey = ComplexKey.of("Bristol", "1372307616500");
Run Code Online (Sandbox Code Playgroud)

小智 5

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

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
    }
Run Code Online (Sandbox Code Playgroud)