使用Spring-Cloud-Stream的KStream-GlobalKTable-Join-如何检查GlobalKTable的内容?

时间:2019-06-26 07:58:03

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

我正在使用Spring-Cloud-Stream实现一个KStream-GlobalKTable-Join,我正面临一个问题,即join操作没有任何匹配,但是绝对可以。代码如下:

@Component
@EnableBinding(CustomProcessor.class)
public class MyProcessor {
  private static final Log LOGGER = 
  LogFactory.getLog(MyProcessor.class);

  @Autowired
  private InteractiveQueryService interactiveQueryService;

  ReadOnlyKeyValueStore<Object, Object> keyValueStore;

  @StreamListener
  @SendTo(CustomProcessor.OUTPUT)
  public KStream<EventKey, EventEnriched> process(
    @Input(CustomProcessor.INPUT) KStream<EventKey, EventEnriched> inputStream,
    @Input(CustomProcessor.LOOKUP) GlobalKTable<LookupKey, LookupData> lookupStore
  ) {

    keyValueStore = interactiveQueryService.getQueryableStore("lookupStore", QueryableStoreTypes.keyValueStore());

    LOGGER.info("Lookup: " + keyValueStore.get(new LookupKey("google.de")));

    return inputStream.leftJoin(
      lookupStore,
      (inputKey, inputValue) -> {
        return new LookupKey(inputValue.getDomain().replace("www.", ""));
      },
      this::enrichData
    );
  }


  public EventEnriched enrichData(EventEnriched input, LookupData lookupRecord) {

    ...

  }
}

自定义处理器

public interface CustomProcessor extends KafkaStreamsProcessor {
  String INPUT = "input";
  String OUTPUT = "output";
  String LOOKUP = "lookupTable";

  @Input(CustomProcessor.LOOKUP)
  GlobalKTable<LookupKey, ?> lookupTable();
}

无需在 MyProcessor

中拨打电话
keyValueStore.get(...)

代码运行正常,但是GlobalKTable似乎为空。但是如果我打电话

LOGGER.info("Lookup: " + keyValueStore.get(new LookupKey("google.de")));

为了检查GlobalKTable,运行该应用程序失败并显示以下信息:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-26T09:04:00.000 [ERROR] [main-858] [org.springframework.boot.SpringApplication] [reportFailure:858] Application run failed
org.springframework.beans.factory.BeanInitializationException: Cannot setup StreamListener for public org.apache.kafka.streams.kstream.KStream MyProcessor.process(org.apache.kafka.streams.kstream.KStream,org.apache.kafka.streams.kstream.GlobalKTable); nested exception is java.lang.reflect.InvocationTargetException
at org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsStreamListenerSetupMethodOrchestrator.orchestrateStreamListenerSetupMethod(KafkaStreamsStreamListenerSetupMethodOrchestrator.java:214)
at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor.doPostProcess(StreamListenerAnnotationBeanPostProcessor.java:226)
at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$0(StreamListenerAnnotationBeanPostProcessor.java:196)
at java.base/java.lang.Iterable.forEach(Iterable.java:75)
at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor.injectAndPostProcessDependencies(StreamListenerAnnotationBeanPostProcessor.java:330)
at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor.afterSingletonsInstantiated(StreamListenerAnnotationBeanPostProcessor.java:113)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:866)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at Transformer.main(Transformer.java:31)
Caused by: java.lang.reflect.InvocationTargetException: null
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.cloud.stream.binder.kafka.streams.KafkaStreamsStreamListenerSetupMethodOrchestrator.orchestrateStreamListenerSetupMethod(KafkaStreamsStreamListenerSetupMethodOrchestrator.java:179)
... 15 common frames omitted
Caused by: java.lang.NullPointerException: null
at MyProcessor.process(MyProcessor.java:62)
... 20 common frames omitted

Process finished with exit code 1

有人在代码中看到问题吗?如何检查GlobaKTable的内容?

最好的问候 马丁

1 个答案:

答案 0 :(得分:0)

现在我越来越接近问题了。我试图查询lookupStore。如果我使用

final ReadOnlyKeyValueStore<LookupKey, LookupData> lookupStore =
            interactiveQueryService.getQueryableStore("myStore", QueryableStoreTypes.<LookupKey, LookupData>keyValueStore())

然后

lookupStore.get(key)

从不返回值。但是,如果我这样创建一个HashMap:

final KeyValueIterator<LookupKey, LookupData> lookups = lookupStore.all();

Map<LookupKey, LookupData> lookupMap = new HashMap<>();
while (lookups.hasNext()) {
    KeyValue<LookupKey, LookupData> nextLookup = lookups.next();
    lookupMap.put(nextLookup.key, nextLookup.value);
}
lookups.close();

hashMap包含正确的数据,并且正在向每个键返回正确的值。但是GlobalKTable本身由于某种原因而无法连接。它永远不会得到任何匹配。