Search in sources :

Example 11 with PartitionOffsetRequestInfo

use of kafka.api.PartitionOffsetRequestInfo in project cdap by caskdata.

the class KafkaUtil method getOffsetByTimestamp.

/**
   * Fetch the starting offset of the last segment whose latest message is published before the given timestamp.
   * The timestamp can also be special value {@link OffsetRequest$#EarliestTime()}
   * or {@link OffsetRequest$#LatestTime()}.
   *
   * @param consumer the consumer to send request to and receive response from
   * @param topic the topic for fetching the offset from
   * @param partition the partition for fetching the offset from
   * @param timestamp the timestamp to use for fetching last offset before it
   * @return the latest offset
   *
   * @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 long getOffsetByTimestamp(SimpleConsumer consumer, String topic, int partition, long timestamp) throws KafkaException {
    // Fire offset request
    OffsetRequest request = new OffsetRequest(ImmutableMap.of(new TopicAndPartition(topic, partition), new PartitionOffsetRequestInfo(timestamp, 1)), kafka.api.OffsetRequest.CurrentVersion(), consumer.clientId());
    OffsetResponse response = consumer.getOffsetsBefore(request);
    if (response.hasError()) {
        throw Errors.forCode(response.errorCode(topic, partition)).exception();
    }
    // Retrieve offsets from response
    long[] offsets = response.offsets(topic, partition);
    if (offsets.length == 0) {
        if (timestamp != kafka.api.OffsetRequest.EarliestTime()) {
            // Hence, use the earliest time to find out the offset
            return getOffsetByTimestamp(consumer, topic, partition, kafka.api.OffsetRequest.EarliestTime());
        }
        // This shouldn't happen. The find earliest offset response should return at least one offset.
        throw new UnknownServerException("Empty offsets received from offsets request on " + topic + ":" + partition + " from broker " + consumer.host() + ":" + consumer.port());
    }
    LOG.debug("Offset {} fetched for {}:{} with timestamp {}.", offsets[0], topic, partition, timestamp);
    return offsets[0];
}
Also used : OffsetResponse(kafka.javaapi.OffsetResponse) PartitionOffsetRequestInfo(kafka.api.PartitionOffsetRequestInfo) TopicAndPartition(kafka.common.TopicAndPartition) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) OffsetRequest(kafka.javaapi.OffsetRequest)

Aggregations

PartitionOffsetRequestInfo (kafka.api.PartitionOffsetRequestInfo)11 TopicAndPartition (kafka.common.TopicAndPartition)11 OffsetRequest (kafka.javaapi.OffsetRequest)8 HashMap (java.util.HashMap)7 OffsetResponse (kafka.javaapi.OffsetResponse)6 SimpleConsumer (kafka.javaapi.consumer.SimpleConsumer)3 PrestoException (com.facebook.presto.spi.PrestoException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 OffsetRequest (kafka.api.OffsetRequest)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1 UnknownServerException (org.apache.kafka.common.errors.UnknownServerException)1