为什么我的Kafka Transformer的StateStore无法访问?

时间:2018-04-07 22:23:25

标签: apache-kafka-streams

我正在尝试实现Transformer类

public class StreamSorterByTimeStampWithDelayTransformer < V > 
    implements Transformer< Long, V, KeyValue< Long, V > >

该类的构造函数为每个实例创建一个StateStore,因此:

this.state_store_name = "state_store_" + this.hashCode();

KeyValueBytesStoreSupplier key_value_store_supplier = Stores.persistentKeyValueStore( state_store_name );
StoreBuilder< KeyValueStore< String, V > > key_value_store_builder = 
    Stores.keyValueStoreBuilder( key_value_store_supplier, Serdes.String(), v_instance.getSerde() );
stream_builder.addStateStore( key_value_store_builder );

Transformer init方法因此引用StateStore:

public void init(ProcessorContext context) {
    this.context = context;
    this.key_value_store = (KeyValueStore< String, V >) context.getStateStore( state_store_name );
    // schedule a punctuate() method every "poll_ms" (wall clock time)
    this.context.schedule( this.poll_ms, PunctuationType.WALL_CLOCK_TIME, 
            (timestamp) -> pushOutOldEnoughEntries() );
}

我认为我必须省略一步,因为在进行getStateStore调用时,它 导致异常说:

Processor KSTREAM-TRANSFORM-0000000003 has no access to StateStore

我省略或做错了什么?

1 个答案:

答案 0 :(得分:7)

您需要将商店连接到变压器:

stream.transform(..., this.state_store_name);