1

I'm trying to perform a flatTransform in a Spring Cloud Kafka Streams app. But I'm not sure where exactly to put the KafkaStreamsStateStore annotation. At the moment I'm getting the error: Invalid topology: StateStore activeInstruments is not added yet. If anybody could give me some guidance I'd be very grateful.

@SpringBootApplication
public class InstrumentsApp {

  public static void main(String[] args) {
    SpringApplication.run(InstrumentsApp.class, args);
  }

  public static class InstrumentsConsumer {

    @Bean
    public Serde<InstrumentsRes> instrumentsResSerde() {
      ObjectMapper mapper = new ObjectMapper(new MessagePackFactory());
      mapper.registerModule(new JavaTimeModule());
      mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
      return new JsonSerde<>(InstrumentsRes.class, mapper);
    }

    @Bean
    public Serde<Instrument> instrumentSerde() {
      ObjectMapper mapper = new ObjectMapper(new MessagePackFactory());
      mapper.registerModule(new JavaTimeModule());
      mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
      mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
      return new JsonSerde<>(Instrument.class, mapper);
    }

    @Bean
    @KafkaStreamsStateStore(name = "activeInstruments",
        type = KafkaStreamsStateStoreProperties.StoreType.KEYVALUE)
    public Consumer<KStream<String, InstrumentsRes>> process() {
      return instruments -> instruments.flatTransform(
          new TransformerSupplier<String, InstrumentsRes, Iterable<KeyValue<String, Instrument>>>() {
            public Transformer<String, InstrumentsRes, Iterable<KeyValue<String, Instrument>>> get() {
              return new Transformer<String, InstrumentsRes, Iterable<KeyValue<String, Instrument>>>() {

                private ProcessorContext context;
                private KeyValueStore<String, InstrumentsRes> state;

                @SuppressWarnings("unchecked")
                @Override
                public void init(ProcessorContext context) {
                  this.context = context;
                  this.state = (KeyValueStore<String, InstrumentsRes>) context
                      .getStateStore("activeInstruments");
                }

                @Override
                public Iterable<KeyValue<String, Instrument>> transform(String key,
                    InstrumentsRes value) {
                  List<KeyValue<String, Instrument>> result = new ArrayList<>();
                  for (Instrument instrument : value.result) {
                    result.add(KeyValue.pair(instrument.instrumentName, instrument));
                  }
                  InstrumentsRes prevValue = state.get(key);
                  if (prevValue != null) {
                    HashSet<String> prevInstrumentNames = value.getInstrumentNames();
                    HashSet<String> newInstrumentNames = value.getInstrumentNames();
                    prevInstrumentNames.removeAll(newInstrumentNames);
                    for (String instrumentName : prevInstrumentNames) {
                      result.add(KeyValue.pair(instrumentName, null));
                    }
                  }
                  state.put(key, value);
                  return result;
                }

                public void close() {
                }
              };
            }
          }, "activeInstruments");
    }

  }

}

1 Answers1

1

To answer my own question, I believe the KafkaStreamsStateStore annotation is now deprecated: https://github.com/spring-cloud/spring-cloud-stream-binder-kafka/issues/676#issuecomment-502923103.

Now I've created a bean of type StoreBuilder, as suggested in the comment, and everything is working as expected.