我正在尝试将主题作为DomainEvent
的流来阅读。密钥包含在数据中。使用以下管道,我正在尝试构建聚合事件的KTable:
DomainEvent
映射到KeyValue<String, DomainEvent>
DomainEvent
折叠成Board
聚合抛出错误表示可能已迁移新主题。我正在读它,因为它不存在。
org.apache.kafka.streams.errors.InvalidStateStoreException: the state store, board-events-snapshots, may have migrated to another instance.
at org.apache.kafka.streams.state.internals.StreamThreadStateStoreProvider.stores(StreamThreadStateStoreProvider.java:48)
at org.apache.kafka.streams.state.internals.QueryableStoreProvider.getStore(QueryableStoreProvider.java:57)
at org.apache.kafka.streams.KafkaStreams.store(KafkaStreams.java:1034)
at org.springframework.cloud.stream.binder.kafka.streams.QueryableStoreRegistry.getQueryableStoreType(QueryableStoreRegistry.java:47)
at io.pivotal.dmfrey.eventStoreDemo.domain.client.kafka.service.KafkaBoardClient.find(KafkaBoardClient.java:61)
at io.pivotal.dmfrey.eventStoreDemo.domain.client.kafka.KafkaBoardClientEmbeddedKafkaTests.receiveAndValidateBoard(KafkaBoardClientEmbeddedKafkaTests.java:119)
at io.pivotal.dmfrey.eventStoreDemo.domain.client.kafka.KafkaBoardClientEmbeddedKafkaTests.testFind(KafkaBoardClientEmbeddedKafkaTests.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
代码
@StreamListener( "input" )
public void process( KStream<Object, DomainEvent> input ) {
input
.map( new KeyValueMapper<Object, DomainEvent, KeyValue<String, DomainEvent>>() {
@Override
public KeyValue<String, DomainEvent> apply( Object key, DomainEvent value ) {
KeyValue<String, DomainEvent> kv = new KeyValue<>( value.getBoardUuid().toString(), value );
System.out.println( "kv=" + kv.toString() );
return kv;
}
}
)
.groupBy( (s, domainEvent) -> s, Serialized.with( Serdes.String(), domainEventSerde ) )
.aggregate(
new Initializer<Board>() {
@Override
public Board apply() {
return new Board();
}
},
new Aggregator<String, DomainEvent, Board>() {
@Override
public Board apply( String key, DomainEvent value, Board aggregate ) {
return aggregate.handleEvent( value );
}
},
Materialized.as( BOARD_EVENTS_SNAPSHOTS )
);
}
(Lambdas已经扩展到此处发布)
如果我注释掉.aggregate
步骤,我会看到它处理事件。
查询KTable的代码
ReadOnlyKeyValueStore<String, Board> store = queryableStoreRegistry.getQueryableStoreType( BOARD_EVENTS_SNAPSHOTS, QueryableStoreTypes.<String, Board>keyValueStore() );
Board board = store.get( boardUuid.toString() );
if( null != board ) {
board.flushChanges();
log.debug( "find : board=" + board.toString() );
log.debug( "find : exit" );
return board;
} else {
throw new IllegalArgumentException( "board[" + boardUuid.toString() + "] not found!" );
}