|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import socket |
| 4 | +from kafka import KafkaProducer, KafkaConsumer, TopicPartition |
| 5 | + |
| 6 | +KAFKA_HOSTS = ["kafka:9092"] |
| 7 | + |
| 8 | +class Producer(): |
| 9 | + def __init__(self): |
| 10 | + super(Producer, self).__init__() |
| 11 | + self._client_id = socket.gethostname() |
| 12 | + self._producer = None |
| 13 | + |
| 14 | + def send(self, topic, message): |
| 15 | + if not self._producer: |
| 16 | + try: |
| 17 | + self._producer = KafkaProducer(bootstrap_servers=KAFKA_HOSTS, |
| 18 | + client_id=self._client_id, |
| 19 | + api_version=(0, 10), retries=1) |
| 20 | + except Exception as e: |
| 21 | + print(str(e)) |
| 22 | + self._producer = None |
| 23 | + |
| 24 | + if self._producer: |
| 25 | + try: |
| 26 | + self._producer.send(topic, message.encode('utf-8')) |
| 27 | + print("send "+topic+": ") |
| 28 | + print(message) |
| 29 | + except Exception as e: |
| 30 | + print(str(e)) |
| 31 | + else: |
| 32 | + print("producer not available") |
| 33 | + |
| 34 | + def flush(self): |
| 35 | + if self._producer: |
| 36 | + self._producer.flush() |
| 37 | + |
| 38 | + def close(self): |
| 39 | + if self._producer: |
| 40 | + self.flush() |
| 41 | + self._producer.close() |
| 42 | + |
| 43 | +class Consumer(): |
| 44 | + def __init__(self, group=None): |
| 45 | + super(Consumer, self).__init__() |
| 46 | + self._client_id = socket.gethostname() |
| 47 | + self._group = group |
| 48 | + |
| 49 | + def messages(self, topic, timeout=None): |
| 50 | + c = KafkaConsumer(topic, bootstrap_servers=KAFKA_HOSTS, client_id=self._client_id, |
| 51 | + group_id=self._group, api_version=(0, 10)) |
| 52 | + |
| 53 | + partitions = c.partitions_for_topic(topic) |
| 54 | + if not partitions: |
| 55 | + raise Exception("Topic "+topic+" not exist") |
| 56 | + |
| 57 | + timeout1 = 100 if timeout is None else timeout |
| 58 | + while True: |
| 59 | + partitions = c.poll(timeout1) |
| 60 | + if partitions: |
| 61 | + for p in partitions: |
| 62 | + for msg in partitions[p]: |
| 63 | + yield msg.value.decode('utf-8') |
| 64 | + if timeout is not None: |
| 65 | + yield "" |
| 66 | + |
| 67 | + c.close() |
| 68 | + |
| 69 | + def debug(self, topic): |
| 70 | + c = KafkaConsumer(bootstrap_servers=KAFKA_HOSTS, client_id=self._client_id, |
| 71 | + group_id=None, api_version=(0, 10)) |
| 72 | + |
| 73 | + # assign/subscribe topic |
| 74 | + partitions = c.partitions_for_topic(topic) |
| 75 | + if not partitions: |
| 76 | + raise Exception("Topic "+topic+" not exist") |
| 77 | + c.assign([TopicPartition(topic, p) for p in partitions]) |
| 78 | + |
| 79 | + # seek to beginning if needed |
| 80 | + c.seek_to_beginning() |
| 81 | + |
| 82 | + # fetch messages |
| 83 | + while True: |
| 84 | + partitions = c.poll(100) |
| 85 | + if partitions: |
| 86 | + for p in partitions: |
| 87 | + for msg in partitions[p]: |
| 88 | + yield msg.value.decode('utf-8') |
| 89 | + yield "" |
| 90 | + |
| 91 | + c.close() |
0 commit comments