在RIAK上获取MapReduce结果(使用Java客户端) [英] Getting MapReduce results on RIAK (using the Java client)

查看:71
本文介绍了在RIAK上获取MapReduce结果(使用Java客户端)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am storing Person POJOs (4 string fields - id, name, lastUpdate, Data) on RIAK, then trying to fetch these objects with MapReduce.

我所做的与Basho文档非常相似:

I am doing it very similar to Basho documentation:

    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构造函数被调用:

the Person's String constructor is invoked:

public Person(String str){}

(我必须具有此构造函数,否则会因缺少该异常而得到异常) 在这里,我将对象作为一个字符串-一个字符串中的对象字段带有一个奇怪的定界符.

(I must have this constructor, otherwise I get an exception for it is missing) In there I get the object as a String - the Object's fields in one string with a strange delimiter.

为什么我没有将对象自动转换为POJO?我真的需要遍历字符串并反序列化吗?我在做错什么吗?s

why am I not getting the object automatically converted to my POJO? do I really need to go over the string and deserialize it? am i doing something wrong?s

推荐答案

您正在使用的JS函数无法执行您认为的工作:)它根据具有必须提供的特定值的字段来选择对象作为阶段的论据.

The JS function you're using doesn't do what you think it does :) It selects objects based on a field with a specific value you have to supply as an argument to the phase.

我认为您正在寻找的是mapValuesJson,它将执行您似乎想做的事情.

I think what you're looking for is mapValuesJson which will do what you seem to be wanting to do.

此外,您在POJO中根本不需要构造函数.

Also, you don't need a constructor at all in your POJO.

下面的代码应该为您指明正确的方向(显然,这非常简单,POJO中的所有公共字段都没有注释):

The code below should point you in the right direction (obviously this is super-simple with all public fields in the POJO and no annotations):

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";


}

这篇关于在RIAK上获取MapReduce结果(使用Java客户端)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆