2013-06-27 48 views
1

我是CouchBase的新手。我有几个简单的文件:如何通过java客户端查询couchbase中的特定键?

1. { 
    "docType": "DeviceData", 
    "id": "57 0F 75 00 C8 [email protected] 23:59:38.979", 
    "time": 1372271378979, 
    "group": "London" 
} 

2. { 
    "docType": "DeviceData", 
    "id": "57 0F 75 00 C8 [email protected] 10:02:46.197", 
    "time": 1372307566197, 
    "group": "Bristol" 
} 

3. { 
    "docType": "DeviceData", 
    "id": "57 0F 75 00 C8 [email protected] 10:03:36.4", 
    "time": 1372307616400, 
    "group": "Bristol" 
} 

我有要求查询组和时间。例如我想要取所有与组=布里斯托尔的文件和时间从1372307616100到1372307616500.所以我处理我应该得到3文档中的2。

所以我要创建视图:

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

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

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

但是,让零文件。

请让我知道我有什么问题吗?需要帮助谢谢。

* 编辑: 我尝试使用复杂键以及像下面,但没有运气。

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

1.尝试查看使用什么的'Stale'参数值的方式。并尝试将其设置为'Stale = false'。 2.检查您是否发布了您的视图。 3.使用Couchbase Web管理面板中的相同参数检查视图。如果它返回正确的值,那么问题出现在您的应用程序中,而不是couchbase。 – m03geek

+0

我已经发布了该视图,我可以在使用REST网址尝试获取记录。 – GuruKulki

+0

“陈旧”参数呢?正如我记得它应该像'query.setStale(Stale陈旧)'。 PS:顺便说一下,不要在你的视图中包含文档,如:'emit([doc.group,doc.time],doc);'。最好使用'emit([doc.group,doc.time],null);'并将'IncludeDocs'添加到您的查询中:'query.setIncludeDocs(boolean include)'。 – m03geek

回答

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 
    } 
相关问题