Skip to content

Instantly share code, notes, and snippets.

@nilsmagnus
Created February 15, 2017 09:11
Show Gist options
  • Save nilsmagnus/809c56201a9e17ee405c1255f0b71aca to your computer and use it in GitHub Desktop.
Save nilsmagnus/809c56201a9e17ee405c1255f0b71aca to your computer and use it in GitHub Desktop.
Get newest offsets of all topics for your client, using the sarama/shopify library.
package main
import (
"github.com/Shopify/sarama"
"log"
"time"
"fmt"
"strings"
)
func main() {
config := sarama.NewConfig()
config.ClientID = "your-client-id"
config.Consumer.Return.Errors = true
brokers := []string{"localhost:9092"}
client, clienterr := sarama.NewClient(brokers, config)
if clienterr != nil {
log.Fatal(clienterr)
}
defer client.Close()
topics, topicerr := client.Topics()
if topicerr != nil {
log.Fatal(topicerr)
}
for _, topic := range topics {
if strings.Contains(topic, "__consumer_offsets") {
continue
}
partitions, partitionerr := client.Partitions(topic)
if partitionerr != nil {
log.Fatal(partitionerr)
}
for _, partition := range partitions {
offset, offseterr := client.GetOffset(topic, partition, sarama.OffsetNewest)
if offseterr != nil {
log.Fatal(offseterr)
} else {
fmt.Printf("Offset for topic %s at %v on partition %d is %d\n", topic, time.Now(), partition, offset)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment