public interface PartitionConsumer<K,V>
extends java.io.Closeable
It allows the discovery of available partitions of a given topic and the ability to bind to exactly one topic partition to consume data from it. The interface allows full control over the offset but also allows the underlying implementation to handle offset control autonomously while iterating over the records.
This interface is heavily inspired by the new unified consumer interface introduced within Kafka 0.9.0 but is backwards compatible to older Kafka version since it relies on its own data representations. Also it allows only a subset of operations (e.g. only possible to consume one partition per consumer instance) which simplifies the concrete implementation in older Kafka versions.
Modifier and Type | Method and Description |
---|---|
void |
assign(TopicPartition partition)
Manually assign a partition to this consumer.
|
void |
close()
Close the consumer
|
long |
earliestPosition()
Get the earliest offset for the assigned partition
It is an error if the consumer is not assigned to a partition before invoking this method.
|
TopicPartition |
getAssignment()
Get currently assigned topic partition
|
long |
latestPosition()
Get the latest offset for the assigned partition
It is an error if the consumer is not assigned to a partition before invoking this method.
|
java.util.Collection<TopicPartition> |
partitionsFor(java.lang.String topic)
Get partitions for a given topic
|
java.util.List<PartitionConsumerRecord<K,V>> |
poll(int timeout)
Fetch data for the assigned partition.
|
long |
position()
Get the offset of the next record that will be fetched for the assigned partition (if a record with that offset exists).
|
void |
seek(long offset)
Explicitly sets the fetch offset which will be used on the next
poll(timeout)
It is an error if the consumer is not assigned to a partition before invoking this method. |
java.util.Collection<TopicPartition> partitionsFor(java.lang.String topic)
topic
- The topic to get partitions forPartitionConsumerException
- when an unrecoverable error occurs (e.g. no broker available)void assign(TopicPartition partition)
partition
- The partition to assign this consumerPartitionConsumerException
- when an unrecoverable error occurs (e.g. no broker available)TopicPartition getAssignment()
java.util.List<PartitionConsumerRecord<K,V>> poll(int timeout)
On each poll, consumer will try to use the last consumed offset as the starting offset and fetch sequentially. The last
consumed offset can be manually set through seek(long)
timeout
- The time, in milliseconds, spent waiting in poll if data is not available in the buffer.
If 0, returns immediately with any records that are available currently in the buffer, else returns empty.
Must not be negative.PartitionConsumerException
- when an unrecoverable error occurs (e.g. no broker available)long position()
PartitionConsumerException
- when an unrecoverable error occurs (e.g. no broker available)long earliestPosition()
PartitionConsumerException
- when an unrecoverable error occurs (e.g. no broker available)long latestPosition()
PartitionConsumerException
- when an unrecoverable error occurs (e.g. no broker available)void seek(long offset)
poll(timeout)
It is an error if the consumer is not assigned to a partition before invoking this method.offset
- the offset which will be used on the next poll(timeout)
PartitionConsumerException
- when an unrecoverable error occurs (e.g. no broker available)void close()
close
in interface java.lang.AutoCloseable
close
in interface java.io.Closeable