-
Notifications
You must be signed in to change notification settings - Fork 33
Add example for python function #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
bb27906
81461a1
d114f50
1d14a53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,47 @@ | ||||||||||
# python-function | ||||||||||
|
||||||||||
This is an example of how to use the python function. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
## Project Structure | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
``` | ||||||||||
python_function | ||||||||||
├── __init__.py | ||||||||||
├── custom_object_function.py | ||||||||||
└── pyserde | ||||||||||
├── __init__.py | ||||||||||
└── serde.py | ||||||||||
``` | ||||||||||
|
||||||||||
In python, if a folder contains `__init__` file, we call it a package, python_function and pyserde are both packages. Compressing `python_function` directory will generate `python_function.zip`. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
## How to use | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section describes how to use the Python function. |
||||||||||
### Start pulsar standalone | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example shows how to start Pulsar in Docker. |
||||||||||
``` | ||||||||||
docker run -d -it \ | ||||||||||
-p 6650:6650 \ | ||||||||||
-p 8080:8080 \ | ||||||||||
--name pulsar-standalone \ | ||||||||||
apachepulsar/pulsar-all:2.7.1 \ | ||||||||||
bin/pulsar standalone | ||||||||||
``` | ||||||||||
|
||||||||||
### Start python function by use zip package | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
``` | ||||||||||
./bin/pulsar-admin functions create \ | ||||||||||
--tenant public --namespace default --name my_function \ | ||||||||||
--py /YOUR-PATH/python_function.zip \ | ||||||||||
--classname python_function.custom_object_function.CustomObjectFunction \ | ||||||||||
--custom-serde-inputs '{"input-topic-1":"python_function.pyserde.serde.CustomSerDe","input-topic-2":"python_function.pyserde.serde.CustomSerDe"}' \ | ||||||||||
--output-serde-classname python_function.pyserde.serde.CustomSerDe \ | ||||||||||
--output output-topic-3 | ||||||||||
``` | ||||||||||
Replace `YOUR-PATH` with your file path. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
### Run produce and consume | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
``` | ||||||||||
python test-producer-consumer.py | ||||||||||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
|
||
|
||
from pulsar import Function, SerDe | ||
from python_function.pyserde.serde import MyObject | ||
|
||
# Function that deals with custom objects | ||
class CustomObjectFunction(Function): | ||
def __init__(self): | ||
pass | ||
|
||
def process(self, input, context): | ||
retval = MyObject() | ||
retval.a = input.a + 11 | ||
retval.b = input.b + 24 | ||
return retval |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from pulsar import SerDe | ||
|
||
class MyObject(object): | ||
def __init__(self): | ||
self.a = 0 | ||
self.b = 0 | ||
|
||
class CustomSerDe(SerDe): | ||
def __init__(self): | ||
pass | ||
|
||
def serialize(self, object): | ||
return ("%d,%d" % (object.a, object.b)).encode('utf-8') | ||
|
||
def deserialize(self, input_bytes): | ||
split = str(input_bytes.decode()).split(',') | ||
retval = MyObject() | ||
retval.a = int(split[0]) | ||
retval.b = int(split[1]) | ||
return retval |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from pulsar import Client, SerDe | ||
|
||
class MyObject(object): | ||
def __init__(self): | ||
self.a = 0 | ||
self.b = 0 | ||
|
||
class CustomSerDe(SerDe): | ||
def __init__(self): | ||
pass | ||
def serialize(self, object): | ||
return ("%d,%d" % (object.a, object.b)).encode('utf-8') | ||
def deserialize(self, input_bytes): | ||
split = str(input_bytes.decode()).split(',') | ||
retval = MyObject() | ||
retval.a = int(split[0]) | ||
retval.b = int(split[1]) | ||
return retval | ||
|
||
client = Client('pulsar://localhost:6650') | ||
producer1 = client.create_producer(topic='input-topic-1') | ||
producer2 = client.create_producer(topic='input-topic-2') | ||
consumer = client.subscribe( | ||
topic='output-topic-3', | ||
subscription_name='my-subscription') | ||
|
||
ser = CustomSerDe() | ||
for i in range(100): | ||
dataObject = MyObject() | ||
dataObject.a = i | ||
dataObject.b = i + 100 | ||
d = ser.serialize(dataObject) | ||
producer1.send(d) | ||
producer2.send(d) | ||
print('send msg "%d", "%d"' % (dataObject.a, dataObject.b)) | ||
for i in range(100): | ||
msg = consumer.receive() | ||
consumer.acknowledge(msg) | ||
ex = msg.value() | ||
d = ser.deserialize(ex) | ||
print('receive and ack msg "%d", "%d"' % (d.a, d.b)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.