1、引入所需pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.czj</groupId>
<artifactId>presto</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.yahoo.datasketches:memory</include>
<include>com.yahoo.datasketches:sketches-core</include>
<include>com.google.guava:guava</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<name>presto</name>
<description>presto</description>
<dependencies>
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-spi</artifactId>
<version>0.281</version>
</dependency>
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-array</artifactId>
<version>0.264</version>
</dependency>
<dependency>
<groupId>com.google.gdata</groupId>
<artifactId>core</artifactId>
<version>1.47.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.yahoo.datasketches</groupId>
<artifactId>sketches-core</artifactId>
<version>0.13.0</version>
</dependency>
</dependencies>
</project>
2、添加resources
创建目录META-INF/services文章来源:https://www.toymoban.com/news/detail-535491.html
创建文件 com.facebook.presto.spi.Plugin,指明你所实现的Plugin位置文章来源地址https://www.toymoban.com/news/detail-535491.html
com.test.presto.function.InstallPlugin
3、函数的实现
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 com.yahoo.memory.Memory;
import com.yahoo.sketches.tuple.DoubleSummary;
import com.yahoo.sketches.tuple.DoubleSummaryDeserializer;
import com.yahoo.sketches.tuple.Sketch;
import com.yahoo.sketches.tuple.Sketches;
import io.airlift.slice.Slice;
/**
将byte[]的sketch转换成double类型
*/
public class SketchFunciton {
@ScalarFunction("sketch_estimate")
@Description("hive sketch to double function")
@SqlType(StandardTypes.DOUBLE)
public static double sketch_estimate(@SqlType(StandardTypes.VARBINARY) Slice input) {
try {
Sketch<DoubleSummary> sketch = Sketches.heapifySketch(
Memory.wrap(input.byteArray()),
new DoubleSummaryDeserializer()
);
return sketch.getEstimate();
} catch (Exception e) {
e.printStackTrace();
return -1d;
}
}
}
实现Plugin接口
import com.facebook.presto.spi.Plugin;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
public class InsightPlugin implements Plugin {
@Override
public Set<Class<?>> getFunctions()
{
return ImmutableSet.<Class<?>>builder()
.add(SketchFunciton.class)
.build();
}
}
4、打包放入presto安装目录的plugin目录中(博主非常规操作,放入了plugin/hive-function-namespace)
5、重启presto服务,即可使用自定义函数
到了这里,关于Presto自定义函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!