在Pig中将多个地图组合在一起 [英] Combining Multiple Maps together in Pig

查看:63
本文介绍了在Pig中将多个地图组合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次使用猪.我已经找到了想要的答案,但嵌套格式很奇怪:

I am using pig for the first time. I've gotten to the point where I have exactly the answer I want, but in a weirdly nested format:

{(price,49),(manages,"1d74426f-2b0a-4777-ac1b-042268cab09c")}

我希望输出为单个地图,没有任何包装:

I'd like the output to be a single map, without any wrapping:

[price#49, manages#"1d74426f-2b0a-4777-ac1b-042268cab09c"]

我已经设法使用TOMAP来解决这个问题,但是我不知道如何合并和展平它.

I've managed to use TOMAP to get this far, but I can't figure out how to merge and flatten it away.

{([price_specification#{"amount":49,"currency":"USD"}]),([manages#"newest-nodes/1d74426f-2b0a-4777-ac1b-042268cab09c"])}

我应该怎么做?

推荐答案

不幸的是,没有内置函数可以为您执行此操作.您必须编写自己的UDF.幸运的是,这很简单.

Unfortunately, there are no built-in functions to do this for you. You'll have to write your own UDF. Fortunately, this is a simple one.

exec方法将类似于:

public Map<String, Object> exec(Tuple input) {
    Map<String, Object> m = new HashMap<String, Object>();
    for (int i = 0; i < input.size(); i++)
        m.putAll((Map<String, Object>) input.get(i));

    return m;
}

UDF可以使用任意数量的映射作为参数.

The UDF could take any number of maps as arguments.

请注意,如果两个或两个以上的地图共享一个密钥,那么最后遇到的一个将被保留,另一个将被覆盖.

Note that if two or more maps share a key, then the final one encountered will be the one that is kept and the others get overwritten.

这篇关于在Pig中将多个地图组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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