use of kafka.javaapi.FetchResponse in project cdap by caskdata.
the class KafkaUtil method fetchMessages.
/**
* Fetches messages from the given topic, partition and offset using the provided {@link SimpleConsumer}.
*
* @return A {@link ByteBufferMessageSet} of the given topic, partition for messages fetched
*
* @throws OffsetOutOfRangeException if the given offset is out of range.
* @throws NotLeaderForPartitionException if the broker that the consumer is talking to is not the leader
* for the given topic and partition.
* @throws UnknownTopicOrPartitionException if the topic or partition is not known by the Kafka server
* @throws UnknownServerException if the Kafka server responded with error.
*/
public static ByteBufferMessageSet fetchMessages(SimpleConsumer consumer, String topic, int partition, int fetchSize, long requestOffset) throws KafkaException {
FetchRequest req = new FetchRequestBuilder().clientId(consumer.clientId()).addFetch(topic, partition, requestOffset, fetchSize).build();
FetchResponse fetchResponse = consumer.fetch(req);
if (fetchResponse.hasError()) {
throw Errors.forCode(fetchResponse.errorCode(topic, partition)).exception();
}
return fetchResponse.messageSet(topic, partition);
}
Aggregations