-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add python examples for hstream kafka
- Loading branch information
Showing
8 changed files
with
169 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Develop with Python Kafka client | ||
|
||
This guide will show you how to use Python Kafka client to interact with | ||
HStream. Currenty, we support [kafka-python] and [confluent-kafka]. | ||
|
||
::: info | ||
|
||
The following examples assume that you have Python3.7+ installed. | ||
|
||
::: | ||
|
||
## Installation | ||
|
||
```sh | ||
# If you want to use kafka-python | ||
pip install kafka-python | ||
|
||
# Or if you want to use confluent-kafka | ||
pip install confluent-kafka | ||
``` | ||
|
||
::: tips | ||
|
||
Prefer to use a virtual environment? Check out Python's built-in | ||
[venv](https://docs.python.org/3/library/venv.html). | ||
|
||
::: | ||
|
||
## Create a Topic | ||
|
||
::: code-group | ||
|
||
<!-- prettier-ignore --> | ||
@snippet_group kafka-examples/python/snippets/guides.py [kafka-python] common create-topic | ||
|
||
::: | ||
|
||
## Kafka Producer | ||
|
||
::: code-group | ||
|
||
<!-- prettier-ignore --> | ||
@snippet_group kafka-examples/python/snippets/guides.py [kafka-python] common produce | ||
|
||
::: | ||
|
||
## Kafka Consumer | ||
|
||
::: code-group | ||
|
||
<!-- prettier-ignore --> | ||
@snippet_group kafka-examples/python/snippets/guides.py [kafka-python] common consume | ||
|
||
::: | ||
|
||
[kafka-python]: https://github.com/dpkp/kafka-python | ||
[confluent-kafka]: https://github.com/confluentinc/confluent-kafka-python |
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 |
---|---|---|
|
@@ -3,4 +3,4 @@ order: ['write', 'receive', 'process', 'ingest-and-distribute'] | |
collapsed: false | ||
--- | ||
|
||
Develope | ||
Develop |
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
Empty file.
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,67 @@ | ||
# [common] | ||
|
||
import os | ||
from kafka.admin import NewTopic | ||
from kafka import KafkaAdminClient, KafkaConsumer, KafkaProducer | ||
|
||
# NOTE: Replace with your own host and port | ||
host = os.getenv("GUIDE_HOST", "127.0.0.1") | ||
port = os.getenv("GUIDE_PORT", 9092) | ||
topic_name = "my_topic" | ||
|
||
# [common] | ||
|
||
|
||
# [create-topic] | ||
def create_topic(): | ||
admin = KafkaAdminClient(bootstrap_servers=f"{host}:{port}") | ||
topic = NewTopic(name=topic_name, num_partitions=1, replication_factor=1) | ||
admin.create_topics([topic]) | ||
|
||
|
||
# [create-topic] | ||
|
||
|
||
# [produce] | ||
def produce(): | ||
producer = KafkaProducer( | ||
bootstrap_servers=f"{host}:{port}", | ||
acks="all", | ||
linger_ms=100, | ||
) | ||
futures = [ | ||
producer.send(topic_name, b"hello, hstream " + str(i).encode()) | ||
for i in range(5) | ||
] | ||
for future in futures: | ||
response = future.get(timeout=10) | ||
print("Producer response:", response) | ||
|
||
|
||
# [produce] | ||
|
||
|
||
# [consume] | ||
def consume(): | ||
consumer = KafkaConsumer( | ||
topic_name, | ||
bootstrap_servers=f"{host}:{port}", | ||
auto_offset_reset="earliest", | ||
enable_auto_commit=False, | ||
fetch_max_wait_ms=1000, | ||
) | ||
i = 0 | ||
for msg in consumer: | ||
print("Consumer response", msg) | ||
i += 1 | ||
if i >= 5: | ||
consumer.close() | ||
|
||
|
||
# [consume] | ||
|
||
|
||
if __name__ == "__main__": | ||
create_topic() | ||
produce() | ||
consume() |