How to add a custom StateStore to the Kafka Streams DSL processor?

后端 未结 1 400
感情败类
感情败类 2020-12-29 15:14

For one of my Kafka streams apps, I need to use the features of both DSL and Processor API. My streaming app flow is

source -> selectKey -> filter -&g         


        
相关标签:
1条回答
  • 2020-12-29 16:09

    You need to register you store outside of you processor using StreamsBuilder (or KStreamBuilder in older releases). First you create the store, than you registers it to StreamsBuilder (KStreamBuilder), and when you add the processor you provide the store name to connect the processor and the store.

    StreamsBuilder builder = new StreamsBuilder();
    
    // create store
    StoreBuilder storeBuilder = Stores.keyValueStoreBuilder(
        Stores.persistentKeyValueStore("invStore"),
        Serdes.String(),
        invSerde));
    // register store
    builder.addStateStore(storeBuilder);
    
    KStream<String, String> source = builder.stream(source_stream);
    source.selectKey(new MyKeyValueMapper())
            .filterNot((k,v) -> k.equals("UnknownGroup"))
            .process(() -> new MyProcessor(), "invStore"); // connect store to processor by providing store name
    
    
    // older API:
    
    KStreamBuilder builder = new KStreamBuilder();
    
    // create store
    StateStoreSupplier storeSupplier = (KeyValueStore)Stores.create("invStore")
        .withKeys(Serdes.String())
        .withValues(invSerde)
        .persistent()
        .build();
    // register store
    builder.addStateStore(storeSupplier);
    
    KStream<String, String> source = builder.stream(source_stream);
    source.selectKey(new MyKeyValueMapper())
            .filterNot((k,v) -> k.equals("UnknownGroup"))
            .process(() -> new MyProcessor(), "invStore"); // connect store to processor by providing store name
    
    0 讨论(0)
提交回复
热议问题