在RIAK上获取MapReduce结果(使用Java客户端)

我在RIAK上存储Person POJO(4个字符串字段 – id,name,lastUpdate,Data),然后尝试使用MapReduce获取这些对象。

我这样做与Basho文档非常相似:

BucketMapReduce m = riakClient.mapReduce("person"); m.addMapPhase(new NamedJSFunction("Riak.mapByFields"), true); MapReduceResult result = m.execute(); Collection tmp = result.getResult(Person.class); 

调用Person的String构造函数:

 public Person(String str){} 

(我必须有这个构造函数,否则我得到一个exception,因为它丢失)在那里我得到一个String的对象 – 一个字符串中的Object字段有一个奇怪的分隔符。

为什么我没有让对象自动转换为我的POJO? 我真的需要翻阅字符串并反序列化吗? 我做错了吗?

您正在使用的JS函数不会按照您的想法执行操作:)它根据具有特定值的字段选择对象,您必须将其作为阶段的参数提供。

我认为你正在寻找的是mapValuesJson ,它将完成你似乎想要做的事情。

此外,您的POJO中根本不需要构造函数。

下面的代码应该指向正确的方向(显然这对于​​POJO中的所有公共字段都是超级简单的并且没有注释):

 public class App { public static void main( String[] args ) throws IOException, RiakException { IRiakClient client = RiakFactory.httpClient(); Bucket b = client.fetchBucket("test_mr").execute(); b.store("myobject", new Person()).execute(); IRiakObject o = b.fetch("myobject").execute(); System.out.println(o.getValueAsString()); BucketMapReduce m = client.mapReduce("test_mr"); m.addMapPhase(new NamedJSFunction("Riak.mapValuesJson"), true); MapReduceResult result = m.execute(); System.out.println(result.getResultRaw()); Collection tmp = result.getResult(Person.class); for (Person p : tmp) { System.out.println(p.data); } client.shutdown(); } } class Person { public String id = "12345"; public String name = "my name"; public String lastUpdate = "some time"; public String data = "some data"; }