presto自定义UDF函数

以大小写转换为例
写个插件

public class PrestoUdfPlugin implements Plugin {
    @Override
    public Set<Class<?>> getFunctions() {
        return ImmutableSet.<Class<?>>builder()
                // 添加插件class
                .add(MyFunctions.class)
                .build();
    }
}

写自定义函数

import com.facebook.presto.spi.function.Description;
import com.facebook.presto.common.type.StandardTypes;
import com.facebook.presto.spi.function.Description;
import com.facebook.presto.spi.function.ScalarFunction;
import com.facebook.presto.spi.function.SqlType;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;

public class MyFunctions {
    @ScalarFunction("my_upper") // 固定参数,表示函数名的意思,也就我们在使用Presto的时候用的函数名
    @Description("我的大小写转换函数") // 函数的注释
    @SqlType(StandardTypes.VARCHAR) // 表示数据类型
    public static Slice toUpper(@SqlType(StandardTypes.VARCHAR) Slice input) {
        // 将获取到的数据转换大写
        String s = input.toStringUtf8().toUpperCase();
        // 在转换后的数据放入内存返回
        return Slices.utf8Slice(s);
    }
}

resources下创建目录,注意META-INF是父目录,services是子目录,只是idea合并显示了,不是说文件名里面有点.
然后创建文件com.facebook.presto.spi.Plugin
在这里插入图片描述
文件内容为插件内的路径,比如这里为

com.xx.xx.presto.PrestoUdfPlugin

利用maven打包成jar包上传到presto安装目录下的plugin目录,注意依赖也必须打包进去
在这里插入图片描述
之后重启presto,没启动的用start,已经启动的用restart命令.

[root@mypc01 presto]# ./bin/launcher restart
Started as 72838

jps命令查看确认有PrestoServer进程,表示启动成功.

进入presto命令行就可以使用自定义函数了

[root@mypc01 presto]# ./presto --server 192.168.xx.101:8080 --catalog hive
>select my_upper('abc');
>ABC

参考

Functions — Trino 351 Documentation
https://trino.io/docs/current/develop/functions.html

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值