Interface KafkaReadStream<K,V>
- All Superinterfaces:
ReadStream<org.apache.kafka.clients.consumer.ConsumerRecord<K,V>>, StreamBase
ReadStream for consuming Kafka ConsumerRecord.
The pause() and resume() provides global control over reading the records from the consumer.
The pause(Set) and resume(Set) provides finer grained control over reading records
for specific Topic/Partition, these are Kafka's specific operations.
-
Method Summary
Modifier and TypeMethodDescriptionManually assign a set of partitions to this consumer.Get the set of partitions currently assigned to this consumer.batchHandler(Handler<org.apache.kafka.clients.consumer.ConsumerRecords<K, V>> handler) Set the handler that will be called when a new batch of records is returned from Kafka.beginningOffsets(Set<org.apache.kafka.common.TopicPartition> topicPartitions) Get the first offset for the given partitions.beginningOffsets(org.apache.kafka.common.TopicPartition topicPartition) Get the first offset for the given partition.close()Close the streamFuture<Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata>> commit()Commit current offsets for all the subscribed list of topics and partition.Future<Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata>> commit(Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets) Commit the specified offsets for the specified list of topics and partitions to Kafka.Future<org.apache.kafka.clients.consumer.OffsetAndMetadata> committed(org.apache.kafka.common.TopicPartition topicPartition) Get the last committed offset for the given partition (whether the commit happened by this process or another).static <K,V> KafkaReadStream <K, V> create(Vertx vertx, KafkaClientOptions options) Create a new KafkaReadStream instancestatic <K,V> KafkaReadStream <K, V> create(Vertx vertx, KafkaClientOptions options, Class<K> keyType, Class<V> valueType) Create a new KafkaReadStream instancestatic <K,V> KafkaReadStream <K, V> create(Vertx vertx, KafkaClientOptions options, org.apache.kafka.common.serialization.Deserializer<K> keyDeserializer, org.apache.kafka.common.serialization.Deserializer<V> valueDeserializer) Create a new KafkaReadStream instancestatic <K,V> KafkaReadStream <K, V> Create a new KafkaReadStream instancestatic <K,V> KafkaReadStream <K, V> Create a new KafkaReadStream instancestatic <K,V> KafkaReadStream <K, V> create(Vertx vertx, Map<String, Object> config, org.apache.kafka.common.serialization.Deserializer<K> keyDeserializer, org.apache.kafka.common.serialization.Deserializer<V> valueDeserializer) Create a new KafkaReadStream instancestatic <K,V> KafkaReadStream <K, V> create(Vertx vertx, Properties config) Create a new KafkaReadStream instancestatic <K,V> KafkaReadStream <K, V> create(Vertx vertx, Properties config, Class<K> keyType, Class<V> valueType) Create a new KafkaReadStream instancestatic <K,V> KafkaReadStream <K, V> create(Vertx vertx, Properties config, org.apache.kafka.common.serialization.Deserializer<K> keyDeserializer, org.apache.kafka.common.serialization.Deserializer<V> valueDeserializer) Create a new KafkaReadStream instancestatic <K,V> KafkaReadStream <K, V> Create a new KafkaReadStream instancestatic <K,V> KafkaReadStream <K, V> create(Vertx vertx, org.apache.kafka.clients.consumer.Consumer<K, V> consumer, KafkaClientOptions options) Create a new KafkaReadStream instancelongdemand()Returns the current demand.endHandler(Handler<Void> endHandler) Set an end handler.endOffsets(Set<org.apache.kafka.common.TopicPartition> topicPartitions) Get the last offset for the given partitions.endOffsets(org.apache.kafka.common.TopicPartition topicPartition) Get the last offset for the given partition.exceptionHandler(Handler<Throwable> handler) Set an exception handler on the read stream.fetch(long amount) Fetch the specifiedamountof elements.Set a data handler.Get metadata about partitions for all topics that the user is authorized to view.Future<Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndTimestamp>> offsetsForTimes(Map<org.apache.kafka.common.TopicPartition, Long> topicPartitionTimestamps) Look up the offsets for the given partitions by timestamp.Future<org.apache.kafka.clients.consumer.OffsetAndTimestamp> offsetsForTimes(org.apache.kafka.common.TopicPartition topicPartition, long timestamp) * Look up the offset for the given partition by timestamp.partitionsAssignedHandler(Handler<Set<org.apache.kafka.common.TopicPartition>> handler) Set the handler called when topic partitions are assigned to the consumerpartitionsFor(String topic) Get metadata about the partitions for a given topic.partitionsRevokedHandler(Handler<Set<org.apache.kafka.common.TopicPartition>> handler) Set the handler called when topic partitions are revoked to the consumerpause()Pause theReadStream, it sets the buffer infetchmode and clears the actual demand.Suspend fetching from the requested partitions.paused()Get the set of partitions that were previously paused by a call topause(Set).Executes a poll for getting messages from Kafka.pollTimeout(Duration timeout) Sets the poll timeout for the underlying native Kafka Consumer.position(org.apache.kafka.common.TopicPartition partition) Get the offset of the next record that will be fetched (if a record with that offset exists).resume()Resume reading, and sets the buffer inflowingmode.Resume specified partitions which have been paused with pause.seek(org.apache.kafka.common.TopicPartition topicPartition, long offset) Overrides the fetch offsets that the consumer will use on the next poll.seek(org.apache.kafka.common.TopicPartition topicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata offsetAndMetadata) Overrides the fetch offsets that the consumer will use on the next poll.seekToBeginning(Set<org.apache.kafka.common.TopicPartition> topicPartitions) Seek to the first offset for each of the given partitions.Seek to the last offset for each of the given partitions.Subscribe to all topics matching specified pattern to get dynamically assigned partitions.Subscribe to the given list of topics to get dynamically assigned partitions.Get the current subscription.Unsubscribe from topics currently subscribed with subscribe.unwrap()Methods inherited from interface ReadStream
blockingStream, collect, pipe, pipeTo
-
Method Details
-
exceptionHandler
Description copied from interface:ReadStreamSet an exception handler on the read stream.- Specified by:
exceptionHandlerin interfaceReadStream<K>- Specified by:
exceptionHandlerin interfaceStreamBase- Parameters:
handler- the exception handler- Returns:
- a reference to this, so the API can be used fluently
-
handler
KafkaReadStream<K,V> handler(Handler<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>> handler) Description copied from interface:ReadStreamSet a data handler. As data is read, the handler will be called with the data.- Specified by:
handlerin interfaceReadStream<K>- Returns:
- a reference to this, so the API can be used fluently
-
pause
KafkaReadStream<K,V> pause()Description copied from interface:ReadStreamPause theReadStream, it sets the buffer infetchmode and clears the actual demand.While it's paused, no data will be sent to the data
handler.- Specified by:
pausein interfaceReadStream<K>- Returns:
- a reference to this, so the API can be used fluently
-
resume
KafkaReadStream<K,V> resume()Description copied from interface:ReadStreamResume reading, and sets the buffer inflowingmode. If theReadStreamhas been paused, reading will recommence on it.- Specified by:
resumein interfaceReadStream<K>- Returns:
- a reference to this, so the API can be used fluently
-
fetch
Description copied from interface:ReadStreamFetch the specifiedamountof elements. If theReadStreamhas been paused, reading will recommence with the specifiedamountof items, otherwise the specifiedamountwill be added to the current stream demand.- Specified by:
fetchin interfaceReadStream<K>- Returns:
- a reference to this, so the API can be used fluently
-
endHandler
Description copied from interface:ReadStreamSet an end handler. Once the stream has ended, and there is no more data to be read, this handler will be called.- Specified by:
endHandlerin interfaceReadStream<K>- Returns:
- a reference to this, so the API can be used fluently
-
demand
long demand()Returns the current demand.-
If the stream is in flowing mode will return
- If the stream is in fetch mode, will return the current number of elements still to be delivered or 0 if paused.
Long.MAX_VALUE.- Returns:
- current demand
-
create
Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useconfig- Kafka consumer configuration- Returns:
- an instance of the KafkaReadStream
-
create
static <K,V> KafkaReadStream<K,V> create(Vertx vertx, Properties config, Class<K> keyType, Class<V> valueType) Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useconfig- Kafka consumer configurationkeyType- class type for the key deserializationvalueType- class type for the value deserialization- Returns:
- an instance of the KafkaReadStream
-
create
static <K,V> KafkaReadStream<K,V> create(Vertx vertx, Properties config, org.apache.kafka.common.serialization.Deserializer<K> keyDeserializer, org.apache.kafka.common.serialization.Deserializer<V> valueDeserializer) Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useconfig- Kafka consumer configurationkeyDeserializer- key deserializervalueDeserializer- value deserializer- Returns:
- an instance of the KafkaReadStream
-
create
Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useconfig- Kafka consumer configuration- Returns:
- an instance of the KafkaReadStream
-
create
static <K,V> KafkaReadStream<K,V> create(Vertx vertx, Map<String, Object> config, Class<K> keyType, Class<V> valueType) Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useconfig- Kafka consumer configurationkeyType- class type for the key deserializationvalueType- class type for the value deserialization- Returns:
- an instance of the KafkaReadStream
-
create
static <K,V> KafkaReadStream<K,V> create(Vertx vertx, Map<String, Object> config, org.apache.kafka.common.serialization.Deserializer<K> keyDeserializer, org.apache.kafka.common.serialization.Deserializer<V> valueDeserializer) Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useconfig- Kafka consumer configurationkeyDeserializer- key deserializervalueDeserializer- value deserializer- Returns:
- an instance of the KafkaReadStream
-
create
Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useoptions- Kafka consumer options- Returns:
- an instance of the KafkaReadStream
-
create
static <K,V> KafkaReadStream<K,V> create(Vertx vertx, KafkaClientOptions options, Class<K> keyType, Class<V> valueType) Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useoptions- Kafka consumer optionskeyType- class type for the key deserializationvalueType- class type for the value deserialization- Returns:
- an instance of the KafkaReadStream
-
create
static <K,V> KafkaReadStream<K,V> create(Vertx vertx, KafkaClientOptions options, org.apache.kafka.common.serialization.Deserializer<K> keyDeserializer, org.apache.kafka.common.serialization.Deserializer<V> valueDeserializer) Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useoptions- Kafka consumer optionskeyDeserializer- key deserializervalueDeserializer- value deserializer- Returns:
- an instance of the KafkaReadStream
-
create
static <K,V> KafkaReadStream<K,V> create(Vertx vertx, org.apache.kafka.clients.consumer.Consumer<K, V> consumer) Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useconsumer- native Kafka consumer instance- Returns:
- an instance of the KafkaReadStream
-
create
static <K,V> KafkaReadStream<K,V> create(Vertx vertx, org.apache.kafka.clients.consumer.Consumer<K, V> consumer, KafkaClientOptions options) Create a new KafkaReadStream instance- Parameters:
vertx- Vert.x instance to useconsumer- native Kafka consumer instanceoptions- options used only for tracing settings- Returns:
- an instance of the KafkaReadStream
-
committed
Future<org.apache.kafka.clients.consumer.OffsetAndMetadata> committed(org.apache.kafka.common.TopicPartition topicPartition) Get the last committed offset for the given partition (whether the commit happened by this process or another).- Parameters:
topicPartition- topic partition for getting last committed offset- Returns:
- a future notified on operation completed
-
pause
Suspend fetching from the requested partitions.Due to internal buffering of messages, the record handler will continue to observe messages from the given
topicPartitionsuntil some time after the givencompletionHandleris called. In contrast, the once the givencompletionHandleris called thebatchHandler(Handler)will not see messages from the giventopicPartitions.- Parameters:
topicPartitions- topic partition from which suspend fetching- Returns:
- a
Futurecompleted with the operation result
-
paused
Get the set of partitions that were previously paused by a call topause(Set).- Returns:
- a
Futurecompleted with the operation result
-
resume
-
seekToEnd
Seek to the last offset for each of the given partitions.Due to internal buffering of messages, the record handler will continue to observe messages fetched with respect to the old offset until some time after the given
completionHandleris called. In contrast, the once the givencompletionHandleris called thebatchHandler(Handler)will only see messages consistent with the new offset.- Parameters:
topicPartitions- topic partition for which seek- Returns:
- a
Futurecompleted with the operation result
-
seekToBeginning
Seek to the first offset for each of the given partitions.Due to internal buffering of messages, the record handler will continue to observe messages fetched with respect to the old offset until some time after the given
completionHandleris called. In contrast, the once the givencompletionHandleris called thebatchHandler(Handler)will only see messages consistent with the new offset.- Parameters:
topicPartitions- topic partition for which seek- Returns:
- a
Futurecompleted with the operation result
-
seek
Overrides the fetch offsets that the consumer will use on the next poll.Due to internal buffering of messages, the record handler will continue to observe messages fetched with respect to the old offset until some time after the given
completionHandleris called. In contrast, the once the givencompletionHandleris called thebatchHandler(Handler)will only see messages consistent with the new offset.- Parameters:
topicPartition- topic partition for which seekoffset- offset to seek inside the topic partition- Returns:
- a
Futurecompleted with the operation result
-
seek
Future<Void> seek(org.apache.kafka.common.TopicPartition topicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata offsetAndMetadata) Overrides the fetch offsets that the consumer will use on the next poll.Due to internal buffering of messages, the record handler will continue to observe messages fetched with respect to the old offset until some time after the given
completionHandleris called. In contrast, the once the givencompletionHandleris called thebatchHandler(Handler)will only see messages consistent with the new offset.- Parameters:
topicPartition- topic partition for which seekoffsetAndMetadata- offset to seek inside the topic partition- Returns:
- a
Futurecompleted with the operation result
-
partitionsRevokedHandler
KafkaReadStream<K,V> partitionsRevokedHandler(Handler<Set<org.apache.kafka.common.TopicPartition>> handler) Set the handler called when topic partitions are revoked to the consumer- Parameters:
handler- handler called on revoked topic partitions- Returns:
- current KafkaReadStream instance
-
partitionsAssignedHandler
KafkaReadStream<K,V> partitionsAssignedHandler(Handler<Set<org.apache.kafka.common.TopicPartition>> handler) Set the handler called when topic partitions are assigned to the consumer- Parameters:
handler- handler called on assigned topic partitions- Returns:
- current KafkaReadStream instance
-
subscribe
Subscribe to the given list of topics to get dynamically assigned partitions.Due to internal buffering of messages, when changing the subscribed topics the old set of topics may remain in effect (as observed by the handler(Handler) record handler}) until some time after the given
completionHandleris called. In contrast, the once the givencompletionHandleris called thebatchHandler(Handler)will only see messages consistent with the new set of topics.- Parameters:
topics- topics to subscribe to- Returns:
- a
Futurecompleted with the operation result
-
subscribe
Subscribe to all topics matching specified pattern to get dynamically assigned partitions.Due to internal buffering of messages, when changing the subscribed topics the old set of topics may remain in effect (as observed by the handler(Handler) record handler}) until some time after the given
completionHandleris called. In contrast, the once the givencompletionHandleris called thebatchHandler(Handler)will only see messages consistent with the new set of topics.- Parameters:
pattern- Pattern to subscribe to- Returns:
- a
Futurecompleted with the operation result
-
unsubscribe
-
subscription
-
assign
Manually assign a set of partitions to this consumer.Due to internal buffering of messages, when reassigning the old set of partitions may remain in effect (as observed by the handler(Handler) record handler)} until some time after the given
completionHandleris called. In contrast, the once the givencompletionHandleris called thebatchHandler(Handler)will only see messages consistent with the new set of partitions.- Parameters:
partitions- partitions which want assigned- Returns:
- a future notified with the result
-
assignment
-
listTopics
-
commit
-
commit
Future<Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata>> commit(Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets) Commit the specified offsets for the specified list of topics and partitions to Kafka.- Parameters:
offsets- offsets list to commit- Returns:
- a
Futurecompleted with the operation result
-
partitionsFor
-
close
-
position
-
offsetsForTimes
Future<Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndTimestamp>> offsetsForTimes(Map<org.apache.kafka.common.TopicPartition, Long> topicPartitionTimestamps) Look up the offsets for the given partitions by timestamp.- Parameters:
topicPartitionTimestamps- A map with pairs of (TopicPartition, Timestamp).- Returns:
- a future notified on operation completed
-
offsetsForTimes
Future<org.apache.kafka.clients.consumer.OffsetAndTimestamp> offsetsForTimes(org.apache.kafka.common.TopicPartition topicPartition, long timestamp) * Look up the offset for the given partition by timestamp.- Parameters:
topicPartition- Partition to query.timestamp- Timestamp used to determine the offset.- Returns:
- a future notified on operation completed
-
beginningOffsets
Future<Map<org.apache.kafka.common.TopicPartition, Long>> beginningOffsets(Set<org.apache.kafka.common.TopicPartition> topicPartitions) Get the first offset for the given partitions.- Parameters:
topicPartitions- the partitions to get the earliest offsets.- Returns:
- a future notified on operation completed
-
beginningOffsets
-
endOffsets
Future<Map<org.apache.kafka.common.TopicPartition, Long>> endOffsets(Set<org.apache.kafka.common.TopicPartition> topicPartitions) Get the last offset for the given partitions. The last offset of a partition is the offset of the upcoming message, i.e. the offset of the last available message + 1.- Parameters:
topicPartitions- the partitions to get the end offsets.- Returns:
- a future notified on operation completed
-
endOffsets
Get the last offset for the given partition. The last offset of a partition is the offset of the upcoming message, i.e. the offset of the last available message + 1.- Parameters:
topicPartition- the partition to get the end offset.- Returns:
- a future notified on operation completed
-
unwrap
-
batchHandler
KafkaReadStream<K,V> batchHandler(Handler<org.apache.kafka.clients.consumer.ConsumerRecords<K, V>> handler) Set the handler that will be called when a new batch of records is returned from Kafka. Batch handlers need to take care not to block the event loop when dealing with large batches. It is better to process records individually using therecord handler.- Parameters:
handler- handler called each time Kafka returns a batch of records.- Returns:
- current KafkaReadStream instance.
-
pollTimeout
Sets the poll timeout for the underlying native Kafka Consumer. Defaults to 1000 ms. Setting timeout to a lower value results in a more 'responsive' client, because it will block for a shorter period if no data is available in the assigned partition and therefore allows subsequent actions to be executed with a shorter delay. At the same time, the client will poll more frequently and thus will potentially create a higher load on the Kafka Broker.- Parameters:
timeout- The time, 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 native Kafka consumer's buffer, else returns empty. Must not be negative.
-
poll
Executes a poll for getting messages from Kafka.- Parameters:
timeout- The maximum time to block (must not be greater thanLong.MAX_VALUEmilliseconds)- Returns:
- a future notified on operation completed
-