Created
June 10, 2018 11:37
-
-
Save kadnan/ce1b05d4351a1b928f612b1e72593bfa to your computer and use it in GitHub Desktop.
methods to connect Kafa Producer and publishing data.
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
def publish_message(producer_instance, topic_name, key, value): | |
try: | |
key_bytes = bytes(key, encoding='utf-8') | |
value_bytes = bytes(value, encoding='utf-8') | |
producer_instance.send(topic_name, key=key_bytes, value=value_bytes) | |
producer_instance.flush() | |
print('Message published successfully.') | |
except Exception as ex: | |
print('Exception in publishing message') | |
print(str(ex)) | |
def connect_kafka_producer(): | |
_producer = None | |
try: | |
_producer = KafkaProducer(bootstrap_servers=['localhost:9092'], api_version=(0, 10)) | |
except Exception as ex: | |
print('Exception while connecting Kafka') | |
print(str(ex)) | |
finally: | |
return _producer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment