consumer committablesource和plainsource之间有什么区别?

798qvoo8  于 2021-06-06  发布在  Kafka
关注(0)|答案(1)|浏览(400)

我正在尝试使用消费者图书馆https://doc.akka.io/docs/alpakka-kafka/current/consumer.html 方法 committableSource 具体如下:

Consumer
    .committableSource(consumerSettings, Subscriptions.topics("SAP-EVENT-BUS"))
    .map(_.committableOffset)
    .toMat(Committer.sink(committerSettings))(Keep.both)
    .mapMaterializedValue(DrainingControl.apply)
    .run()

这里的问题是,如何获得消费者从中收到的消息 Kafka ?
与以下代码段一起工作:

Consumer
    .plainSource(
      consumerSettings,
      Subscriptions.topics("SAP-EVENT-BUS"))
    .to(Sink.foreach(println))
    .run()

整个代码片段:

private implicit val materializer = ActorMaterializer()
  private val config = context.system.settings.config.getConfig("akka.kafka.consumer")
  private val consumerSettings =
    ConsumerSettings(config, new StringDeserializer, new StringDeserializer)
      .withBootstrapServers("localhost:9092")
      .withGroupId("SAP-SENDER-GROUP")
      .withProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest")

  private val committerSettings = CommitterSettings(context.system)

  Consumer
    .committableSource(consumerSettings, Subscriptions.topics("TOPIC"))
    .map(_.committableOffset)
    .toMat(Committer.sink(committerSettings))(Keep.both)
    .mapMaterializedValue(DrainingControl.apply)
    .run()

  Consumer
    .plainSource(
      consumerSettings,
      Subscriptions.topics("SAP-EVENT-BUS"))
    .to(Sink.foreach(println))
    .run()

或者我必须同时使用两者,一个用于提交,另一个用于消费。

stszievb

stszievb1#

而不是 Committer.sink ,终止流,使用 Committer.flow 它允许您继续流,直到您选择用另一个接收器终止它。

相关问题