add topic-offset to simple consumer: can be used to determine earliest offset available too

This commit is contained in:
Paul Ingles 2014-05-01 15:23:56 +01:00
parent 5b7afa5723
commit ddb0a77391
2 changed files with 8 additions and 4 deletions

View file

@ -1,4 +1,4 @@
(defproject clj-kafka/clj-kafka "0.2.2-0.8"
(defproject clj-kafka/clj-kafka "0.2.3-0.8"
:min-lein-version "2.0.0"
:dependencies [[org.clojure/clojure "1.5.1"]
[zookeeper-clj "0.9.3"]

View file

@ -31,9 +31,13 @@
(defn topic-meta-data [consumer topics]
(to-clojure (.send consumer (TopicMetadataRequest. topics))))
(defn latest-topic-offset [consumer topic partition]
(let [tp (TopicAndPartition. topic partition)
pori (PartitionOffsetRequestInfo. -1 1)
(defn topic-offset [consumer topic partition offset-position]
(let [op {:latest -1 :earliest -2}
tp (TopicAndPartition. topic partition)
pori (PartitionOffsetRequestInfo. (offset-position op) 1)
hm (java.util.HashMap. {tp pori})]
(let [response (.getOffsetsBefore consumer (OffsetRequest. hm (kafka.api.OffsetRequest/CurrentVersion) "clj-kafka-id"))]
(first (.offsets response topic partition)))))
(defn latest-topic-offset [consumer topic partition]
(topic-offset consumer topic partition :latest))