线程"main"中的异常org.apache.kafka.streams.errors.InvalidStateStoreException: [英] Exception in thread "main" org.apache.kafka.streams.errors.InvalidStateStoreException:

查看:99
本文介绍了线程"main"中的异常org.apache.kafka.streams.errors.InvalidStateStoreException:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问在同一Java程序中创建的inMemoryStore.但是返回一个异常线程主要"中的异常org.apache.kafka.streams.errors.InvalidStateStoreException:状态存储storeName可能已迁移到另一个实例."

I am trying to access the inMemoryStore that I am creating with in the same java program. But returning a exception as "Exception in thread "main" org.apache.kafka.streams.errors.InvalidStateStoreException: The state store, storeName, may have migrated to another instance."

当我使用persistentKeyValueStore时,它可以正常工作,并且能够创建存储并返回值.

When I am using the persistentKeyValueStore it is working fine and able to create the store and return the values.

package com.bakdata.streams_store.demo;

import java.util.Collection;
import java.util.Properties;

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
import org.apache.kafka.streams.state.KeyValueStore;
import org.apache.kafka.streams.state.QueryableStoreTypes;
import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
import org.apache.kafka.streams.state.StoreBuilder;
import org.apache.kafka.stre7ams.state.Stores;
import org.apache.kafka.streams.state.StreamsMetadata;

public class InMemoryStore {

public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, "stream-id-0001");
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

    String storeName = "sample";
    KeyValueBytesStoreSupplier stateStore = Stores.inMemoryKeyValueStore(storeName);
    StoreBuilder<KeyValueStore<String, String>> storeBuilder = Stores.keyValueStoreBuilder(stateStore, Serdes.String(), Serdes.String());

    StreamsBuilder builder = new StreamsBuilder();
    builder.addStateStore(storeBuilder);
    KStream<String, String> inputStream = builder.stream("material_test1");
    KafkaStreams streams = new KafkaStreams(builder.build(), props);

    try {
        streams.start();
        Thread.sleep(30000);
    } catch (final Throwable e) {
        System.exit(1);
    }
    final ReadOnlyKeyValueStore<String, String> keyValueStore = streams.store(storeName, QueryableStoreTypes.keyValueStore());
    KeyValueIterator<String, String> range = keyValueStore.all();
    while (range.hasNext()) {
        KeyValue<String, String> next = range.next();
        System.out.println("Key: " + next.key + ", value: " + next.value);
    }
}
}

线程主"中的异常org.apache.kafka.streams.errors.InvalidStateStoreException:状态存储,样本可能已迁移到另一个实例.在org.apache.kafka.streams.state.internals.QueryableStoreProvider.getStore(QueryableStoreProvider.java:62)在org.apache.kafka.streams.KafkaStreams.store(KafkaStreams.java:1067)在com.bakdata.streams_store.demo.InMemoryStore.main(InMemoryStore.java:59)

Exception in thread "main" org.apache.kafka.streams.errors.InvalidStateStoreException: The state store, sample, may have migrated to another instance. at org.apache.kafka.streams.state.internals.QueryableStoreProvider.getStore(QueryableStoreProvider.java:62) at org.apache.kafka.streams.KafkaStreams.store(KafkaStreams.java:1067) at com.bakdata.streams_store.demo.InMemoryStore.main(InMemoryStore.java:59)

我希望从ReadOnlyStoreQuery打印这些值.

I am expecting to print the values from the ReadOnlyStoreQuery.

推荐答案

您不能在流上拥有StateStore,因为单个键可能有多个值.您需要先将其转换为KTable( streams.table(...))或GlobalKtable( streams.globalTable(...)).

You cannot have a StateStore on a stream as there could be multiple values for a single key. You need to turn it into a KTable (streams.table(...)) or GlobalKtable (streams.globalTable(...)) first.

科特琳(Kotlin)示例:

Kotlin example:

val businessObjects = streamsBuilder.globalTable("topic", eventStore("store-name"))

其中 eventStore 是:

fun eventStore(name: String) = Materialized.`as`<String, String>(Stores.inMemoryKeyValueStore(name))
    .withKeySerde(Serdes.String())
    .withValueSerde(Serdes.String())

启动流后:

var store: ReadOnlyKeyValueStore<String, String> = streams.store("store-name", keyValueStore<String, String>())

注意:当流准备就绪时,还有一个接口 KafkaStreams.StateListener

Note: There is also an interface KafkaStreams.StateListener for when the streams are ready

 override fun onChange(newState: KafkaStreams.State?, oldState: KafkaStreams.State?) =
    Option.fromNullable(newState)
        .filter { REBALANCING == oldState && RUNNING == it }
        .map { store = streams.store("store-name", keyValueStore<String, String>()) }
        .getOrElse { log.info("Waiting for Kafka being in REBALANCING -> RUNNING, but it is $oldState -> $newState") }

或者,您也可以使用

stream.groupByKey().reduce(...)

此处所述.

这篇关于线程"main"中的异常org.apache.kafka.streams.errors.InvalidStateStoreException:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆