[16]-Presto UDFs开发-插件注入式

#Presto UDFs开发示例
GitHub presto-udfs开发支持了一部分HIVE UDFs,多谢分享,在此基础上进行我们的UDF开发学习。

  • git代码presto-udfs,新建一个测试udf,
    UdfTest输入一个字符串str,返回str_hjw
package com.qubole.presto.udfs.udfLearn;

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

import static io.airlift.slice.Slices.utf8Slice;

public class UdfTest
{
  private UdfTest() {}

  @Description("This is a udf test, return a result equals concat(input,'_hjw')")
  @ScalarFunction("udf_test")
  @SqlType(StandardTypes.VARCHAR)
  public static Slice locateString(@SqlType(StandardTypes.VARCHAR) Slice input)
  {
    StringBuilder result = new StringBuilder();
    result.append(input).append("_hjw");
    return utf8Slice(result.toString());
  }
}
  • 打包jar

    mvn package 
    
  • 在Preto根目录plugin目录下新建udfs文件夹

/presto-server-0.191/plugin$ ls
accumulo                geospatial              memory                  presto-thrift           sqlserver
atop                    hive-hadoop2            ml                      raptor                  teradata-functions
blackhole               jmx                     mongodb                 redis                   tpcds
cassandra               kafka                   mysql                   redshift                tpch
example-http            localfile               postgresql              resource-group-managers udfs
  • 将打包好的udfs-2.0.2-SNAPSHOT.jar复制过来
/plugin/udfs$ ls
udfs-2.0.2-SNAPSHOT.jar
  • 重启服务(这里是单机,只启动本机服务即可,集群见参考目录)
/bin$ ./launcher start
Started as 790
  • 启动CLI
Presto/CLI/presto$ ./presto --server localhost:8080 --catalog hive --schema default
presto:default>
presto:default> select udf_test('str')
             -> ;
                       _col0
---------------------------------------------------
 Slice{base=[B@6d9ac4da, address=16, length=3}_hjw
(1 row)
Query 20180212_052513_00002_ntdq9, FINISHED, 1 node
Splits: 17 total, 17 done (100.00%)
0:00 [0 rows, 0B] [0 rows/s, 0B/s]

返回值的类型有点问题,暂时先放放…有空再检查下类型问题

Slice{base=[B@6d9ac4da, address=16, length=3}_hjw

#参考目录

  • presto-udfs
  • Plugging in Presto UDFs
  • Function not registered error #3967
  • spi-overview

你可能感兴趣的:([23]Presto)