Class PollContext<K,V>

java.lang.Object
io.confluent.parallelconsumer.PollContext<K,V>
All Implemented Interfaces:
Iterable<RecordContext<K,V>>

public class PollContext<K,V> extends Object implements Iterable<RecordContext<K,V>>
Context object used to pass messages to process to users processing functions.

Results sets can be iterated in a variety of ways. Explore the different methods available.

You can access for ConsumerRecords directly, or you can get the RecordContext wrappers, which provide extra information about the specific records, such as RecordContext.getNumberOfFailedAttempts().

Note that if you are not setting a ParallelConsumerOptions.batchSize, then you can use the getSingleRecord(), and it's convenience accessors (value(), offset(), key() getSingleConsumerRecord()). But if you have configured batching, they will all throw an IllegalArgumentException, as it's not valid to have batches of messages and yet tread the batch input as a single record.

  • Field Details

  • Constructor Details

    • PollContext

      public PollContext()
  • Method Details

    • streamInternal

      public Stream<RecordContextInternal<K,V>> streamInternal()
      Returns:
      a flat Stream of RecordContexts, which wrap the ConsumerRecords in this result set
    • stream

      public Stream<RecordContext<K,V>> stream()
      Returns:
      a flat Stream of RecordContexts, which wrap the ConsumerRecords in this result set
    • streamConsumerRecords

      public Stream<org.apache.kafka.clients.consumer.ConsumerRecord<K,V>> streamConsumerRecords()
      Returns:
      a flat Stream of ConsumerRecord in this poll set
    • getSingleRecord

      public RecordContext<K,V> getSingleRecord()
      Must not be using batching (ParallelConsumerOptions.batchSize).
      Returns:
      the single RecordContext entry in this poll set
      Throws:
      IllegalArgumentException - if a ParallelConsumerOptions.getBatchSize() has been set.
    • getSingleConsumerRecord

      public org.apache.kafka.clients.consumer.ConsumerRecord<K,V> getSingleConsumerRecord()
      Must not be using batching (ParallelConsumerOptions.batchSize).
      Returns:
      the single ConsumerRecord entry in this poll set
      See Also:
    • value

      public V value()
      For backwards compatibility with ConsumerRecord.value().

      Must not be using batching (ParallelConsumerOptions.batchSize).

      Returns:
      the single ConsumerRecord.value() entry in this poll set
      See Also:
    • key

      public K key()
      For backwards compatibility with ConsumerRecord.key().

      Must not be using batching (ParallelConsumerOptions.batchSize).

      Returns:
      the single ConsumerRecord.key() entry in this poll set
      See Also:
    • offset

      public long offset()
      For backwards compatibility with ConsumerRecord.offset().

      Must not be using batching (ParallelConsumerOptions.batchSize).

      Returns:
      the single ConsumerRecord.offset() entry in this poll set
      See Also:
    • getContextsFlattened

      public List<RecordContext<K,V>> getContextsFlattened()
      Returns:
      a flat List of RecordContexts, which wrap the ConsumerRecords in this result set
    • getConsumerRecordsFlattened

      public List<org.apache.kafka.clients.consumer.ConsumerRecord<K,V>> getConsumerRecordsFlattened()
      Returns:
      a flat List of ConsumerRecords in this result set
    • iterator

      public Iterator<RecordContext<K,V>> iterator()
      Specified by:
      iterator in interface Iterable<K>
      Returns:
      a flat Iterator of the RecordContexts, which wrap the ConsumerRecords in this result set
    • forEach

      public void forEach(Consumer<? super RecordContext<K,V>> action)
      Specified by:
      forEach in interface Iterable<K>
      Parameters:
      action - to perform on the RecordContexts, which wrap the ConsumerRecords in this result set
    • spliterator

      public Spliterator<RecordContext<K,V>> spliterator()
      Specified by:
      spliterator in interface Iterable<K>
      Returns:
      a flat Spliterator of the RecordContexts, which wrap the ConsumerRecords in this result set
    • getByTopicPartitionMap

      public Map<org.apache.kafka.common.TopicPartition,Set<RecordContext<K,V>>> getByTopicPartitionMap()
      Returns:
      a Map of TopicPartition to RecordContext Set, which wrap the ConsumerRecords in this result set
    • size

      public long size()
      Returns:
      the total count of records in this result set
    • getOffsetsFlattened

      public List<Long> getOffsetsFlattened()
      Get all the offsets for the records in this result set.

      Note that this flattens the result, so if there are records from multiple TopicPartitions, the partition they belong to will be lost. If you want that information as well, try getOffsets().

      Returns:
      a flat List of offsets in this result set
      See Also:
    • getOffsets

      public Map<org.apache.kafka.common.TopicPartition,List<Long>> getOffsets()
      Map of partitions to offsets.

      If you don't need the partition information, try getOffsetsFlattened().

      Returns:
      a map of TopicPartition to offsets, of the records in this result set
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object