在 Pig 中将多个地图组合在一起 [英] Combining Multiple Maps together in 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屋!