本文整理汇总了Golang中github.com/Shopify/sarama.Consumer.Partitions方法的典型用法代码示例。如果您正苦于以下问题:Golang Consumer.Partitions方法的具体用法?Golang Consumer.Partitions怎么用?Golang Consumer.Partitions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/Shopify/sarama.Consumer
的用法示例。
在下文中一共展示了Consumer.Partitions方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getPartitions
func getPartitions(c sarama.Consumer) ([]int32, error) {
if *partitions == "all" {
return c.Partitions(*topic)
}
tmp := strings.Split(*partitions, ",")
var pList []int32
for i := range tmp {
val, err := strconv.ParseInt(tmp[i], 10, 32)
if err != nil {
return nil, err
}
pList = append(pList, int32(val))
}
return pList, nil
}
示例2: GetKafkaPartitions
// GetKafkaPartitions is a helper function to look up which partitions are available
// via the given brokers for the given topic. This should be called only on startup.
func GetKafkaPartitions(brokerHosts []string, topic string) (partitions []int32, err error) {
if len(brokerHosts) == 0 {
return partitions, errors.New("at least 1 broker host is required")
}
if len(topic) == 0 {
return partitions, errors.New("topic name is required")
}
var cnsmr sarama.Consumer
cnsmr, err = sarama.NewConsumer(brokerHosts, sarama.NewConfig())
if err != nil {
return partitions, err
}
defer func() {
if cerr := cnsmr.Close(); cerr != nil && err == nil {
err = cerr
}
}()
return cnsmr.Partitions(topic)
}
示例3: findPartitions
func findPartitions(consumer sarama.Consumer, config consumeConfig) []int32 {
allPartitions, err := consumer.Partitions(config.topic)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read partitions for topic %v err=%v\n", config.topic, err)
os.Exit(1)
}
_, hasDefaultOffset := config.offsets[-1]
partitions := []int32{}
if !hasDefaultOffset {
for _, p := range allPartitions {
_, ok := config.offsets[p]
if ok {
partitions = append(partitions, p)
}
}
} else {
partitions = allPartitions
}
return partitions
}