【问题标题】:Kafka Streams Materialized View with KotlinKafka 使用 Kotlin 流式传输物化视图
【发布时间】:2018-11-04 05:23:48
【问题描述】:

要在 Java 中创建一个 kafka 流状态存储,我可以这样做:

final KGroupedStream<String, String> wordCounts = textLines
            .flatMapValues(value -> Arrays.asList(pattern.split(value.toLowerCase())))
            .groupBy((key, word) -> word);

wordCounts.count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as(WORD_COUNT_STORE));

我正在尝试将其转换为 Kotlin,如下所示:

val wordCounts: KGroupedStream<String, String> = textLines
        .flatMapValues({value -> value.split("\\W+") })
        .groupBy({ _, word -> word})

wordCounts.count(Materialized.<String, Long, KeyValueStore<Bytes, Array<Byte>>>as(WORD_COUNT_STORE))

但是,我收到以下编译器错误:

Interface KeyValueStore does not have constructors

我需要做什么?

【问题讨论】:

标签: kotlin apache-kafka apache-kafka-streams


【解决方案1】:

如果它对其他人有用,以及 Raman 建议的反引号,我必须进行其他一些更改:

  • 首先,需要在as 方法之后指定泛型类型,而不是直接在Materialized 类之后。
  • 其次,我不得不使用ByteArray,而不是使用Array&lt;Byte&gt;。

所以对我有用的整行代码是:

wordCounts.count(Materialized.`as`<String, Long, KeyValueStore<Bytes, ByteArray>>(WORD_COUNT_STORE))

【讨论】:

    【解决方案2】:

    由于 as 是 Kotlin 中的保留字,请尝试用反引号将 as 括起来,即

    `as`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2016-06-04
      相关资源
      最近更新 更多