我有编译器抱怨的以下代码:
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$1: String, x$2: 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$1: String, x$2: 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 https://kafka.apache.org/10/javadoc/index.html?org/apache/kafka/streams/KafkaStreams.html但无法弄清楚,我做错了什么?
方法实现:
* 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"));
它就像一个魅力。