What is the passthrough used for in alpakka-kafka connector while producing messages?

孤街浪徒 提交于 2019-12-13 02:55:53

问题


This is the code for producing a single message to Kafka given in the doc https://doc.akka.io/docs/alpakka-kafka/current/producer.html

val single: ProducerMessage.Envelope[KeyType, ValueType, PassThroughType] =
  ProducerMessage.single(
    new ProducerRecord("topicName", key, value),
    passThrough
  )

Could you please explain what is passThrough used for?


回答1:


passThrough is an additional value which comes to be available for use in ProducerMessage.Results’s passThrough().

As the official documentation states, the best and the most widely used practice is to use this value as a transport to passing the CommittableOffset to a downstream Committer.Sink.

This seems convenient in cases when you are using a CommittableSource along with the enable.auto.commit Kafka consumer option set to false. In such cases you have to commit Kafka offsets manually as you processing them. I in particular use such approach when reading records from one Kafka topic, processing them and then sending them to another Kafka topic.

Here is a simple use case:

Consumer
      .committableSource(consumerSettings, Subscriptions.topics(topicIn))
      .mapAsync { msg ⇒
        process(msg.record.value).map {
          response ⇒
            ProducerMessage.single(
              new ProducerRecord(topicOut, msg.record.key, response),
              passThrough = msg.committableOffset
            )
        }
      }
      .via(Producer.flexiFlow(producerSettings))
      .map(_.passThrough) // extract the passThrough
      .toMat(Committer.sink(committerSettings))(Keep.both) // commiting offsets
      .mapMaterializedValue(DrainingControl.apply)
      .run()


来源:https://stackoverflow.com/questions/58577149/what-is-the-passthrough-used-for-in-alpakka-kafka-connector-while-producing-mess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!