akshayg21
akshayg21

Reputation: 51

How to run kafka producer that is written in Python?

I am new to Kafka but have seen a few tutorials so I know how Kafka works. I am trying to run a producer that I have written in Python but I don't know how to run this file after I have started my zookeeper server and kafka server. If anyone can tell me the structure of the command that is to be written in command prompt, I would really appreciate it. Thanks!

Kafka Producer:

import json
import time
from kafka import KafkaProducer
from kafka.errors import KafkaError
from kafka.future import log

if __name__ == "__main__":
  producer = KafkaProducer(bootstrap_servers= 'localhost: 9092')

future = producer.send('my-topic', b"test")

try:
    record_metadata = future.get( timeout=10)
except KafkaError :
    log.exeption()
    pass

print( record_metadata.topic)
print(record_metadata.partition)
print(record_metadata.offset)

producer = KafkaProducer(value_serializer = lambda m: json.dumps(m).encode('ascii'))
producer.send('json-topic',{'key':'value'})

for _ in range (100):
    producer.send('my-topic', b"test")
    producer.send('my-topic',b"\xc2Hola, mundo!")
    time.sleep(1)

Upvotes: 1

Views: 2248

Answers (2)

Dry_accountant_09
Dry_accountant_09

Reputation: 1499

Add shebang line at the top to your script:

#!/usr/bin/env python-version Replace the python-version with python2 for 2.x and python3 for 3.x To check the version of python use the command: python -V The shebang line will determine the script ability to run as standalone. This will help when you want to double-click the script and execute it and not from the terminal. or simply say

python scriptname.py

Upvotes: 1

streetturtle
streetturtle

Reputation: 5850

So your question is how to run a python script? Simply save it, make executable and execute:

chmod +x ./kProducer.py
python ./kproducer.py

More detail are here: How to Run a Python Script via a File or the Shell

Upvotes: 2

Related Questions