【问题标题】:Where exactly is the default partition decided in kafka producer?kafka producer 中的默认分区到底在哪里?
【发布时间】:2018-05-11 11:38:13
【问题描述】:

我正在尝试通过源代码进行 grep,但我能找到的只是

/Users/myUser/.m2/repository/org/apache/kafka/kafka_2.10/0.9.0.2/kafka_2.10-0.9.0.2.jar!/kafka/javaapi/producer/Producer.class

public void send(List<KeyedMessage<K, V>> messages) {
   this.underlying().send(scala.collection.JavaConversions..MODULE$.asScalaBuffer(messages).toSeq());
}

哪个调用

/Users/myUser/.m2/repository/org/apache/kafka/kafka_2.10/0.9.0.2/kafka_2.10-0.9.0.2.jar!/kafka/producer/Producer.class

public void send(Seq<KeyedMessage<K, V>> messages) {
    synchronized(this.lock()) {
        if(this.hasShutdown().get()) {
            throw new ProducerClosedException();
        } else {
            this.recordStats(messages);
            boolean var3 = this.sync();
            BoxedUnit var4;
            if(var3) {
                this.eventHandler().handle(messages);
                var4 = BoxedUnit.UNIT;
            } else {
                if(var3) {
                    throw new MatchError(BoxesRunTime.boxToBoolean(var3));
                }

                this.asyncSend(messages);
                var4 = BoxedUnit.UNIT;
            }

            BoxedUnit var10000 = BoxedUnit.UNIT;
        }
    }
}

这很令人困惑,因为我希望 send 函数都接受来自 default partition 的分区 id,比如

public void send(List<KeyedMessage<K, V>> messages, int partition id)

或者以某种方式调用默认分区来获取send方法定义中的分区id

但我没有看到在任何地方使用的分区 ID。

有人可以指点我在哪里寻找分区决定的方向吗?

是在我们打电话给producer.send之前吗?

【问题讨论】:

  • Partitioner 是在 send 方法中决定的,但是您正在查看旧的 Producer API,而不是新的(Kafka 0.10.x)
  • asyncSend 是您应该查找此特定代码的位置
  • @cricket_007 不幸的是,我们使用的是 kafka 版本&lt;kafka.version&gt;0.9.0.2&lt;/kafka.version&gt;
  • 更新了我的答案

标签: java apache-kafka


【解决方案1】:

您正在查看旧 API,该逻辑是 in the EventHandler,即 called by the send method


对于新客户,you can see it in the doSend method

 int partition = partition(record, serializedKey, serializedValue, cluster);
 tp = new TopicPartition(record.topic(), partition);

The partitioner class is loaded from the properties given to the Producer。如果未定义,则为默认值

【讨论】:

    猜你喜欢
    • 2022-01-15
    • 1970-01-01
    • 2020-01-27
    • 2020-06-25
    • 2013-02-03
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多