Skip to content

Commit 9b0ca8b

Browse files
committed
feat: add support for broker setting
1 parent 5df364d commit 9b0ca8b

8 files changed

Lines changed: 97 additions & 67 deletions

File tree

README.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,19 @@ topics = [
2525
Topic("test-topic-worker", hello_world)
2626
]
2727

28-
streamapp = App(topics=topics)
28+
streamapp = App(topics=topics, broker="kafka:0.0.0.0:29092")
2929

3030
while True:
3131
# This should be any other worker
3232
loop.run_until_complete(streamapp.run())
3333
```
3434

35+
## Example
36+
37+
- [simple-kafka-consumer](example/simple-kafka-consumer)
38+
3539
## Usage
3640

3741
1. Clone repository
3842
2. Run `poetry install`
3943
3. Write apps inside `example` folder (for now)
40-
41-
## TODO
42-
43-
- [ ] emit events interface
44-
- [ ] timers
45-
- [ ] Kstream feature compatibility
46-
- [ ] general interface stability
47-
- [ ] documentation
48-
- [ ] tests

dam/application.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@
55
class App:
66
def __init__(
77
self,
8+
broker: str,
89
topics: Optional[List] = None,
910
loop: Optional[asyncio.AbstractEventLoop] = None,
1011
):
1112
if loop is None:
1213
loop = asyncio.get_event_loop()
1314
self.loop = loop
1415
self.topics = topics
16+
self.broker, self.hostname = broker.split(":", 1)
1517

1618
async def run(self):
1719
for topic in self.topics:
20+
topic._configure({
21+
"bootstrap_servers": self.hostname
22+
})
1823
await topic.run()
24+
25+
async def __call__(self, loop):
26+
if loop is not None:
27+
self.loop = loop
28+
await self.run()

dam/topic.py

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import asyncio
2-
from typing import Callable
2+
from typing import Callable, Dict
33

4-
from aiokafka import AIOKafkaConsumer
4+
from aiokafka import AIOKafkaConsumer, errors
55

66
from dam.stream import Stream
77

88

99
class Topic:
1010
"""Kafka topic"""
1111

12-
def __init__(self, name: str, func: Callable):
12+
def __init__(self, name: str, func: Callable, **kwargs):
1313
self.name = name
1414
self.func = func
15+
self.consumer_kwargs: Dict = kwargs
16+
17+
def _configure(self, conf: Dict):
18+
self.conf = conf
1519

1620
def create_consumer(
1721
self, name: str, loop: asyncio.AbstractEventLoop = None
@@ -22,13 +26,14 @@ def create_consumer(
2226
return AIOKafkaConsumer(
2327
name,
2428
loop=loop,
25-
bootstrap_servers="0.0.0.0:29092",
26-
group_id="test-consumer-group",
2729
auto_offset_reset="earliest",
30+
**self.conf,
31+
**self.consumer_kwargs
2832
)
2933

3034
async def run(self):
3135
consumer = self.create_consumer(self.name)
36+
3237
await consumer.start()
3338

3439
try:
@@ -37,29 +42,3 @@ async def run(self):
3742
finally:
3843
# Will leave consumer group; perform autocommit if enabled.
3944
await consumer.stop()
40-
41-
42-
# async def consume_from_kafka(loop):
43-
# logger.debug("Task Consuming from kafka initiated...")
44-
# consumer = AIOKafkaConsumer(
45-
# TOPIC,
46-
# loop=loop,
47-
# bootstrap_servers=BOOTSTRAP_SERVERS,
48-
# group_id=GROUP_ID,
49-
# auto_offset_reset=AUTO_OFFSET_RESET,
50-
# )
51-
52-
# # Get cluster layout and join group `my-group`await consumer.start()
53-
# await consumer.start()
54-
55-
# try:
56-
# # Consume messages
57-
# async for msg in consumer:
58-
# print(f"Task Consuming from kafka")
59-
# print(
60-
# f"Topic: {msg.topic}, Partition: {msg.partition}, Offset: {msg.offset},"
61-
# f" Key: {msg.key}, Value: {msg.value}, Timestamp: {msg.timestamp}\n"
62-
# )
63-
# finally:
64-
# # Will leave consumer group; perform autocommit if enabled.
65-
# await consumer.stop()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Simple kafka consumer example
2+
3+
1. Run docker compose
4+
5+
```bash
6+
docker-compose up
7+
```
8+
9+
2. Install requirmeents
10+
11+
```bash
12+
pip install -r requirements.txt
13+
```
14+
15+
3. Run the code from the root folder of dam
16+
17+
```bash
18+
python example/simple-kafka-consumer.py
19+
```
20+
21+
4. Send events
22+
23+
Open a producer console
24+
25+
```bash
26+
docker-compose exec kafka kafka-console-producer --broker-list kafka:9092 --topic test-topic-worker
27+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: '3'
2+
services:
3+
zookeeper:
4+
image: "confluentinc/cp-zookeeper"
5+
hostname: zookeeper
6+
ports:
7+
- 32181:32181
8+
environment:
9+
- ZOOKEEPER_CLIENT_PORT=32181
10+
- ALLOW_ANONYMOUS_LOGIN=yes
11+
kafka:
12+
image: confluentinc/cp-kafka
13+
hostname: kafka
14+
container_name: kafka
15+
ports:
16+
- 9092:9092
17+
- 29092:29092
18+
depends_on:
19+
- zookeeper
20+
environment:
21+
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:32181
22+
- KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1
23+
- KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
24+
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT_HOST://localhost:29092,PLAINTEXT://kafka:9092
25+
- KAFKA_BROKER_ID=1
26+
- ALLOW_PLAINTEXT_LISTENER=yes
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aioworker
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import asyncio
2+
from aioworker import Worker
3+
from dam import App, Topic
4+
5+
loop = asyncio.get_event_loop()
6+
7+
8+
async def hello_world(stream):
9+
async for value in stream:
10+
print(value.value)
11+
12+
topics = [
13+
Topic("test-topic-worker", hello_world),
14+
]
15+
16+
streamapp = App(topics=topics, broker="kafka:0.0.0.0:29092")
17+
18+
Worker(tasks=[streamapp]).run()

example/simple_consumer.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)