-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the Kafka broker in Python (#1363)
* Update data model * Support Kafka as messaging protocol in Python client (#1362) * Reformat code (#1362) * Update broker __init__ --------- Co-authored-by: Sven Oehler <oehler.sven@web.de> Co-authored-by: Sven Oehler <43231162+SvenO3@users.noreply.github.com>
- Loading branch information
1 parent
25bf66f
commit 7fe524e
Showing
7 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
streampipes-client-python/streampipes/functions/broker/kafka_broker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
import logging | ||
from typing import Any, AsyncIterator, Dict | ||
|
||
from confluent_kafka import Consumer # type: ignore | ||
from streampipes.functions.broker.broker import Broker | ||
from streampipes.functions.broker.kafka_message_fetcher import KafkaMessageFetcher | ||
from streampipes.model.common import random_letters | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class KafkaBroker(Broker): | ||
"""Implementation of the NatsBroker""" | ||
|
||
async def _makeConnection(self, hostname: str, port: int) -> None: | ||
"""Helper function to connect to a server. | ||
Parameters | ||
---------- | ||
hostname: str | ||
The hostname of the of the server, which the broker connects to. | ||
port: int | ||
The port number of the connection. | ||
Returns | ||
------- | ||
None | ||
""" | ||
self.kafka_consumer = Consumer( | ||
{"bootstrap.servers": f"{hostname}:{port}", "group.id": random_letters(6), "auto.offset.reset": "latest"} | ||
) | ||
|
||
async def createSubscription(self) -> None: | ||
"""Creates a subscription to a data stream. | ||
Returns | ||
------- | ||
None | ||
""" | ||
self.kafka_consumer.subscribe([self.topic_name]) | ||
|
||
logger.info(f"Subscribed to stream: {self.stream_id}") | ||
|
||
async def publish_event(self, event: Dict[str, Any]): | ||
"""Publish an event to a connected data stream. | ||
Parameters | ||
---------- | ||
event: Dict[str, Any] | ||
The event to be published. | ||
Returns | ||
------- | ||
None | ||
""" | ||
|
||
# await self.publish(subject=self.topic_name, payload=json.dumps(event).encode("utf-8")) | ||
|
||
async def disconnect(self) -> None: | ||
"""Closes the connection to the server. | ||
Returns | ||
------- | ||
None | ||
""" | ||
self.kafka_consumer.close() | ||
logger.info(f"Stopped connection to stream: {self.stream_id}") | ||
|
||
def get_message(self) -> AsyncIterator: | ||
"""Get the published messages of the subscription. | ||
Returns | ||
------- | ||
An async iterator for the messages. | ||
""" | ||
|
||
return KafkaMessageFetcher(self.kafka_consumer) |
50 changes: 50 additions & 0 deletions
50
streampipes-client-python/streampipes/functions/broker/kafka_message_fetcher.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# | ||
# 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 confluent_kafka import Consumer # type: ignore | ||
|
||
|
||
class KafkaMsg: | ||
"""An internal representation of a Kafka message | ||
Parameters | ||
---------- | ||
data: Byte Array | ||
The received Kafka message as byte array | ||
""" | ||
|
||
def __init__(self, data): | ||
self.data = data | ||
|
||
|
||
class KafkaMessageFetcher: | ||
"""Fetches the next message from Kafka | ||
Parameters | ||
---------- | ||
consumer: Consumer | ||
The Kafka consumer | ||
""" | ||
|
||
def __init__(self, consumer: Consumer): | ||
self.consumer = consumer | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
async def __anext__(self): | ||
msg = self.consumer.poll(0.1) | ||
return KafkaMsg(msg.value()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters