overloaded method value aggregate with alternatives

廉价感情. 提交于 2021-02-10 18:42:56

问题


I have following function, that does not compile:

  private def save(pea: KStream[String, String]): Unit = {
    pea
      .groupByKey()
      .aggregate(() => """{folder: ""}""",
        (_: String, _: String, value: String) => value,
        EventStoreTopology.Store)
  }

the error message is:

[error]   [VR](x$1: org.apache.kafka.streams.kstream.Initializer[VR], x$2: org.apache.kafka.streams.kstream.Aggregator[_ >: String, _ >: String, VR], x$3: org.apache.kafka.streams.processor.StateStoreSupplier[org.apache.kafka.streams.state.KeyValueStore[_, _]])org.apache.kafka.streams.kstream.KTable[String,VR] <and>
[error]   [VR](x$1: org.apache.kafka.streams.kstream.Initializer[VR], x$2: org.apache.kafka.streams.kstream.Aggregator[_ >: String, _ >: String, VR], x$3: org.apache.kafka.common.serialization.Serde[VR])org.apache.kafka.streams.kstream.KTable[String,VR] <and>
[error]   [VR](x$1: org.apache.kafka.streams.kstream.Initializer[VR], x$2: org.apache.kafka.streams.kstream.Aggregator[_ >: String, _ >: String, VR], x$3: org.apache.kafka.streams.kstream.Materialized[String,VR,org.apache.kafka.streams.state.KeyValueStore[org.apache.kafka.common.utils.Bytes,Array[Byte]]])org.apache.kafka.streams.kstream.KTable[String,VR]
[error]  cannot be applied to (() => String, (String, String, String) => String, io.khinkali.eventstore.EventStoreTopology.Persistent)
[error]       .aggregate(() => """{folder: ""}""",
[error]        ^
[error] one error found
[error] (eventstore/compile:compileIncremental) Compilation failed 

the signature of aggregate is:

<VR> KTable<K, VR> aggregate(final Initializer<VR> initializer,
                             final Aggregator<? super K, ? super V, VR> aggregator,
                             final Materialized<K, VR, KeyValueStore<Bytes, byte[]>> materialized);

And the EventStoreTopology.Store is defined as:

object EventStoreTopology {

  type Persistent = Materialized[String, String, KeyValueStore[Bytes, Array[Byte]]]
  val StoreName: String = "EventStore"
  val Store: Persistent = Materialized.as(StoreName)

}

What am I doing wrong?


回答1:


The compiler need some help to infer the correct type for the aggregator parameter.

To make it compile you can try:

val store: Materialized[String, String, KeyValueStore[Bytes, Array[Byte]]] = ???

private def save(pea: KStream[String, String]): Unit = {
  val aggregator: Aggregator[String, String, String] = (_, _, value: String) => value
  pea
    .groupByKey()
    .aggregate(() => """{folder: ""}""",
      aggregator,
      store)
}


来源:https://stackoverflow.com/questions/48145527/overloaded-method-value-aggregate-with-alternatives

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