You can customize functions to extend SQL statements to meet personalized requirements. These functions are called UDFs.
This section describes how to develop and apply HetuEngine function plugins.
This sample implements two function plugins described in the following table.
Parameter |
Description |
Type |
---|---|---|
add_two |
Adds 2 to the input integer and returns the result. |
ScalarFunction |
avg_double |
Aggregates and calculates the average value of a specified column. The field type of the column is double. |
AggregationFunction |
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.test.udf</groupId> <artifactId>udf-test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>hetu-plugin</packaging> <dependencies> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>26.0-jre</version> </dependency> <dependency> <groupId>io.hetu.core</groupId> <artifactId>presto-spi</artifactId> <version>1.2.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4.1</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>io.hetu</groupId> <artifactId>presto-maven-plugin</artifactId> <version>9</version> <extensions>true</extensions> </plugin> </plugins> </build> </project>
public class TestUDF4 { @ScalarFunction("add_two") @SqlType(StandardTypes.INTEGER) public static long add2(@SqlNullable @SqlType(StandardTypes.INTEGER) Long i) { return i+2; }
@AggregationFunction("avg_double") public class AverageAggregation { @InputFunction public static void input( LongAndDoubleState state, @SqlType(StandardTypes.DOUBLE) double value) { state.setLong(state.getLong() + 1); state.setDouble(state.getDouble() + value); } @CombineFunction public static void combine( LongAndDoubleState state, LongAndDoubleState otherState) { state.setLong(state.getLong() + otherState.getLong()); state.setDouble(state.getDouble() + otherState.getDouble()); } @OutputFunction(StandardTypes.DOUBLE) public static void output(LongAndDoubleState state, BlockBuilder out) { long count = state.getLong(); if (count == 0) { out.appendNull(); } else { double value = state.getDouble(); DOUBLE.writeDouble(out, value / count); } } }
public interface LongAndDoubleState extends AccumulatorState { long getLong(); void setLong(long value); double getDouble(); void setDouble(double value); }
public class RegisterFunctionTestPlugin implements Plugin { @Override public Set<Class<?>> getFunctions() { return ImmutableSet.<Class<?>>builder() .add(TestUDF4.class) .add(AverageAggregation.class) .build(); } }
Before the deployment, ensure that:
source bigdata_env
kinit HetuEngine user
Enter the password as prompted and change the password upon the first authentication.
hdfs dfs -mkdir -p /user/hetuserver/udf/data/externalFunctionsPlugin
hdfs dfs -put udf-test-0.0.1-SNAPSHOT /user/hetuserver/udf/data/externalFunctionsPlugin
hdfs dfs -chown -R hetuserver:hadoop /user/hetuserver/udf/data
cd /opt/client
source bigdata_env
kinit HetuEngine user
hetu-cli --catalog hive --schema default
select * from test1; name | price --------|------- apple | 17.8 orange | 25.0 (2 rows)
select avg_double(price) from test1;
select avg_double(price) from test1; _col0 ------- 21.4 (1 row)
select add_two(4); _col0 ------- 6 (1 row)