I am continuously producing a message to a topic using pykafka
producer.produce('test')
I would like to get the most recent message. I found a solution on the pykafka Github page which suggests:
client = KafkaClient(hosts="xxxxxxx")
topic = client.topics['mytopic']
consumer = topic.get_simple_consumer(
auto_offset_reset=OffsetType.LATEST,
reset_offset_on_start=True)
LAST_N_MESSAGES = 2
offsets = [(p, op.next_offset - LAST_N_MESSAGES) for p, op in consumer._partitions.iteritems()]
consumer.reset_offsets(offsets)
consumer.consume()
However, I don't really understand what is going on here, and it only gets the most recent message if there are at least two messages already there.
Is there a more robust solution?