【问题标题】:Getting MapReduce results on RIAK (using the Java client)在 RIAK 上获取 MapReduce 结果(使用 Java 客户端)
【发布时间】:2012-07-18 14:16:05
【问题描述】:

我在 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){}

(我必须有这个构造函数,否则我会因为它丢失而出现异常) 在那里,我将对象作为字符串 - 一个字符串中的对象字段带有一个奇怪的分隔符。

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

【问题讨论】:

    标签: java mapreduce riak


    【解决方案1】:

    您正在使用的 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";
    
    
    }
    

    【讨论】:

    • 嗨,布赖恩,感谢您的回复。实际上我已经尝试过你提到的 JS 函数,但是我什至没有通过执行阶段。我得到一个 IOException:{"phase":0,"error":"[{>,477},{>,>},{>,>}]","input":"{ok,{r_object,> ,>,[{r_content,{dict,7....也许你提到的注释是我所缺少的?
    • 听起来您在 riak 中存储的任何内容都不是有效的 JSON;例如JSON.parse 错误(或至少,您存储的至少一项无效)。上面的代码是一个完整的工作示例——不需要注释;如果您想在读取/写入对象时指定索引、链接等,则使用它们。
    • 嗨,布赖恩,非常感谢!我正在运行您的示例并且得到了我的原始异常: org.codehaus.jackson.map.JsonMappingException: No适合类型 [simple type, class com.att.cso.omss.data.riak.controllers.RiakBaseController$Personx ]: 无法从 JSON 对象实例化(需要添加/启用类型信息?)然后我用您的代码创建了一个新的干净项目 - 它有效!显然,我的 POM 中的一些较旧的依赖项指向其他 json 版本,它覆盖了 RIAK 的需求。谢谢!
    • @user1510151 - 很高兴我能帮上忙。请考虑支持答案并接受它。此外,您可以直接在 riak-users 邮件列表中与我们联系,lists.basho.com/mailman/listinfo/riak-users_lists.basho.com - 我们所有的工程师都会监控并回复该列表。
    • btw - 当我使用Convertor(KRYO) 存储对象时,mapreduce 不起作用--> 我得到反序列化异常。有没有办法让它工作?因为没有 withConvertor 存储 linkWalking 是有问题的......
    猜你喜欢
    • 1970-01-01
    • 2017-04-02
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多