Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache pig Pig:在嵌套的foreach中获取索引_Apache Pig - Fatal编程技术网

Apache pig Pig:在嵌套的foreach中获取索引

Apache pig Pig:在嵌套的foreach中获取索引,apache-pig,Apache Pig,我有一个pig脚本,代码如下: scores = LOAD 'file' as (id:chararray, scoreid:chararray, score:int); scoresGrouped = GROUP scores by id; top10s = foreach scoresGrouped{ sorted = order scores by score DESC; sorted10 = LIMIT sorted 10; GENERATE group as i

我有一个pig脚本,代码如下:

scores = LOAD 'file' as (id:chararray, scoreid:chararray, score:int);
scoresGrouped = GROUP scores by id;
top10s = foreach scoresGrouped{
    sorted = order scores by score DESC;
    sorted10 = LIMIT sorted 10;
    GENERATE group as id, sorted10.scoreid as top10candidates;
};
它给我带来了一个像

 id1, {(scoreidA),(scoreidB),(scoreIdC)..(scoreIdFoo)}
然而,我也希望包括项目的索引,所以我会得到如下结果

 id1, {(scoreidA,1),(scoreidB,2),(scoreIdC,3)..(scoreIdFoo,10)}

是否可能以某种方式将索引包含在嵌套的foreach中,或者我必须编写自己的UDF以在以后添加它?

您需要一个UDF,它的唯一参数是要向其添加秩的已排序包。我以前也有同样的需要。以下是exec函数,可为您节省一点时间:

public DataBag exec(Tuple b) throws IOException {
    try {
        DataBag bag = (DataBag) b.get(0);
        Iterator<Tuple> it = bag.iterator();
        while (it.hasNext()) {
            Tuple t = (Tuple)it.next();
            if (t != null && t.size() > 0 && t.get(0) != null) {
                t.append(n++);
            }
            newBag.add(t);
        }
    } catch (ExecException ee) {
        throw ee;
    } catch (Exception e) {
        int errCode = 2106;
        String msg = "Error while computing item number in " + this.getClass().getSimpleName();
        throw new ExecException(msg, errCode, PigException.BUG, e);           
    }

    return newBag;
}
publicdatabageexec(元组b)抛出IOException{
试一试{
数据包=(数据包)b.get(0);
迭代器it=bag.Iterator();
while(it.hasNext()){
Tuple t=(Tuple)it.next();
如果(t!=null&&t.size()>0&&t.get(0)!=null){
t、 追加(n++);
}
新增(t);
}
}捕获(被执行者){
投掷物;
}捕获(例外e){
int errCode=2106;
String msg=“计算“+this.getClass().getSimpleName()中的项目编号时出错”;
抛出新的ExecException(msg、errCode、PigException.BUG、e);
}
返回新包;
}
(计数器
n
初始化为
exec
函数外部的类变量。)


您还可以实现,这将允许您这样做,即使您的整个包无法放入内存中。(计数内置函数可以做到这一点。)只需确保设置
n=1Lcleanup()
方法中选择code>,并
返回newBag
getValue()
中,其他所有内容都是相同的。

您需要一个UDF,它的唯一参数是要向其添加排名的已排序行李。我以前也有同样的需要。以下是exec函数,可为您节省一点时间:

public DataBag exec(Tuple b) throws IOException {
    try {
        DataBag bag = (DataBag) b.get(0);
        Iterator<Tuple> it = bag.iterator();
        while (it.hasNext()) {
            Tuple t = (Tuple)it.next();
            if (t != null && t.size() > 0 && t.get(0) != null) {
                t.append(n++);
            }
            newBag.add(t);
        }
    } catch (ExecException ee) {
        throw ee;
    } catch (Exception e) {
        int errCode = 2106;
        String msg = "Error while computing item number in " + this.getClass().getSimpleName();
        throw new ExecException(msg, errCode, PigException.BUG, e);           
    }

    return newBag;
}
publicdatabageexec(元组b)抛出IOException{
试一试{
数据包=(数据包)b.get(0);
迭代器it=bag.Iterator();
while(it.hasNext()){
Tuple t=(Tuple)it.next();
如果(t!=null&&t.size()>0&&t.get(0)!=null){
t、 追加(n++);
}
新增(t);
}
}捕获(被执行者){
投掷物;
}捕获(例外e){
int errCode=2106;
String msg=“计算“+this.getClass().getSimpleName()中的项目编号时出错”;
抛出新的ExecException(msg、errCode、PigException.BUG、e);
}
返回新包;
}
(计数器
n
初始化为
exec
函数外部的类变量。)


您还可以实现,这将允许您这样做,即使您的整个包无法放入内存中。(计数内置函数可以做到这一点。)只需确保设置
n=1Lcleanup()
方法中选择code>,并
返回newBag
getValue()
中,其他所有内容都是相同的。

要为包中的元素编制索引,您可以使用LinkedIn项目中的自定义项:


为了索引包中的元素,您可以使用LinkedIn项目中的自定义项:


我发现Enumerate udf真的很难使用。例如,Enumerate(STRSPLITTOBAG(myStr,\\\\,0))总是导致空指针错误。例如,枚举(STRSPLITTOBAG(myStr,\\\\\”,0))总是导致空指针错误。