使用ektorp

时间:2017-12-21 17:55:09

标签: java pagination couchdb ektorp

您好我想通过应用reduce和分页从couchdb-view获取数据。

我的视图将reduce函数结果作为复数键给出如下

{"rows":[
         {"key":{"attribute":"Attribute1"},"value":20},
         {"key":{"attribute":"Attribute2"},"value":1}
         {"key":{"attribute":"Attribute3"},"value":1}
]}

我正在尝试使用ektorp从couchdb获取数据,请检查以下代码

PageRequest pageRequest = PageRequest.firstPage(10);
ViewQuery query = new ViewQuery()
            .designDocId("_design/medesign")
            .viewName("viewname")
            .includeDocs(false)
            .reduce(true)
            .group(true);
Page<ViewResult> rs1 = db.queryForPage(query, pageRequest, ViewResult.class);

rs1.forEach(v -> {
            System.out.println(v.getSize());
        });

我收到以下错误

org.ektorp.DbAccessException: com.fasterxml.jackson.databind.JsonMappingException: 
Can not construct instance of org.ektorp.ViewResult: 
no int/Int-argument constructor/factory method to deserialize from Number value (20)
at [Source: N/A; line: -1, column: -1]

1 个答案:

答案 0 :(得分:0)

如果您想要分页的简化数据,CouchDB不会提供分页详细信息。

使用分页包含文档的请求
group=false & reduce=false & include_docs=true

网址:http://localhost:5984/dn_anme/_design/design_name/_view/viewname?include_docs=true&reduce=false&skip=0&group=false&limit=2

回应:

{
   "total_rows":81,
   "offset":0,
   "rows":[
      {
         "id":"906a74b8019716f1240a7117580ec172",
         "key":{
            "attribute":"BuildArea"
         },
         "value":1,
         "doc":{
            "_id":"906a74b8019716f1240a7117580ec172",
            "_rev":"3-7e0a1da0c2260040f8a9787636385785",
            "country":"POL",            
            "recordStatus":"MATCHED"
         }
      },
      {
         "id":"906a74b8019716f1240a7117580eaefb",
         "key":{
            "attribute":"Area",
         },
         "value":1,
         "doc":{
            "_id":"906a74b8019716f1240a7117580eaefb",
            "_rev":"3-165ea3a3ed07ad8cce1f3e095cd476b5",
            "country":"POL",
            "recordStatus":"MATCHED"
         }
      }
   ]
}

请求减少 group=true& reduce=true& include_docs=false

网址:http://localhost:5984/dn_anme/_design/design_name/_view/viewname?include_docs=false&reduce=true&group=true&limit=2

Resoonse:

{
   "rows":[
      {
         "key":[
            "BuildArea"
         ],
         "value":1
      },
      {
         "key":[
            "Area"
         ],
         "value":1
      }
   ]
}

请求中的差异:
使用分页包含文档的请求会提供页面数据{"total_rows":81, "offset":0, rows":[{...},{...}]} 请求with reduce {"rows":[{...},{..}]}

如何获取分页缩减数据:

  

步骤1:从视图中请求rows_per_page + 1行
  第2步:如果响应一个额外的记录而不是page_size那么有更多的记录
  步骤3:计算并更新跳过值并进入下一页的步骤1     注意:添加skip不是很多记录的好选择,而不是find start键和添加开始键,它有利于更好的性能