Senior
What attributes from Kafka can a consumer read from a message, besides the body itself?
sobes.tech AI
Answer from AI
Besides the message body (payload), a Kafka consumer can read the following message metadata:
- Message key (key) — used to determine the partition and routing logic.
- Topic (topic) — the name of the topic from which the message was read.
- Partition (partition) — the partition number within the topic.
- Offset (offset) — the position of the message within the partition, a unique identifier for reading.
- Timestamp (timestamp) — the creation or logging time of the message.
- Headers (headers) — arbitrary key-value pairs that can be used to pass additional information.
Example of obtaining these metadata in Java:
ConsumerRecord<String, String> record = ...; // received message
String key = record.key();
String value = record.value();
String topic = record.topic();
int partition = record.partition();
long offset = record.offset();
long timestamp = record.timestamp();
Headers headers = record.headers();