Skip to content

Instantly share code, notes, and snippets.

@darkrus
Created June 12, 2016 17:16
Show Gist options
  • Save darkrus/f30d060b8b86af2bccb405d014646e44 to your computer and use it in GitHub Desktop.
Save darkrus/f30d060b8b86af2bccb405d014646e44 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from kafka import KafkaConsumer
from kafka import TopicPartition
import time
topic = TopicPartition('test',0)
# Create connection to Kafka broker
consumer = KafkaConsumer(bootstrap_servers='localhost:9092', group_id=None)
# Assign partition list for the consumer
consumer.assign([topic])
# Get position of next reccord
position = consumer.position(topic)
# Specify to fetch the last message
consumer.seek(topic, position - 1)
message = consumer.poll(1000)
# Close conection to broker
consumer.close()
print('Message : ' + str(message[topic][0].value))
print('Timestamp : ' + str(message[topic][0].timestamp))
print message
print ( int(time.time()) - int(message[topic][0].timestamp/1000) )
print message[topic][0].timestamp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment