在 Apache Pulsar 之 Java Function 實(shí)踐篇 我們介紹了如何在本地快速編寫并部署自己的 Java Function,這篇文章將向大家介紹如何在本地快速部署并運(yùn)行 Python Function。
在 Function 系列的 第一篇 中,我們說(shuō)到,Pulsar Functions 的實(shí)現(xiàn)有兩種方式,一種是 Plugin 的形式,一種是 SDK 的形式。Python 和 Java 都屬于 Plugin 的實(shí)現(xiàn)形式,所以他們的部署、運(yùn)行以及內(nèi)部實(shí)現(xiàn)的原理都很相似,都是動(dòng)態(tài)加載用戶編寫好的 user code file,將其內(nèi)嵌到對(duì)應(yīng)的 instance file 中,作為 Function 運(yùn)行的一部分,最后將一個(gè)完整的 instance file 提交給 Function Worker 來(lái)做相應(yīng)的處理。所以 Apache Pulsar 之 Java Function 實(shí)踐篇 中提到的加載 user code file 的原理在這里同樣適用。
編寫 Python Function
Python Function 代碼示例:
from pulsar import Function
# The classic ExclamationFunction that appends an exclamation at the end
# of the input
class ExclamationFunction(Function):
def __init__(self):
pass
def process(self, input, context):
return input + '!'
在上述代碼示例中,我們首先從 Pulsar 中導(dǎo)入 Function 模塊: from pulsar import Function,當(dāng)用戶編寫自己的 user code file 時(shí),繼承 Function class,并實(shí)現(xiàn) process() 方法。
process() 主要有兩個(gè)參數(shù): input 代表用戶的輸入, context 代表 Pulsar Function 對(duì)外暴露給用戶的一個(gè)接口,用戶可以根據(jù)提供的 context 對(duì)象,獲取 Python Function 中的屬性。
部署 Python Function
部署 Python Function 包含以下 3 個(gè)步驟:
(1) 安裝 Python client。
Python Function 的實(shí)現(xiàn)依賴于 Python client,所以在部署 Python Function 之前,需要安裝相應(yīng)版本的 Python client。
在 Python Function 中,你可以使用如下命令,安裝指定版本的 Python client:
pip install python-client==2.3.0
(2)啟動(dòng) Pulsar。
Pulsar Function 相當(dāng)于 Pulsar 的計(jì)算單元,其本質(zhì)的運(yùn)行最后是交給 Pulsar broker 去做相應(yīng)的處理。所以,在運(yùn)行 Pulsar Function 之前,我們需要先運(yùn)行 Pulsar,具體運(yùn)行的方式參照 Apache Pulsar 之 Java Function實(shí)踐篇 部署 standalone Pulsar 的模塊。當(dāng)啟動(dòng) Docker image 之后,首先使用 docker cp 將所需要的 Python file 拷貝到 Pulsar 的 Docker image 內(nèi)。之后執(zhí)行:
docker exec -it [CONTAINER ID] /bin/bash
(3)啟動(dòng) Function 實(shí)例。
進(jìn)入容器內(nèi)部。bin 目錄存放了所有運(yùn)行 Pulsar 相關(guān)的命令,Pulsar Function 的相關(guān)命令托管在 pulsar-admin 之下,所以我們可以使用 ./bin/pulsar-admin 來(lái)快速啟動(dòng)一個(gè) Function 的實(shí)例。
在完成啟動(dòng)之前,F(xiàn)unction 運(yùn)行還需要一些必要的參數(shù)列表, Apache Pulsar 之 Java Function實(shí)踐篇 的 部署 Java Function 模塊,我們對(duì)每一個(gè)參數(shù)做了詳細(xì)的解釋,下面我們主要說(shuō)明一下 Python Function 與 Java Function 在參數(shù)列表中有哪些異同,具體如下:
相同點(diǎn)
- functions
- localrun/create
- inputs
- output
- tenant
- namespace
- name
- classname
以上參數(shù)屬于運(yùn)行 Pulsar Function 的必要參數(shù),包括之后將介紹到的 Go Function 也是同理。需要特別說(shuō)明的是,inputs 是復(fù)數(shù),output 是單數(shù),這說(shuō)明在 function 中,我們?cè)试S有多個(gè) topics 作為 function 的輸入,但是輸出只能有一個(gè)。
不同點(diǎn)
- --py
為了區(qū)分不同語(yǔ)言的 function,用戶可以根據(jù)語(yǔ)言來(lái)指定不同的 function。在 Java 中,使用 --jar 來(lái)指定需要運(yùn)行的 jar 包,Python 中通過(guò) --py 來(lái)指定具體需要運(yùn)行的 Python Function 文件。
啟動(dòng) Python Function
完整啟動(dòng) Python Function 的命令示例如下:
./bin/pulsar-admin functions \
localrun/create \
--py [your python function path] \
--inputs [input topics] \
--output [output topic] \
--tenant [default:public] \
--namespace [default:default] \
--name [custom unique python function name] \
--calssname [your python function name]
在啟動(dòng)之后,所有輔助命令與 Java Function 均相同??梢允褂靡韵旅畈榭聪嚓P(guān)幫助文檔。
./bin/pulsar-admin functions`