Consumer committableSource和plainSource有什么区别?

时间:2019-04-14 15:07:28

标签: scala apache-kafka alpakka

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

 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()

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

1 个答案:

答案 0 :(得分:1)

使用Committer.sink代替Committer.flow来终止流,而使用{{1}}可以继续流,直到选择使用其他接收器终止流为止。