Clickhouse自定义UDF函数
文章目录
- 一、python环境
- 二、config.xml添加内容
- 三、xml文件
- 四、py文件
- 五、CK操作
一、python环境
需要安装python3环境
二、config.xml添加内容
config.xml添加如下内容,这里是*_function.xml,所以后续的xml文件都要以_function.xml结尾:
<user_defined_executable_functions_config>*_function.xml</user_defined_executable_functions_config>
三、xml文件
放在config.xml同级目录下,内容示例:
<functions><function><name>xx</name><type>executable</type><max_command_execution_time>50</max_command_execution_time><command_termination_timeout>100</command_termination_timeout><pool_size>8</pool_size><return_type>String</return_type><return_name>result</return_name><argument><type>String</type><name>data</name></argument><format>TabSeparated</format><execute_direct>0</execute_direct><command>python3 /etc/clickhouse-server/xx.py</command></function>
</functions>
<function>: 定义为一个外部函数。
<name>: 定义外部函数的名称,此处为 xx。
<type>: 定义外部函数的类型,此处为 executable,表示此函数由一个可执行文件实现。
<max_command_execution_time>: 定义外部函数的最大执行时间,单位为秒,此处为 50 秒。
<command_termination_timeout>: 定义外部函数命令的终止时间,单位为毫秒,此处为 100 毫秒。
<pool_size>: 定义外部函数的池大小来处理多线程执行请求,此处为 8 个线程。
<return_type>: 定义外部函数的返回类型,此处为字符串类型 String。
<return_name>: 定义返回值的名称,此处为 result。
<argument>: 定义外部函数的一个参数,此处参数为一个字符串 data。
<type>: 定义参数的类型,此处为 String。
<name>: 定义参数的名称,此处为 data。
<format>: 定义返回结果的格式,此处为 TabSeparated,表示以制表符为分隔符的文本格式。
<execute_direct>: 定义是否使用 direct executor(直接执行器),可选的值为 0 或 1。此处为 0,表示使用默认的执行模式。
<command>: 定义该外部函数对应的命令,此处为 python3 /etc/clickhouse-server/xx.py,表示运行 Python3 可执行文件 /etc/clickhouse-server/xx.py 来实现 cutAllWithComa 函数。
四、py文件
权限:
1. /etc/clickhouse-server 目录为755
2. python文件需要执行权限
3. /etc/clickhouse-server 下的xml文件所有者为clickhouse ,方便起见,直接对 /etc/clickhouse-server 级联设置所有者即可
路径要跟上边的xml文件保持一致,简单示例:
import requests
import sys
import reurl = 'http://ip:port/jiekou?data='if __name__ == '__main__':for data in sys.stdin:response = requests.get(url, params=data)if response.status_code == 200:data = response.texttext = ','.join(re.findall(r'"([^"]+)"', str(data)))print(text)else:print('请求失败,错误码:', response.status_code)sys.stdout.flush()pass
五、CK操作
加载function,查询是否成功,这里查询的xx要跟xml文件保持一致
system reload functions;
select * from system.functions where name = 'xx';
SELECT xx(content) xx from tb_nm;
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
