You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
goKafka/consume-partition.go

66 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"github.com/Shopify/sarama"
"log"
"os"
"os/signal"
)
func main() {
// 一,获取消费者
consumer, err := sarama.NewConsumer([]string{"localhost:9092"}, nil)
if err != nil {
log.Fatalln(err)
}
defer func() {
if err := consumer.Close(); err != nil {
log.Fatalln(err)
}
}()
// 二获取topic下的分区列表
topic := "topic_more_partition_1" // 3
partitions, err := consumer.Partitions(topic)
if err != nil {
log.Fatalln(err)
}
log.Println(partitions)
// 三,遍历每个分区
var consumeCounter = 0
for _, partition := range partitions {
// 获取每个分区的消费者
partitionConsumer, err := consumer.ConsumePartition(topic, partition, sarama.OffsetNewest)
if err != nil {
log.Fatalln(err)
}
// 四,利用 goroutine 完成消费
go func(pc sarama.PartitionConsumer) {
// 关闭
defer func() {
if err := pc.Close(); err != nil {
log.Fatalln(err)
}
}()
// 消费
for msg := range pc.Messages() {
log.Println(pc)
log.Printf("Consumed message, partition: %d, offset: %d, topic: %s, value: %s\n", msg.Partition, msg.Offset, msg.Topic, msg.Value)
consumeCounter++
}
}(partitionConsumer)
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
select {
case <-signals:
}
log.Printf("Consumed: %d\n", consumeCounter)
}