我在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<Person> tmp = result.getResult(Person.class);
调用Person的String构造函数:
public Person(String str){}
(我必须有这个构造函数,否则我会得到一个异常,因为它丢失了) 在那里我将对象作为String - 对象的字段在一个字符串中,带有一个奇怪的分隔符。
为什么我没有让对象自动转换为我的POJO?我真的需要翻阅字符串并反序列化吗?我做错了吗?s
答案 0 :(得分:3)
你正在使用的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<Person> 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";
}