当前位置:首页 > PHP教程 > PHP总结归纳

HadoopPigAlgebraicInterface

仔细看了一下hadoop pig 的udf 文档 在 algebraic interface 设计上还是可以学习的。 一些聚合函数,如 sum, count 都得实现 algebraic 接口 此接口要实现 三个方法,这三个方法都是返回具体实现的 class name 并且这些 class name都要实现 exec方法 public

仔细看了一下hadoop pig 的udf 文档 在 algebraic interface 设计上还是可以学习的。

一些聚合函数,如 sum, count 都得实现 algebraic 接口

此接口要实现 三个方法,这三个方法都是返回具体实现的 class name

并且这些 class name都要实现 exec方法

    public interface algebraic{
            public string getinitial();
            public string getintermed();
            public string getfinal();
    }

看 pig built in count 的实现

这几个方法都可以对应对相关的hadoop 的map combine,reduce

map 对应 initial

combine 对应 intermed

reduce 对应 reduce

发现 java 的内部静态内还是很有用的

public class count extends evalfunc implements algebraic{
    public long exec(tuple input) throws ioexception {return count(input);}
    public string getinitial() {return initial.class.getname();}
    public string getintermed() {return intermed.class.getname();}
    public string getfinal() {return final.class.getname();}
    static public class initial extends evalfunc {
            public tuple exec(tuple input) throws ioexception {return
                    tuplefactory.getinstance().newtuple(count(input));}
    }
    static public class intermed extends evalfunc {
            public tuple exec(tuple input) throws ioexception {return
                    tuplefactory.getinstance().newtuple(sum(input));}
    }
    static public class final extends evalfunc {
            public tuple exec(tuple input) throws ioexception {return sum(input);}
    }
    static protected long count(tuple input) throws execexception {
            object values = input.get(0);
            if (values instanceof databag) return ((databag)values).size();
            else if (values instanceof map) return new long(((map)values).size());
    }
    static protected long sum(tuple input) throws execexception, numberformatexception {
            databag values = (databag)input.get(0);
            long sum = 0;
            for (iterator (tuple) it = values.iterator(); it.hasnext();) {
                    tuple t = it.next();
                    sum += (long)t.get(0);
            }
            return sum;
    }
}

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!