Combining Multiple Maps together in Pig

馋奶兔 提交于 2019-12-11 18:35:00

问题


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

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"])}

How should I be going about this?


回答1:


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.

The exec method would just go something like:

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

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.



来源:https://stackoverflow.com/questions/21791240/combining-multiple-maps-together-in-pig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!