Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ektorp在couchdb中获取从id到string prop的java.util.Map_Java_Couchdb_Jackson_Ektorp - Fatal编程技术网

如何使用ektorp在couchdb中获取从id到string prop的java.util.Map

如何使用ektorp在couchdb中获取从id到string prop的java.util.Map,java,couchdb,jackson,ektorp,Java,Couchdb,Jackson,Ektorp,我在处理我认为简单的问题时遇到了困难。基本上,我需要一个java.util.Map,其中id作为映射键,文档的someField作为值 我真的被这件事缠住了,这让我大吃一惊。我试着写一个单独的观点: @View(map="function(d) { if (d.someField) { emit(d.someField, null); } }", name = "someField") 然后使用以下Java: public Map<String, String> getSomeFi

我在处理我认为简单的问题时遇到了困难。基本上,我需要一个
java.util.Map
,其中id作为映射键,文档的
someField
作为值

我真的被这件事缠住了,这让我大吃一惊。我试着写一个单独的观点:

@View(map="function(d) { if (d.someField) { emit(d.someField, null); } }", name = "someField")
然后使用以下Java:

public Map<String, String> getSomeFields() throws JsonParseException, JsonMappingException, IOException {
    ViewQuery q = new ViewQuery().designDocId("_design/" + entity.getSimpleName()).viewName("someField");
    String result = StreamUtils.inputStreamAsString(db.queryForStream(q), false);
    TypeReference<Map<String, String>> mapTypeRef = new TypeReference<Map<String,String>>() {};
    // mapper is a Jackson ObjectMapper
    return mapper.readValue(result, mapTypeRef);
} 
publicmap getSomeFields()抛出JsonParseException、JsonMappingException、IOException{
ViewQuery q=new ViewQuery().designDocId(“_design/”+entity.getSimpleName()).viewName(“someField”);
字符串结果=StreamUtils.inputStreamAsString(db.queryForStream(q),false);
TypeReference mapTypeRef=新的TypeReference(){};
//映射器是一个Jackson对象映射器
返回mapper.readValue(结果,mapTypeRef);
} 
这已经很难看了,但实际上也不起作用,因为
queryForStream
返回的JSON结果似乎包含随机的其他内容,而不仅仅是查询结果。这会导致
readValue
调用抛出
IOException


我还尝试使用
reduce
生成一个包含所有这些值的对象,但结果是coach抱怨reduce没有足够的减少…

您需要预解析CouchDB的输出,因为无法避免在查询中返回所有元数据

首先,视图需要发出正确的数据(对象id及其值)

@View(map=“function(d){if(d.someField){emit(d.id,d.someField);}}”,name=“someField”)

回复的形式是JSON对象字符串=>object。我将首先将整个回复映射到这个,然后选择带有键“rows”的对象,这是一个JSON数组。此数组中的每个元素都是另一个JSON对象,其键为“id”、“key”、“value”。然后需要将这些对象中的每一个映射到输出中的键值对

public Map<String, String> getSomeFields() 
        throws JsonParseException, JsonMappingException, IOException {

    ViewQuery q = 
        new ViewQuery().designDocId("_design/" + 
        entity.getSimpleName()).viewName("someField");

    String queryRresult = 
        StreamUtils.inputStreamAsString(db.queryForStream(q), false);

    TypeReference<Map<String, Object>> mapTypeRef = 
        new TypeReference<Map<String,Object>>() {};

    TypeReference<List<Map<String,String>>> rowsTypeRef = 
        new TypeReference<List<Map<String,String>>>() {};

    // Map of the top level results which includes the couch meta and the
    // rows. We have to use object, because Each value is of a different 
    // type (string, ints, json objects)
    Map<String,Object> topResultMap = 
        mapper.readValue(queryRresult, mapTypeRef);

    // Once we have the top level result, cast the value for key "rows" as 
    // String, and parse it as a rows type, which is a list of maps.
    List<Map<String,String>> rows = 
        mapper.readValue((String) topResultMap.get("rows"), rowsTypeRef);

    // Finally iterator over that list pulling out the id and the value in 
    // the key and value for the results
    Map<String,String> results = new HashMap<String,String>();
    for (Map<String,String> row : rows)
        results.put(row.get("id"), row.get("value"));

    // And return them
    return results;
}
publicmap getSomeFields()
抛出JsonParseException、JsonMappingException、IOException{
ViewQuery q=
新建ViewQuery().designDocId(“\u design/”+
entity.getSimpleName()).viewName(“someField”);
字符串queryResult=
StreamUtils.inputStreamAsString(db.queryForStream(q),false);
类型引用mapTypeRef=
新的TypeReference(){};
TypeReference rowsTypeRef=
新的TypeReference(){};
//顶级结果图,包括沙发元和
//行。我们必须使用对象,因为每个值都具有不同的属性
//类型(字符串、整数、json对象)
映射topResultMap=
readValue(QueryResult,mapTypeRef);
//获得顶级结果后,将键“rows”的值强制转换为
//字符串,并将其解析为rows类型,这是一个映射列表。
列表行=
readValue((字符串)topResultMap.get(“行”),rowsTypeRef);
//最后,对该列表进行迭代,从中提取id和值
//结果的关键和价值
映射结果=新的HashMap();
用于(地图行:行)
结果.put(row.get(“id”)、row.get(“value”);
//然后把它们还给我
返回结果;
}

最后,您需要确保CouchDB视图中没有reduce部分。如果您这样做,您必须将“reduce=false”传递给coach。

我会这样做:

ViewQuery query = ...

Map<String, String> map = new HashMap<String, String>();
for (ViewResult.Row row : db.queryView(query)) {
    map.put(row.getId(), row.getKey());
}

return map;
ViewQuery=。。。
Map Map=newhashmap();
for(ViewResult.Row行:db.queryView(查询)){
put(row.getId(),row.getKey());
}
返回图;

这似乎是最简单的解决方案,谢谢!(请注意,这不会按原样工作,因为在设置行结果集之前,您需要对
.queryView
调用结果执行
.getRows()