重载方法值 table 与备选方案

Overloaded method value table with alternatives

我有以下编译器抱怨的代码:

val state: KTable[String, String]  = builder
  .table("BARY-PATH", Materialized.as("PATH-STORE"))

错误信息:

[error] /home/developer/Desktop/microservices/paths-stream/src/main/scala/io/khinkali/PathsTopology.scala:23:8: overloaded method value table with alternatives:
[error]   [K, V](x: String, x: org.apache.kafka.streams.kstream.Materialized[K,V,org.apache.kafka.streams.state.KeyValueStore[org.apache.kafka.common.utils.Bytes,Array[Byte]]])org.apache.kafka.streams.kstream.KTable[K,V] <and>
[error]   [K, V](x: String, x: org.apache.kafka.streams.Consumed[K,V])org.apache.kafka.streams.kstream.KTable[K,V]
[error]  cannot be applied to (String, org.apache.kafka.streams.kstream.Materialized[Nothing,Nothing,Nothing])
[error]       .table("BARY-PATH", Materialized.as("PATH-STORE"))
[error]        ^

然后我尝试了:

val state: KTable[String, String]  = builder
  .table[String, String]("BARY-PATH", Materialized[String, String,KeyValueStore[org.apache.kafka.common.utils.Bytes, Array[Byte]]].as("PATH-STORE"))

编译器仍然报错:

[error] /home/developer/Desktop/microservices/paths-stream/src/main/scala/io/khinkali/PathsTopology.scala:24:43: object org.apache.kafka.streams.kstream.Materialized is not a value
[error]       .table[String, String]("BARY-PATH", Materialized[String, String,KeyValueStore[org.apache.kafka.common.utils.Bytes, Array[Byte]]].as("PATH-STORE"))

我读了 API docs 但想不通,我做错了什么?

方法实现:

 * Materialize a {@link StateStore} with the given name.
 *
 * @param storeName  the name of the underlying {@link KTable} state store; valid characters are ASCII
 * alphanumerics, '.', '_' and '-'.
 * @param <K>       key type of the store
 * @param <V>       value type of the store
 * @param <S>       type of the {@link StateStore}
 * @return a new {@link Materialized} instance with the given storeName
 */
public static <K, V, S extends StateStore> Materialized<K, V, S> as(final String storeName) {
    Topic.validate(storeName);
    return new Materialized<>(storeName);
} 

在Java,我做了

KTable<String, String> soureTable = builder
    .table("BARY-PATH", Materialized.as("PATH-STORE"));

它就像一个魅力。

尝试在 as 方法之后移动通用类型规范:

val state: KTable[String, String]  = builder
    .table[String, String]("BARY-PATH", Materialized.as[String, String,KeyValueStore[org.apache.kafka.common.utils.Bytes, Array[Byte]]]("PATH-STORE"))

正如您从 Java 签名中看到的那样,对于静态方法,您应该为方法指定泛型类型而不是 class.