org.apache.kafka.clients.consumer
Interface Consumer

All Superinterfaces:
java.io.Closeable
All Known Implementing Classes:
KafkaConsumer, MockConsumer

public interface Consumer
extends java.io.Closeable

See Also:
KafkaConsumer, MockConsumer

Method Summary
 void close()
          Close this consumer
 OffsetMetadata commit(boolean sync)
          Commits offsets returned on the last poll() for the subscribed list of topics and partitions.
 OffsetMetadata commit(java.util.Map<TopicPartition,java.lang.Long> offsets, boolean sync)
          Commits the specified offsets for the specified list of topics and partitions to Kafka.
 java.util.Map<TopicPartition,java.lang.Long> committed(java.util.Collection<TopicPartition> partitions)
          Fetches the last committed offsets for the input list of partitions
 java.util.Map<java.lang.String,? extends Metric> metrics()
          Return a map of metrics maintained by the consumer
 java.util.Map<TopicPartition,java.lang.Long> offsetsBeforeTime(long timestamp, java.util.Collection<TopicPartition> partitions)
          Fetches offsets before a certain timestamp
 java.util.Map<java.lang.String,ConsumerRecords> poll(long timeout)
          Fetches data for the subscribed list of topics and partitions
 java.util.Map<TopicPartition,java.lang.Long> position(java.util.Collection<TopicPartition> partitions)
          Returns the fetch position of the next message for the specified topic partition to be used on the next poll()
 void seek(java.util.Map<TopicPartition,java.lang.Long> offsets)
          Overrides the fetch positions that the consumer will use on the next fetch request.
 void subscribe(java.lang.String... topics)
          Incrementally subscribe to the given list of topics.
 void subscribe(TopicPartition... partitions)
          Incrementally subscribes to a specific topic and partition.
 void unsubscribe(java.lang.String... topics)
          Unsubscribe from the specific topics.
 void unsubscribe(TopicPartition... partitions)
          Unsubscribe from the specific topic partitions.
 

Method Detail

subscribe

void subscribe(java.lang.String... topics)
Incrementally subscribe to the given list of topics. This API is mutually exclusive to subscribe(partitions)

Parameters:
topics - A variable list of topics that the consumer subscribes to

subscribe

void subscribe(TopicPartition... partitions)
Incrementally subscribes to a specific topic and partition. This API is mutually exclusive to subscribe(topics)

Parameters:
partitions - Partitions to subscribe to

unsubscribe

void unsubscribe(java.lang.String... topics)
Unsubscribe from the specific topics. Messages for this topic will not be returned from the next poll() onwards. This should be used in conjunction with subscribe(topics). It is an error to unsubscribe from a topic that was never subscribed to using subscribe(topics)

Parameters:
topics - Topics to unsubscribe from

unsubscribe

void unsubscribe(TopicPartition... partitions)
Unsubscribe from the specific topic partitions. Messages for these partitions will not be returned from the next poll() onwards. This should be used in conjunction with subscribe(topic, partitions). It is an error to unsubscribe from a partition that was never subscribed to using subscribe(partitions)

Parameters:
partitions - Partitions to unsubscribe from

poll

java.util.Map<java.lang.String,ConsumerRecords> poll(long timeout)
Fetches data for the subscribed list of topics and partitions

Parameters:
timeout - The time, in milliseconds, spent waiting in poll if data is not available. If 0, waits indefinitely. Must not be negative
Returns:
Map of topic to records for the subscribed topics and partitions as soon as data is available for a topic partition. Availability of data is controlled by ConsumerConfig.FETCH_MIN_BYTES_CONFIG and ConsumerConfig.FETCH_MAX_WAIT_MS_CONFIG. If no data is available for timeout ms, returns an empty list

commit

OffsetMetadata commit(boolean sync)
Commits offsets returned on the last poll() for the subscribed list of topics and partitions.

Parameters:
sync - If true, the commit should block until the consumer receives an acknowledgment
Returns:
An OffsetMetadata object that contains the partition, offset and a corresponding error code. Returns null if the sync flag is set to false

commit

OffsetMetadata commit(java.util.Map<TopicPartition,java.lang.Long> offsets,
                      boolean sync)
Commits the specified offsets for the specified list of topics and partitions to Kafka.

Parameters:
offsets - The map of offsets to commit for the given topic partitions
sync - If true, commit will block until the consumer receives an acknowledgment
Returns:
An OffsetMetadata object that contains the partition, offset and a corresponding error code. Returns null if the sync flag is set to false.

seek

void seek(java.util.Map<TopicPartition,java.lang.Long> offsets)
Overrides the fetch positions that the consumer will use on the next fetch request. If the consumer subscribes to a list of topics using subscribe(topics), an exception will be thrown if the specified topic partition is not owned by the consumer.

Parameters:
offsets - The map of fetch positions per topic and partition

position

java.util.Map<TopicPartition,java.lang.Long> position(java.util.Collection<TopicPartition> partitions)
Returns the fetch position of the next message for the specified topic partition to be used on the next poll()

Parameters:
partitions - Partitions for which the fetch position will be returned
Returns:
The position from which data will be fetched for the specified partition on the next poll()

committed

java.util.Map<TopicPartition,java.lang.Long> committed(java.util.Collection<TopicPartition> partitions)
Fetches the last committed offsets for the input list of partitions

Parameters:
partitions - The list of partitions to return the last committed offset for
Returns:
The list of offsets for the specified list of partitions

offsetsBeforeTime

java.util.Map<TopicPartition,java.lang.Long> offsetsBeforeTime(long timestamp,
                                                               java.util.Collection<TopicPartition> partitions)
Fetches offsets before a certain timestamp

Parameters:
timestamp - The unix timestamp. Value -1 indicates earliest available timestamp. Value -2 indicates latest available timestamp.
partitions - The list of partitions for which the offsets are returned
Returns:
The offsets for messages that were written to the server before the specified timestamp.

metrics

java.util.Map<java.lang.String,? extends Metric> metrics()
Return a map of metrics maintained by the consumer


close

void close()
Close this consumer

Specified by:
close in interface java.io.Closeable