Package Python Functions

Python functions support the following three packaging formats:

One Python file

To package a Python function into one Python file, complete the following steps.

  1. Write a Python function.

    1. from pulsar import Function # import the Function module from Pulsar
    2. # The classic ExclamationFunction that appends an exclamation at the end
    3. # of the input
    4. class ExclamationFunction(Function):
    5. def __init__(self):
    6. pass
    7. def process(self, input, context):
    8. return input + '!'

    In this example, when you write a Python function, you need to inherit the Function class and implement the process() method.

    process() mainly has two parameters:

    • input represents your input.

    • context represents an interface exposed by the Pulsar Function. You can get the attributes in the Python function based on the provided context object.

  2. Install a Python client. The implementation of a Python function depends on the Python client.

    1. pip install pulsar-client==2.10.0

    And install protobuf tools to generate the proto files:

    1. pip install 'protobuf==3.20.*'
  3. Copy the Python function file to the Pulsar image.

    1. docker exec -it [CONTAINER ID] /bin/bash
    2. docker cp <path of Python function file> CONTAINER ID:/pulsar
  4. Run the Python function using the following command.

    1. ./bin/pulsar-admin functions localrun \
    2. --classname <Python Function file name>.<Python Function class name> \
    3. --py <absolute path of Python Function file> \
    4. --inputs persistent://public/default/my-topic-1 \
    5. --output persistent://public/default/test-1 \
    6. --tenant public \
    7. --namespace default \
    8. --name PythonFunction

    The following log indicates that the Python function starts successfully.

    1. ...
    2. 07:55:03.724 [main] INFO org.apache.pulsar.functions.runtime.ProcessRuntime - Started process successfully
    3. ...

ZIP file

To package a Python function into a ZIP file, complete the following steps.

  1. Prepare the ZIP file.

    1. Assuming the zip file is named as `func.zip`, unzip the `func.zip` folder:
    2. "func/src"
    3. "func/requirements.txt"
    4. "func/deps"

    Take the exclamation.zip file as an example. The internal structure of the example is as follows.

    1. .
    2. ├── deps
    3. └── sh-1.12.14-py2.py3-none-any.whl
    4. └── src
    5. └── exclamation.py
  2. Copy the ZIP file to the Pulsar image.

    1. docker exec -it [CONTAINER ID] /bin/bash
    2. docker cp <path of ZIP file> CONTAINER ID:/pulsar
  3. Run the Python function using the following command.

    1. ./bin/pulsar-admin functions localrun \
    2. --classname exclamation \
    3. --py <absolute path of ZIP file> \
    4. --inputs persistent://public/default/in-topic \
    5. --output persistent://public/default/out-topic \
    6. --tenant public \
    7. --namespace default \
    8. --name PythonFunction

    The following log indicates that the Python function starts successfully.

    1. ...
    2. 07:55:03.724 [main] INFO org.apache.pulsar.functions.runtime.ProcessRuntime - Started process successfully
    3. ...

PIP

Package Python Functions - 图1note

The PIP method is only supported in Kubernetes runtime.

To package a Python function with PIP, complete the following steps.

  1. Configure the functions_worker.yml file.

    1. #### Kubernetes Runtime ####
    2. installUserCodeDependencies: true
  2. Write your Python Function.

    1. from pulsar import Function
    2. import js2xml
    3. # The classic ExclamationFunction that appends an exclamation at the end
    4. # of the input
    5. class ExclamationFunction(Function):
    6. def __init__(self):
    7. pass
    8. def process(self, input, context):
    9. # add your logic
    10. return input + '!'

    You can introduce additional dependencies. When Python functions detect that the file currently used is whl and the installUserCodeDependencies parameter is specified, the system uses the pip install command to install the dependencies required in Python functions.

  3. Generate the whl file.

    1. cd $PULSAR_HOME/pulsar-functions/scripts/python
    2. chmod +x generate.sh
    3. ./generate.sh <path of your Python Function> <path of the whl output dir> <the version of whl>
    4. # e.g: ./generate.sh /path/to/python /path/to/python/output 1.0.0

    The output is written in /path/to/python/output:

    1. -rw-r--r-- 1 root staff 1.8K 8 27 14:29 pulsarfunction-1.0.0-py2-none-any.whl
    2. -rw-r--r-- 1 root staff 1.4K 8 27 14:29 pulsarfunction-1.0.0.tar.gz
    3. -rw-r--r-- 1 root staff 0B 8 27 14:29 pulsarfunction.whl