Combining Multiple Maps together in Pig

2019-09-08 16:31发布

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条回答
冷血范
2楼-- · 2019-09-08 17:13

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.

查看更多
登录 后发表回答