Migrate load_example.py to KafkaProducer / KafkaConsumer
This commit is contained in:
@@ -1,43 +1,46 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import threading, logging, time, collections
|
import threading, logging, time
|
||||||
|
|
||||||
from kafka.client import KafkaClient
|
from kafka import KafkaConsumer, KafkaProducer
|
||||||
from kafka.consumer import SimpleConsumer
|
|
||||||
from kafka.producer import SimpleProducer
|
|
||||||
|
|
||||||
msg_size = 524288
|
msg_size = 524288
|
||||||
|
|
||||||
|
producer_stop = threading.Event()
|
||||||
|
consumer_stop = threading.Event()
|
||||||
|
|
||||||
class Producer(threading.Thread):
|
class Producer(threading.Thread):
|
||||||
daemon = True
|
big_msg = b'1' * msg_size
|
||||||
big_msg = "1" * msg_size
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
client = KafkaClient("localhost:9092")
|
producer = KafkaProducer(bootstrap_servers='localhost:9092')
|
||||||
producer = SimpleProducer(client)
|
|
||||||
self.sent = 0
|
self.sent = 0
|
||||||
|
|
||||||
while True:
|
while not producer_stop.is_set():
|
||||||
producer.send_messages('my-topic', self.big_msg)
|
producer.send('my-topic', self.big_msg)
|
||||||
self.sent += 1
|
self.sent += 1
|
||||||
|
producer.flush()
|
||||||
|
|
||||||
|
|
||||||
class Consumer(threading.Thread):
|
class Consumer(threading.Thread):
|
||||||
daemon = True
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
client = KafkaClient("localhost:9092")
|
consumer = KafkaConsumer(bootstrap_servers='localhost:9092',
|
||||||
consumer = SimpleConsumer(client, "test-group", "my-topic",
|
auto_offset_reset='earliest')
|
||||||
max_buffer_size = None,
|
consumer.subscribe(['my-topic'])
|
||||||
)
|
|
||||||
self.valid = 0
|
self.valid = 0
|
||||||
self.invalid = 0
|
self.invalid = 0
|
||||||
|
|
||||||
for message in consumer:
|
for message in consumer:
|
||||||
if len(message.message.value) == msg_size:
|
if len(message.value) == msg_size:
|
||||||
self.valid += 1
|
self.valid += 1
|
||||||
else:
|
else:
|
||||||
self.invalid += 1
|
self.invalid += 1
|
||||||
|
|
||||||
|
if consumer_stop.is_set():
|
||||||
|
break
|
||||||
|
|
||||||
|
consumer.close()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
threads = [
|
threads = [
|
||||||
Producer(),
|
Producer(),
|
||||||
@@ -48,6 +51,8 @@ def main():
|
|||||||
t.start()
|
t.start()
|
||||||
|
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
|
producer_stop.set()
|
||||||
|
consumer_stop.set()
|
||||||
print 'Messages sent: %d' % threads[0].sent
|
print 'Messages sent: %d' % threads[0].sent
|
||||||
print 'Messages recvd: %d' % threads[1].valid
|
print 'Messages recvd: %d' % threads[1].valid
|
||||||
print 'Messages invalid: %d' % threads[1].invalid
|
print 'Messages invalid: %d' % threads[1].invalid
|
||||||
@@ -55,6 +60,6 @@ def main():
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
|
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
|
||||||
level=logging.DEBUG
|
level=logging.INFO
|
||||||
)
|
)
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user