doris 自定义函数

Last updated on July 19, 2024 pm

🧙 Questions

doris 实现自定义函数
1.2版本以上才支持udf函数

☄️ Ideas

初始化项目
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.isxcode.com -DartifactId=udf -DinteractiveMode=false
编写自定义函数
code udf/src/main/java/com/isxcode/com/App.java
package com.isxcode.com;

import org.apache.hadoop.hive.ql.exec.UDF;

public class App extends UDF {
    public Integer evaluate(Integer value) {
        return value == null ? null : value + 1;
    }
}
编写pom文件
code udf/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>org.apache.doris</groupId>
    <artifactId>udf</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Java UDF Demo</name>
    <url>https://doris.apache.org/</url>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>2.3.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.pentaho</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <finalName>java-udf-demo</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.apache.doris.udf.FunctionServiceDemo</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>org.apache.doris.udf.FunctionServiceDemo</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
打包
# 下载依赖
mvn dependency:resolve
# 打包
mvn clean package -DskipTests
打包文件上传
  • 打包文件: target/java-udf-demo.jar
创建自定义函数

https://openfly.oss-cn-shanghai.aliyuncs.com/java-udf-demo.jar
file:///path/to/java-udf-demo-jar-with-dependencies.jar

-- 查看所有函数
SHOW FUNCTIONS;
SHOW FULL FUNCTIONS;
SHOW GLOBAL FUNCTIONS;
SHOW GLOBAL FUNCTIONS [LIKE 'function_pattern']

-- 创建自定义函数
CREATE FUNCTION ispong_db.java_udf_example(int) RETURNS int PROPERTIES (
    "file"="https://openfly.oss-cn-shanghai.aliyuncs.com/java-udf-demo.jar",
    "symbol"="com.isxcode.com.App",
    "always_nullable"="true",
    "type"="JAVA_UDF"
);

-- 删除函数
DROP FUNCTION java_udf_example(INT);
dbName.funcName()

-- 删除全局函数
DROP GLOBAL FUNCTION java_udf_example(INT)

-- 删除函数
DROP FUNCTION java_udf_example(INT)

-- 函数使用
select username, java_udf_example(age) from ispong_db.users
ERROR 1105 (HY000): errCode = 2, detailMessage = (192.168.90.91)[CANCELLED][INTERNAL_ERROR]error setting certificate verify locations: CAfile: /etc/ssl/certs/ca-certificates.crt CApath: none
yum install -y ca-certificates
ln -s /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt /etc/ssl/certs/ca-certificates.crt
# 添加阿里云镜像
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
apk update
apk add --no-cache ca-certificates

apt-get install -y ca-certificates
ln -s /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt /etc/ssl/certs/ca-certificates.crt

UDAF
UDF


doris 自定义函数
https://ispong.isxcode.com/db/doris/doris 自定义函数/
Author
ispong
Posted on
May 28, 2024
Licensed under