如何在Spring Cloud Stream Kafka Streams应用程序中使用StateStoreBuilder添加StateStore

时间:2019-06-17 11:13:27

标签: apache-kafka spring-cloud apache-kafka-streams spring-cloud-stream

Kafka原生API允许我们create and add a state store using the StreamsBuilder

    final StreamsBuilder builder = new StreamsBuilder();
    ...
    final StoreBuilder<WindowStore<String, Long>> dedupStoreBuilder = Stores.windowStoreBuilder(
            Stores.persistentWindowStore(storeName,
                                         retentionPeriod,
                                         windowSize,
                                         false
            ),
            Serdes.String(),
            Serdes.Long());

    builder.addStateStore(dedupStoreBuilder);

我想使用Spring Cloud Streams进行相同的操作,但无法找出访问StreamsBuilder来添加商店的方式。

我尝试按照doc中的说明检索StreamsBuilderFactoryBean,希望它可以从中获取StreamsBuilder对象,但是bean似乎不可用:

@EnableBinding(KafkaStreamsProcessor::class)
class FraudKafkaStreamsConfiguration(private val context: ApplicationContext) {

    @StreamListener
    @SendTo("output")
    fun process(@Input("input") input: KStream<String, TransferEmitted>): KStream<String, TransferEmitted> {

        val streamsBuilderFactoryBean = context.getBean("&stream-builder-process", StreamsBuilderFactoryBean::class.java)
        ...
        return xxx

    }

}
  

原因:   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   名为“ stream-builder-process”的bean可用

无论如何,我什至不确定这是否是正确的方法。那么,我们如何以编程方式创建StateStore

1 个答案:

答案 0 :(得分:1)

由于我的Scs版本(Fishtown SR3),我没有看到记录的程序,但是好消息是,自Germantown以来,可以声明式创建State Store:

const val DEDUP_STORE = "dedup-store"

@EnableBinding(KafkaStreamsProcessor::class)
class FraudKafkaStreamsConfiguration {

    @KafkaStreamsStateStore(name = DEDUP_STORE, type = KafkaStreamsStateStoreProperties.StoreType.KEYVALUE)
    @StreamListener
    @SendTo("output")
    fun process(@Input("input") input: KStream<String, TransferEmitted>): KStream<String, TransferEmitted> {
        return input.transform(TransformerSupplier { DeduplicationTransformer() }, DEDUP_STORE)

    }

}