dthh5038 2019-08-22 13:09
浏览 124

我如何知道要在Kafka中使用哪个分区?

I have one topic with 3 partitions in Kafka broker 1 partitions have message and 2 partitions are empty, how do I know which partition to consume in one call?

First I assign a TopicPartition with partition equals to kafka.PartitionAny, but this value keeps returning -1

So I have to manually use a counter, and when I successfully consume from one partition but with null message, then count++ and start with next one, until I find the message


for{

    partitions = append(partitions, kafka.TopicPartition{
        Topic:     &topic,
        Partition: partition,
        Offset:    offSet,
        Error:     err,
    })


    err = c.Assign(partitions)
    if err != nil {
         return err
    }

    // retrieve message
    ev, err := c.Poll(-1)
    if err != nil {
         return err
    }

    // if no message, check the next partition
    if ev == nil{
         partition++
    }else{
         break
    }

}

The first two round does not return any message, but it has to wait for the third round to return, is there any way it can automatically detect which partition is stored with un-consumed message?

If there is no other way, can Kafka do the round-robin routing for me? or I have to record the counter myself

Thanks! :)

  • 写回答

1条回答 默认 最新

  • dqm83011 2019-08-22 19:20
    关注

    You should definitely use subscribe() method and then call poll(). If there are available records, you'll get a Records answer containing one or multiple records ( associated with different partitions).

    After processing a record, you can then commit manually ( if you use enable.auto.commit = false) using the medata contained in the record ( topic, partitions, offset, etc..).

    Yannick

    评论

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab