【问题标题】:Replacing RocksDB with In-memory state store in Kafka Streams用 Kafka Streams 中的内存状态存储替换 RocksDB
【发布时间】:2017-06-08 00:56:07
【问题描述】:

我正在使用 Kafka Streams 0.10.1.1 版本。

状态存储的 RocksDB 实现无法处理我们的 50k/msg 速率,因此我想将状态存储更改为内存中的存储。根据文档,这应该是可能的:http://docs.confluent.io/3.1.0/streams/architecture.html#state

但是,当我实现这个时:

val stateStore = Stores.create(stateStoreName).withStringKeys().withStringKeys().inMemory().build()

val procSuppl: KStreamAggregate = ... // I'll spare the implementation details

streamBuilder.addSource(
  "mysource",
  new StringDeserializer(),
  new StringDeserializer(),
  "input_topic"
).addProcessor("proc", procSuppl,  "mysource").addStateStore(stateStore, "proc")

我在运行时遇到了这个错误:

Caused by: java.lang.ClassCastException: org.apache.kafka.streams.state.internals.MeteredKeyValueStore cannot be cast to org.apache.kafka.streams.state.internals.CachedStateStore
2017-01-23T13:19:11.830674020Z  at org.apache.kafka.streams.kstream.internals.KStreamAggregate$KStreamAggregateProcessor.init(KStreamAggregate.java:62)

上述方法的实现是:

public void init(ProcessorContext context) {
        super.init(context);
        store = (KeyValueStore<K, T>) context.getStateStore(storeName);
        ((CachedStateStore) store).setFlushListener(new ForwardingCacheFlushListener<K, V>(context, sendOldValues));
    }

为什么它试图将状态存储转换为 CachedStateStore 实例?如何实现一个简单的内存状态存储,根据文档应该可以实现?

谢谢

【问题讨论】:

  • 顺便说一句:KStreamAggregate 是一个内部类,使用风险自负。

标签: apache-kafka apache-kafka-streams rocksdb


【解决方案1】:

为了创建内存状态存储,需要创建一个存储供应商(使用Stores工厂对象):

val storeSupplier = Stores.inMemoryKeyValueStore("in-mem")

那么在实现KTable的时候需要用到store供应商:

val wordCounts =  builder
  .stream[String, String]("streams-plaintext-input")
  .flatMapValues(textLine => textLine.toLowerCase.split("\\W+"))
  .groupBy((_, word) => word)
  .count()(Materialized.as(storeSupplier))

获取可查询的存储:

val qStore = streams.store(
  wordCounts.queryableStoreName,
  QueryableStoreTypes.keyValueStore[String, Long])

【讨论】:

    猜你喜欢
    • 2022-10-06
    • 2023-03-23
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 2018-10-24
    • 2021-01-04
    相关资源
    最近更新 更多