本文整理匯總了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
}