1

对简单存储桶执行 MapReduce 查询。出于某种原因,我从杰克逊那里得到了一个例外:

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.threetierlogic.AccountService.models.User out of START_ARRAY token
 at [Source: java.io.StringReader@39494ff0; line: 1, column: 2]
        at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
        at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
        at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
        ... 179 more

这是我正在执行的 MapReduce 查询:

DB.client.mapReduce(bucketName)
    .addReducePhase(NamedErlangFunction.REDUCE_IDENTITY)
    .execute().getResult(classOf[User])

现在,如果我使用它调用 JSON,getResultRaw()它会返回一个 JSON 字符串,其中不包含任何值,只包含键:

[["accounts-user","8f0bb6e41592690d701225e263807a5e"],["accounts-user","2687cf9444013c45ba2637e9f6d3d3ad"],["accounts-user","3507e2e1f3d2818fdd276214d594c8e"],["accounts-user","fd186b0293ab7eb737f8b66e353fe4a6"],["accounts-user","bf6ce6bca0f642abfe74f2e2281e676c"],["accounts-user","b58d356551a8df6d3bbaf65e577f4b12"],["accounts-user","8126d599d259fd43f701c90787096049"],["accounts-user","33b9ae3befb23b7030b609158bb762d"],["accounts-user","770a897d5ce8c8e118ae121fc01f4c80"],["accounts-user","edae605390c35256b5df055f5574734d"],["accounts-user","ef19ad34a2be4ab8de111d1590a8768b"],["accounts-user","89a9f29ac937595038d37169f9ba7c8"],["accounts-user","85be26f43f7bb74eefa7683dcc74c555"]]

那么我在这里俯瞰什么?我需要调用某种域转换器还是 MapReduce 查询本身的问题?

编辑

如果有帮助,这就是我IRiakObjects在 Riak 中的存储方式:

def store(o: User) = bucket.store(o).withConverter(new UserConverter(bucketName)).execute()
4

1 回答 1

1

评论中提到的第一个问题是REDUCE_IDENTITY返回的存储桶/密钥对列表不是您想要的。第二个问题是......如果您存储的不是 JSON,那么您不能直接从 mapreduce 转到您的 POJO。第三个(排序)问题是...... Riak mapreduce 真的不是为二进制值制作的。

下面使用说明如何在自定义转换器中使用 Kryo的示例中KryoPersonConverter的和Person类。

我将打破代码示例并内联我的评论:

public class App 
{
    public static void main( String[] args ) throws RiakException
    {
        List<Person> personList = new ArrayList<Person>();

        Person p = new Person("Bob","111 Elm Street","555-1212");
        personList.add(p);
        p = new Person("Jenny","122 Spruce Lane","867-5309");
        personList.add(p);
        p = new Person("Steve","333 Oak place","555-1111");
        personList.add(p);


        IRiakClient client = RiakFactory.pbcClient();
        Bucket b = client.fetchBucket("people").execute();
        KryoPersonConverter converter = new KryoPersonConverter("people");

        for (Person p2 : personList)
        {
            b.store(p2).withConverter(converter).execute();
        }

        p = new Person();
        p.setName("Jenny");
        p = b.fetch(p).withConverter(converter).execute();
        assert(p.getPhone().equals("867-5309")); // I got your number

到现在为止的一切?一个OK!在使用 Kryo 序列化并检索它之后,我们在 Riak 中存储了一个 POJO。

        MapReduceResult result = client.mapReduce("people")
                                       .addMapPhase(NamedErlangFunction.MAP_OBJECT_VALUE)
                                       .execute();

        System.out.println(result.getResultRaw());

在这里我们看到了问题,因为它的输出println()是:

["\u0001\u0001\u000e111 Elm Street\u0001\u0003Bob\u0001\b555-1212","\u0001\u0001\r333 Oak place\u0001\u0005Steve\u0001\b555-1111","\u0001\u0001\u000f122 Spruce Lane\u0001\u0005Jenny\u0001\b867-5309"]

不幸的是,在讨论存储数据时,Riak 中的 mapreduce 真的是要与 JSON 数据(或纯字符串)一起使用。我们有一个 JSON 数组,其中包含我们存储的字节的 JSON 字符串。

要使用它,您必须将Strings 作为 a Collection,然后使用 Kryo 转换字节。

        Collection<String> objects = result.getResult(String.class);


        Kryo kryo = new Kryo();
        kryo.register(Person.class);
        ObjectBuffer buffer = new ObjectBuffer(kryo);

        for (String s : objects)
        {
            Person p3 = buffer.readObject(s.getBytes(), Person.class);
            System.out.println(p3.getName() + " " + p3.getPhone());
        }

        client.shutdown();     
    }
}

你会得到输出:

鲍勃 555-1212
史蒂夫 555-1111
珍妮 867-5309

于 2013-02-15T19:21:46.787 回答