Kafka生产商如何批量生产?

时间:2017-04-11 15:07:16

标签: apache-kafka kafka-producer-api

我们说我有两个经纪人。

我读到KafkaProducer创建的生产者线程等于经纪人的数量。所以在这种情况下我会有两个内部线程。

我们说我有5个主题,而我每秒只收到200条消息。 kafka如何进行批处理?

batch.size = 30条消息。 [topic1 = 5,topic2 = 10,topic3 = 3,topic4 = 10,topic5 = 2条消息]这些是最高订单消息和相应的主题。

kafka如何进行批处理?

2 个答案:

答案 0 :(得分:2)

  

我读到KafkaProducer创建的生产者线程等于经纪人的数量。所以在这种情况下我会有两个内部线程。

不确定,您从何处获取此信息,但这不正确。 KafkaProducer确实有一个后台线程可以将数据异步写入代理。

批处理如何发生,很难详细预测。这取决于你的batch.size(这是一个最大值)。此外,还有linger.ms参数,用于定义在发送数据之前保留数据的时间(即使批次未满)。

更详细地说,将为您托管分区的所有代理提供开放式网络连接。此外,批处理基于分区发生 - 但是,可以将多个批处理包含在对代理的单个请求中。

答案 1 :(得分:1)

Record Accumulator负责批量发往Kafka经纪商的价值。

- maintains a ConcurrentMap<TopicPartition, Deque<RecordBatch>>
- gets Deque of relevant TopicPartition
- gets last RecordBatch present in Deque, if RecordBatch(bytebuffer bounded by batch.size) is not full appends value to the RecordBatch
- if last RecordBatch is null, no RecordBatch exists for the relevant topic partition hence allocates a new byte buffer
- does a double check locking on last RecordBatch again, incase some other thread might have created the RecordBatch
- if RecordBatch exists, tries appending the value
- if still RecordBatch is null, creates MemoryRecords (backed by byte buffer)
- adds MemoryRecords to RecordBatch
- appends value to RecordBatch ( inside MemoryRecords eventually Byte Buffer )
- adds RecordBatch to Deque
  

类层次结构:

RecordBatch
 - MemoryRecords
   - ByteBuffer