Skip to content

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions functions/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# python-function

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# python-function
# Python function


This is an example of how to use the python function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is an example of how to use the python function.
This example describes how to use the Python function.


## Project Structure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Project Structure
## Project structure


```
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`.
Copy link

@Jennifer88huang-zz Jennifer88huang-zz Apr 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`.
In Python, if a folder contains `__init__` file, it is a package. In this project structure, the `python_function` and `pyserde` are both packages. Compressing `python_function` directory generates the `python_function.zip` file.


## How to use

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section describes how to use the Python function.

### Start pulsar standalone
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Start pulsar standalone
### Start Pulsar in standalone mode


Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Start python function by use zip package
### Start Python function
This example shows how to start Python function through a ZIP package.

```
./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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Replace `YOUR-PATH` with your file path.
Replace the `YOUR-PATH` parameter with your file path.


### Run produce and consume
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Run produce and consume
### Run producer and consumer
This example shows how to run a producer and a consumer.


```
python test-producer-consumer.py
```
Empty file.
34 changes: 34 additions & 0 deletions functions/python/python_function/custom_object_function.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
Empty file.
20 changes: 20 additions & 0 deletions functions/python/python_function/pyserde/serde.py
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
41 changes: 41 additions & 0 deletions functions/python/test-producer-consumer.py
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))