Search in sources :

Example 11 with FetchResponse

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);
}
Also used : FetchRequestBuilder(kafka.api.FetchRequestBuilder) FetchRequest(kafka.api.FetchRequest) FetchResponse(kafka.javaapi.FetchResponse)

Aggregations

FetchResponse (kafka.javaapi.FetchResponse)11 FetchRequest (kafka.api.FetchRequest)9 FetchRequestBuilder (kafka.api.FetchRequestBuilder)9 IOException (java.io.IOException)5 SimpleConsumer (kafka.javaapi.consumer.SimpleConsumer)5 ByteBufferMessageSet (kafka.javaapi.message.ByteBufferMessageSet)4 ConnectException (java.net.ConnectException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)3 MessageAndOffset (kafka.message.MessageAndOffset)3 ByteBuffer (java.nio.ByteBuffer)2 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 OffsetRequest (kafka.api.OffsetRequest)1 Broker (kafka.cluster.Broker)1 KafkaException (kafka.common.KafkaException)1 OffsetOutOfRangeException (kafka.common.OffsetOutOfRangeException)1