0

根据这个合流页面:

https://cwiki.apache.org/confluence/display/FLINK/FLIP-106%3A+Support+Python+UDF+in+SQL+Function+DDL

python udf 在 Flink 1.11 中可用于 SQL 函数。

我在这里访问了 flink 文档:

https://ci.apache.org/projects/flink/flink-docs-stable/dev/table/sqlClient.html

并在终端上尝试此操作并使用以下参数启动sql-client.sh :

$ sql-client.sh embedded --pyExecutable /Users/jonathanfigueroa/opt/anaconda3/bin/python --pyFiles /Users/jonathanfigueroa/Desktop/pyflink/inference/test1.py

接着:

> Create Temporary System Function func1 as 'test1.func1' Language PYTHON;
[INFO] Function has been created. 

当我尝试时:

> Select func1(str) From (VALUES ("Name1", "Name2", "Name3"));
[ERROR] Could not execute SQL statement. Reason:
java.lang.IllegalStateException: Instantiating python function 'test1.func1' failed.

我尝试过使用:-pyarch,--pyArchives, -pyexec,--pyExecutable, -pyfs,--pyFiles在每一个组合中.zip, .py,结果都是一样的。

顺便说一句,我的 python 文件如下所示:

def func1(s):
    return s;

有什么我想念的吗?

亲切的问候,

乔纳森

4

1 回答 1

0

python UDF 应该由 中的“udf”装饰器包装pyflink.table.udf,如下所示:

from pyflink.table.types import DataTypes
from pyflink.table.udf import udf

@udf(input_types=[DataTypes.INT()], result_type=DataTypes.INT())
def add_one(a):
    return a + 1

并且在启动 sql-client 时需要加载 flink-python jar,如下所示:

$ cd $FLINK_HOME/bin
$ ./start-cluster.sh
$ ./sql-client.sh embedded -pyfs xxx.py -j ../opt/flink-python_2.11-1.11.0.jar

另外,需要添加taskmanager.memory.task.off-heap.size: 79mbto$FLINK_HOME/conf/flink-conf.yaml或其他可用于设置配置的文件(如sql客户端环境文件),否则执行python udf时会报错:

[ERROR] Could not execute SQL statement. Reason:
org.apache.flink.table.api.TableException: The configured Task Off-Heap Memory 0 bytes is less than the least required Python worker Memory 79 mb. The Task Off-Heap Memory can be configured using the configuration key'taskmanager.memory .task.off-heap.size'.

最好的,

于 2020-07-10T03:47:12.907 回答