How to attach a python consumer script to a particular kafka partition.
On running two instance of the consumer script (given below), each of them randomly picks up one partition and then consumes/prints all messages of that particular partition, As expected.
But as I need to output these messages to a partition named local file on disk, attaching each instance of the script to a pre declared partition ID would make things easier
file name eg. :
Date/Hour/PARTITION_ID-0.CSV
Date/Hour/PARTITION_ID-1.CSV
Any idea on how to achieve that.
Feel free to suggest alternatives approaches.
Kafka Setup:
Topic:my-topic3 PartitionCount:2 ReplicationFactor:2 Configs:
Topic: my-topic3 Partition: 0 Leader: 2 Replicas: 2,1 Isr: 2,1
Topic: my-topic3 Partition: 1 Leader: 1 Replicas: 1,2 Isr: 1,2
Kafka Consumer Script ( in python ) [ WITH FIX ]
from kafka import KafkaConsumer
from kafka import TopicPartition
# To consume latest messages and auto-commit offsets
#consumer = KafkaConsumer('my-topic3',
# group_id='my-group',
# bootstrap_servers=['192.168.150.80:9092'])
# To consume messages from a specific PARTITION [ FIX ]
consumer = KafkaConsumer(bootstrap_servers='192.168.150.80:9092')
consumer.assign([TopicPartition('my-topic3', 1)])
for message in consumer:
# message value and key are raw bytes -- decode if necessary!
# e.g., for unicode: `message.value.decode('utf-8')`
print ("Topic= %s : Partition= %d : Offset= %d: key= %s value= %s" % (message.topic, message.partition,
message.offset, message.key,
message.value))
Update : As suggested below , i used assign function, but kept on getting illegal state error
assign function
consumer.assign([TopicPartition('my-topic3',1)])
Error
Traceback (most recent call last):
File "consumerExample.py", line 13, in <module>
consumer.assign([TopicPartition('my-topic3',1)])
File "/usr/lib/python2.7/site-packages/kafka/consumer/group.py", line 278, in assign
self._subscription.assign_from_user(partitions)
File "/usr/lib/python2.7/site-packages/kafka/consumer/subscription_state.py", line 189, in assign_from_user
raise IllegalStateError(self._SUBSCRIPTION_EXCEPTION_MESSAGE)
kafka.errors.IllegalStateError: You must choose only one way to configure