@
diudiu13216 python-kafka
class Kafka_consumer():
def __init__(self, kafkahost, kafkaport, kafkatopic, groupid,flag,offset_value):
self.kafkaHost = kafkahost
self.kafkaPort = kafkaport
self.kafkatopic = kafkatopic
self.groupid = groupid
self.consumer = KafkaConsumer(self.kafkatopic, group_id = self.groupid,
bootstrap_servers = '{kafka_host}:{kafka_port}'.format(
kafka_host=self.kafkaHost,
kafka_port=self.kafkaPort ),enable_auto_commit=flag, auto_offset_reset=offset_value)
def consume_data(self):
try:
for message in self.consumer:
print "partition",message.partition
print "key",message.key
except Exception as e:
print "error",e
这是我的代码,在跑的过程中,故意中断 kafka,并不会报错,但是我又想显示连接中断的信息