0

I am wondering if there is a way to create a new field in a relation and then assign some sequentially increasing number to it? Here is one example:

ordered_products = ORDER products BY price ASC;
ordered_products_with_sequential_id = FOREACH ordered_products GENERATE price, some_sequential_id;

How can I create some_sequential_id? I am not sure whether that's doable in Pig though.

4

1 回答 1

1

我怀疑您必须编写自己的 UDF 才能运行。一种方法是在实现AtomicInteger中增加 UDF 中的静态变量 ( ) exec

public class IncrEval extends EvalFunc<Long> {

    final static AtomicLong res = new AtomicLong(0);

    @Override
    public Long exec (Tuple tip) throws IOException {
        if (tip == null || tip.size() == 0) {
            return null;
        }

        res.incrementAndGet();
        return res.longValue();
    }
}

猪脚本条目:
b = FOREACH a GENERATE <something>, com.eval.IncrEval() AS ID:long;

于 2013-04-15T23:25:41.903 回答