PIG总和元组中的对应元素

时间:2015-08-06 08:37:16

标签: apache-pig udf

我有一大包元组包含常数但未知数量的整数(超过200)。有没有办法从这个元组中总结相应的元素?

例如SUM_TUPLES({(1, 0, 1), (2, 1, 0)})应该返回(3, 1, 1)

我用Python编写了我的UDF,但由于包和元组非常庞大,我得到GC limit exceeded error

def SUM_TUPLES(tuple_bag):
    if not tuple_bag:
        return []
    result = len(iter(tuple_bag).next())*[0]
    for tup in tuple_bag:
        for i, ele in enumerate(tup):
            result[i] += ele
    return result

1 个答案:

答案 0 :(得分:1)

已经回答here

import operator
tuple_bag = (1, 0, 1), (2, 1, 0)
tuple(map(operator.add, *tuple_bag))