Astra Streaming Functions
Functions are lightweight compute processes that enable you to process each message received on a topic. You can apply custom logic to that message, transforming or enriching it, and then output it to a different topic.
Functions run inside Astra Streaming and are therefore serverless. You write the code for your function in Java or Python, then upload the code. It will be automatically run for each message published to the specified input topic.
Functions are implemented using Apache Pulsar® functions. See Pulsar Functions overview for more information about Pulsar functions.
Add functions in the Functions tab of the Astra Streaming dashboard.
Select Create Function to get started.
Choose your function’s name and namespace.
Choose the file you want to pull the function from and which function you want to use within that file.
Astra generates a list of acceptable classes. Python and Java functions are added a little differently from each other.
Python functions are added by loading a Python file (.py) or a zipped Python file (.zip). When adding Python files, the Class Name is specified as the name of the Python file without the extension plus the class you want to execute.
For example, if the Python file is called
function.py and the class is
exclamation, then the class name would be
function.exclamation. The file can contain multiple classes, but only one will be used. If there is no class in the Python file (when using a basic function, for example) just specify the filename without the extension (ex.
Java functions are added by loading a Java jar file (.jar). When adding Java files, you also need to specify the name of the class to execute as the function.
Choose your input topics.
Choose Optional Destination Topics for output and logging.
Choose Advanced Options and run at least one sink instance.
Choose your Processing Guarantee. The default value is ATLEAST_ONCE. Processing Guarantee offers three options:
ATLEAST_ONCE: Each message sent to the function can be processed more than once.
ATMOST_ONCE: The message sent to the function is processed at most once. Therefore, there is a chance that the message is not processed.
EFFECTIVELY_ONCE: Each message sent to the function will have one output associated with it.
Provide an Option Configuration Key. See the Pulsar Docs for a list of configuration keys.
You have created a function for this namespace. You can confirm your function was created in the Functions tab.
You can also add functions using the Pulsar CLI. We will create a new Python function to consume a message from one topic, add an exclamation point, and publish the results to another topic.
Create the following Python function in
from pulsar import Function class ExclamationFunction(Function): def __init__(self): pass def process(self, input, context): return input + '!'
function.pyto your Pulsar cluster using the Pulsar CLI:
$ ./pulsar-admin functions create \ --py function.py \ --classname function.ExclamationFunction \ --tenant <tenant-name> \ --namespace default \ --name exclamation \ --inputs persistent:///default/ --output persistent:///default/
+ If the function is set up and ready to accept messages, you should see "Created Successfully!"
./pulsar-admin functions list --tenant <tenant-name>to list the functions in your tenant and confirm your new function was created.
Triggering a function is a convenient way to test that the function is working. When you trigger a function, you are publishing a message on the function’s input topic, which triggers the function to run. If the function has an output topic and the function returns data to the output topic, it will be displayed.
Send a test value with Pulsar CLI’s
trigger to test a function you’ve set up.
Listen for messages on the output topic:
$ bin/pulsar-client consume persistent://<tenant-name>/default/<topic-name> \ --subscription-name my-subscription --num-messages 0 # Listen indefinitely
Test your exclamation function with
$ ./pulsar-admin functions trigger \ --name exclamation \ --tenant <tenant-name> \ --namespace default \ --trigger-value "Hello world"
The trigger sends the string
Hello worldto your exclamation function. Your function should output
Hello world!to your consumed output.
You can start, stop, and restart your function by selecting it in the Functions dashboard.
Functions produce logs to help you in debugging. To view your function’s logs, open your function in the Functions dashboard.
In the upper right corner of the function log are controls to Refresh, Copy to Clipboard, and Save your function log.
A function that is already running can be updated with new configuration. The following settings can be updated:
Number of instances
If you need to update any other setting of the function, delete and then re-add the function.
To update your function, select your function in the Functions dashboard.
Select Change File to find your function locally and click Open.
Update your function’s Instances and Timeout. When you’re done, click Update.
An Updates Submitted Successfully flag will appear to let you know your function has been updated.
To delete a function, select the function to be deleted in the Functions dashboard.
A popup will ask you to confirm deletion by entering the function’s name and clicking Delete.
A Function-name Deleted Successfully! flag will appear to let you know you’ve deleted your function.
Follow along with this video from our Five Minutes About Pulsar series to see a Pulsar Python function in action.