How to upload a Pulsar Functions jar package in Python with the REST API #23519
-
Hi Pulsar Community! I hope you're doing well. 😊 Any help or pointers would be greatly appreciated. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Here's an example of using the REST API: this is the way curl can be used to create functions from a jar/nar file: cat >/tmp/functionconfig.json <<EOF
{
"tenant": "public",
"namespace": "default",
"name": "myfunction",
"className": "MyFunction",
"runtime": "JAVA",
"inputs": [
"public/default/input-topic"
],
"output": "public/default/output-topic",
"autoAck": true,
"parallelism": 1
}
EOF
curl -H "Authorization: Bearer $(cat token)" -v -X POST \
-F "functionConfig=@/tmp/functionconfig.json;type=application/json" \
-F "[email protected];type=application/octet-stream" \
http://pulsar-function-worker:6750/admin/v3/functions/public/default/myfunction You can adapt that to Python with ChatGPT or Claude. import json
import requests
import os
def deploy_pulsar_function(token_path, function_jar_path):
# Create function config
function_config = {
"tenant": "public",
"namespace": "default",
"name": "myfunction",
"className": "MyFunction",
"runtime": "JAVA",
"inputs": ["public/default/input-topic"],
"output": "public/default/output-topic",
"autoAck": True,
"parallelism": 1
}
# Save config to temp file
config_path = "/tmp/functionconfig.json"
with open(config_path, "w") as f:
json.dump(function_config, f)
# Read token
with open(token_path) as f:
token = f.read().strip()
# Prepare request
headers = {"Authorization": f"Bearer {token}"}
files = {
"functionConfig": ("functionconfig.json", open(config_path, "rb"), "application/json"),
"data": ("function.jar", open(function_jar_path, "rb"), "application/octet-stream")
}
# Make request
url = "http://pulsar-function-worker:6750/admin/v3/functions/public/default/myfunction"
response = requests.post(url, headers=headers, files=files)
# Cleanup
os.remove(config_path)
return response.status_code, response.text
if __name__ == "__main__":
status, text = deploy_pulsar_function("token", "function.jar")
print(f"Status: {status}")
print(f"Response: {text}") |
Beta Was this translation helpful? Give feedback.
Here's an example of using the REST API:
#22266 (comment)
this is the way curl can be used to create functions from a jar/nar file: