Last active
May 30, 2017 12:00
-
-
Save natewave/9b9ce4c288ecb6615a99c6da74367bad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getCommittedOffsetsForConsumer(consumerConfig: KafkaConsumerConfig, topicPartitions: Seq[TopicPartition]): Map[TopicPartition, Long] = { | |
val result = withCustomStringConsumer(consumer => { | |
consumer.assign(topicPartitions.toList.asJava) | |
val tps = topicPartitions.map( tp => { | |
val offsetAndMetadata = consumer.committed(tp) | |
val startOffset: Long = KafkaCluster.getLogBeginningOffset(consumerConfig.topic).values.head | |
Option(offsetAndMetadata) match { | |
case None => { | |
// log "TopicPartition $tp doesn't have committed offsets" | |
tp -> startOffset | |
} | |
case Some(om) => { | |
val currentOffsets = om.offset() | |
if (currentOffsets < startOffset) { | |
tp -> startOffset | |
} else { | |
tp -> currentOffsets | |
} | |
} | |
} | |
} | |
).toMap | |
consumer.close() | |
tps | |
})(consumerConfig) | |
result.asInstanceOf[Map[TopicPartition, Long]] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment