Skip to content

Instantly share code, notes, and snippets.

@hhstore
Created September 12, 2018 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhstore/66de3b0f8cdfdd5e67ecff39cf1f7d73 to your computer and use it in GitHub Desktop.
Save hhstore/66de3b0f8cdfdd5e67ecff39cf1f7d73 to your computer and use it in GitHub Desktop.
kafka: producer
from kafka import KafkaConsumer, KafkaProducer
# pip install kafka-python
SERVERS = ['localhost:9092', ]
def produce():
p = KafkaProducer(bootstrap_servers=SERVERS)
future = p.send("topic1", key=b"key1", value=b"v1", partition=0)
result = future.get(timeout=10)
print(result)
if __name__ == '__main__':
produce()
@hhstore
Copy link
Author

hhstore commented Sep 12, 2018

consumer:

from kafka import KafkaConsumer, KafkaProducer

SERVERS = ['localhost:9092', ]

def consume():
    c = KafkaConsumer("topic1", group_id='group1', bootstrap_servers=SERVERS)

    for msg in c:
        print("consume:", msg)


if __name__ == '__main__':
    consume()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment