golang连接kafka的小例子-创新互联
package main
import (
"fmt"
"github.com/Shopify/sarama"
)
// kafka消费小
func main() {consumer, err := sarama.NewConsumer([]string{"127.0.0.1:9092"}, nil)
if err != nil {fmt.Println("failed to start consumer err ", err)
return
}
partitionList, err := consumer.Partitions("web_log") //根据topic取到所有的分区
if err != nil {fmt.Println("fail to get list of partition:err", err)
return
}
fmt.Println(partitionList)
// 遍历所有分区
for partition := range partitionList {pc, err := consumer.ConsumePartition("web_log", int32(partition), sarama.OffsetNewest)
if err != nil { fmt.Println("failed to start consumer for partition ", partition, err)
return
}
defer pc.AsyncClose()
// 异步从每个分区消费信息
go func(sarama.Partitioner) { for msg := range pc.Messages() { fmt.Println("Partition ", msg.Partition, msg.Offset, msg.Key, msg.Value)
}
}(pc)
}
}
package main
import (
"fmt"
"github.com/Shopify/sarama"
)
// kafka消费小
func main() {consumer, err := sarama.NewConsumer([]string{"127.0.0.1:9092"}, nil)
if err != nil {fmt.Println("failed to start consumer err ", err)
return
}
partitionList, err := consumer.Partitions("web_log") //根据topic取到所有的分区
if err != nil {fmt.Println("fail to get list of partition:err", err)
return
}
fmt.Println(partitionList)
// 遍历所有分区
for partition := range partitionList {pc, err := consumer.ConsumePartition("web_log", int32(partition), sarama.OffsetNewest)
if err != nil { fmt.Println("failed to start consumer for partition ", partition, err)
return
}
defer pc.AsyncClose()
// 异步从每个分区消费信息
go func(sarama.PartitionConsumer) { for msg := range pc.Messages() { fmt.Println("Partition ", msg.Partition, msg.Offset, msg.Key, msg.Value)
}
}(pc)
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
创新互联建站专注于益阳企业网站建设,成都响应式网站建设公司,商城网站建设。益阳网站建设公司,为益阳等地区提供建站服务。全流程按需设计,专业设计,全程项目跟踪,创新互联建站专业和态度为您提供的服务本文标题:golang连接kafka的小例子-创新互联
网站地址:http://ybzwz.com/article/dhijic.html