Presto 聚合函数(UDAF) 开发详细步骤

1.加入相关依赖:

packaging>presto-plugin

 

 

    0.208

 

    

    

        com.facebook.presto

        presto-spi

        ${presto.verison}

        provided

    

 

    

    

        com.google.guava

        guava

        11.0.2

    

 

 

     com.facebook.presto

     presto-maven-plugin

     0.3

    true

2.创建sum类:

@AggregationFunction("sumdouble")

@Description("this is a double sum function")

public final class DoubleSumAggregation

{

    private DoubleSumAggregation() {}

 

    @InputFunction

    public static void input(@AggregationState NullableDoubleState state, @SqlType(StandardTypes.DOUBLE) double value)

    {

        state.setNull(false);

        state.setDouble(state.getDouble() + value);

    }

 

    @CombineFunction

    public static void combine(@AggregationState NullableDoubleState state, @AggregationState NullableDoubleState otherState)

    {

        if (state.isNull()) {

            state.setNull(false);

            state.setDouble(otherState.getDouble());

            return;

        }

 

        state.setDouble(state.getDouble() + otherState.getDouble());

    }

 

    @OutputFunction(StandardTypes.DOUBLE)

    public static void output(@AggregationState NullableDoubleState state, BlockBuilder out)

    {

        NullableDoubleState.write(DoubleType.DOUBLE, state, out);

    }

}

说明:

@AggregationFunction("sumdouble"):聚合函数的名称,会在你的使用show functions;命令的时候显示出来

@Description():想要别人更了解你的函数,你可以 增加一下描述信息

@InputFunction:标志着这个静态方法为聚合的入口,输入方法,注意没有返回值

@CombineFunction:合并函数可以把其他块的数据进行合并,必须

@OutputFunction(输出类型):标志输出方法,得到最终的结果

2.创建NullableDoubleState.class

官方代码:

@AccumulatorStateMetadata(stateSerializerClass = NullableDoubleStateSerializer.class)

public interface NullableDoubleState

        extends AccumulatorState

{

    double getDouble();

 

    void setDouble(double value);

 

    @InitialBooleanValue(true)

    boolean isNull();

 

    void setNull(boolean value);

 

    static void write(Type type, NullableDoubleState state, BlockBuilder out)

    {

        if (state.isNull()) {

            out.appendNull();

        }

        else {

            type.writeDouble(out, state.getDouble());

        }

    }

}

注意:其他类型,写法类似

3.编写注册类

/**

 * 插件注册类

 */

public class FunctionsPlugin implements Plugin {

    @Override

    public Set> getFunctions() {

        return ImmutableSet.>builder()

                .add(DoubleSumAggregation.class)

                .build();

    }

}

注意:这个类最好和DoubleSumAggregation.class放到一个package下

4.容易被忽视的部分

在resource下创建META-INF/services目录,并创建文件名为com.facebook.presto.spi.Plugin的文件,并添加内容:

xxx.xxx.xxx.FunctionsPlugin

5.打包

mvn clean package

6.把xxx.zip解压到${PRESTOHOME}/plugin

>unzip ${projectdir}/target/xxx.zip -d ${PRESTOHOME}/plugin

注意:需要解压到所有节点上

7.重启之后就可以用

你可能感兴趣的:(presto)