作者介绍: 李仕杨,SelectDB 生态研发工程师,Apache Doris Contributor。
我们在使用各个 SQL 引擎时,会遇到纷繁复杂的查询需求。一部分可以通过引擎自带的内置函数去解决,但内置函数往往具有一定通用性,在部分特殊场景下内置函数可能无法满足需求,所以一般 SQL 引擎会提供 UDF 功能,方便用户通过自己写逻辑来满足特定的需求,Apache Doris 也不例外。
在 Java UDF 之前,Apache Doris 提供了原生 UDF 。由于是使用 C++ 来编写的,执行效率高、速度更快,但是在实际使用中也会存在一些问题:
由上可知,原生的 UDF 实现起来门槛较高且存在一定的不稳定性因素。那么是否有一种实现相对简单、使用门槛较低且与 Doris 代码耦合度低的 UDF 呢?
答案是有的。在 2022 年 12 月正式发布的 Apache Doris 1.2.0 版本(https://github.com/apache/doris/releases)中,我们推出了全新的 Java UDF 和 Remote UDF 功能,其中 Java UDF 不仅能满足以上要求,并且在方便和安全角度为用户带来了全新体验:
Apache Doris 的 BE 是由 C++ 代码编写,如果想在 Doris 中实现 Java UDF,不可避免需要调用 JNI,而不正确的 JNI 调用将导致严重的性能问题。那么该如何设计 Java UDF 以解决这个问题呢?
Doris Java UDF 针对向量化引擎,其设计思路大体如下:
熟悉 Java 的朋友应该都知道,JVM 在直接内存即非堆区的 IO 操作比堆区更高效,因此 Doris Java UDF 一般是在直接内存中对数据进行 IO 操作。通常 UDF 有以下几种情况:
一般 UDF (定长UDF)
UDAF(变长输出)
通过上文介绍,我们基本了解了 Doris Java UDF 的执行情况,那么在实际生产中应该如何来使用Java UDF呢?
Java UDF 使用起来非常简单。Java UDF 在 Doris 内注册完成后,Doris 执行时通过调用 jar 包来实现 UDF 逻辑。顺序结构如下图:
``doris/samples/doris-demo/java-udf-demo/src/main/java/org/apache/doris/udf/AddOne.java
文件,编写 UDF 逻辑,你可以像 Hive UDF 一样在任何地方进行编写和打包,不必跟 Doris 环境相关联。AddOne.java
文件内容如下:// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package org.apache.doris.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
public class AddOne extends UDF {
public Integer evaluate(Integer value) {
return value == null? null: value + 1;
}
}
执行 mvn 打包命令
mvn clean package
创建 UDF
CREATE FUNCTION java_udf_name(int) RETURNS int PROPERTIES (
"file"="file:///path/to/your_jar_name.jar",
"symbol"="org.apache.doris.udf.AddOne",
"always_nullable"="true",
"type"="JAVA_UDF"
);
建表:
CREATE TABLE IF NOT EXISTS test.t1 (`col_1` int NOT NULL)
DISTRIBUTED BY HASH(col_1) PROPERTIES("replication_num" = "1");
插入数据:
insert into test.t1 values(1),(2);
使用udf:
MySQL [(none)]> select col_1, java_udf_name(col_1) as col_2 from test.t1;
+------------+------------+
| col_1 | col_2 |
+------------+------------+
| 1 | 2 |
| 2 | 3 |
+------------+------------+
至此,Doris Java UDF 的创建和使用就完成了,十分简单易用。
export JAVA_HOME=/xxx/xxx
import org.apache.hadoop.hive.ql.exec.UDF;
CREATE FUNCTION name ([,...])
[RETURNS] rettype
PROPERTIES (["key"="value"][,...])
CREATE FUNCTION java_udf_name(int) RETURNS int PROPERTIES (
"file"="file:///path/to/your_jar_name.jar",
"always_nullable"="true",
"type"="JAVA_UDF"
);
java_udf_name
是创建 UDF 的名称,可以进行更改,UDF 名称不能与 Doris 其他函数重命。
名称后的``(int)``
表示函数输入参数是 int 类型,RETURNS``
后的``int``
表示函数输出也是 int 类型;输入输出类型跟 Java 代码中 Evaluate 函数的输入输出类型要保持一致。
file表示 jar 包在本机的路径,应该修改"/path/to/your_jar_name.jar"``
作为 jar 包的绝对路径。如果是多机环境,也可以使用 http 形式表示的路径,例如"file"="http://${host}:${http_port}/${your_jar_file}"
可以使用python命令来简单启动一个http server:
nohup python -m SimpleHTTPServer 12345 > /dev/null 2>&1
(启动python的目录需要跟你的jar包保持一致,比如你的jar放在A机器的/usr/lib下,那么python命令最好也在该机器的该目录下启动)
Symbol 可以参考 Java 代码中的 Package always_nullable
表示 UDF 返回结果中是否可能出现 NULL 值,如果想要在计算中对出现的NULL值有特殊处理,以确定结果中不会返回 NULL,可以设为 false
,有利于提升整个查询计算过程的性能。
通过本文的介绍,了解了Doris Java UDF 的设计与使用方法,那么在实际的应用中,Doris Java UDF 能为使用者带来什么收益呢?
熟悉 Java 的同学也可以快速上手开发 Doris,使用简单便捷,较大提升开发效率。
兼容 Hive UDF,有效降低从 Hadoop 迁移数据的成本。
UDF 代码出错并不会影响 Doris,某种程度上保证了 Doris 的更好稳定运行。
与 Doris 代码解耦,真正做到了"Write Once Run Anywhere"
执行效率方面,Java UDF 是完全向量化执行的,一次执行多行数据只调用一次 JNI,结合堆外内存、Zero Copy 等优化技术,用户在使用 Java UDF 时,也能得到与之前的 C++ UDF 一致甚至更佳的查询性能体验。
如果你的 UDF 已被许多场景应用,可以将 UDF 贡献到 Apache Doris 社区。贡献步骤可参考:https://doris.incubator.apache.org/zh-CN/docs/dev/ecosystem/udf/contribute-udf
需要注意的是,Doris BE 端是由 C++ 代码实现的,因此你所贡献的内置 UDF 也需要由C++代码实现。Apache Doris 社区期待你的加入!
本文引用
[1] Viktor Rosenfeld, René Müller, Pinar Tözün, etc. Processing Java UDFs in a C++ environment. SoCC 2017: 419-431.
[2] Marcel Kornacker, Alexander Behm, Victor Bittorf, etc. Impala: A Modern, Open-Source SQL Engine for Hadoop. CIDR 2015.
[3]DSIP-001: Java UDF: https://cwiki.apache.org/confluence/display/DORIS/DSIP-001%3A+Java+UDF
[4]Apache Doris GitHub: https://github.com/apache/doris
|