java.lang.IllegalStateException:不应发生这种情况,因为headers()仅应在处理记录时调用

时间:2019-05-29 05:37:08

标签: apache-kafka-streams

(使用Kafka Streams)启动Stream应用程序失败,并显示“ java.lang.IllegalStateException:这不应发生,因为headers()仅应在处理记录时调用”

这似乎仅在主题中已经有数据时启动应用程序时发生。如果主题为空并且我开始向其推送数据,那么一切都很好。

有人知道为什么会这样吗?

谢谢

This should not happen as headers() should only be called while a record is processed
java.lang.IllegalStateException: This should not happen as headers() should only be called while a record is processed
        at org.apache.kafka.streams.processor.internals.AbstractProcessorContext.headers(AbstractProcessorContext.java:149)
        at org.apache.kafka.streams.state.internals.CachingKeyValueStore.putInternal(CachingKeyValueStore.java:235)
        at org.apache.kafka.streams.state.internals.CachingKeyValueStore.put(CachingKeyValueStore.java:222)
        at org.apache.kafka.streams.state.internals.CachingKeyValueStore.put(CachingKeyValueStore.java:37)
        at org.apache.kafka.streams.state.internals.MeteredKeyValueStore.put(MeteredKeyValueStore.java:153)
        at org.apache.kafka.streams.processor.internals.ProcessorContextImpl$KeyValueStoreReadWriteDecorator.put(ProcessorContextImpl.java:446)
        at fr.mediametrie.GeoIPEnrichmentProcessor.transform(GeoIPEnrichmentProcessor.java:97)
        at fr.mediametrie.GeoIPEnrichmentProcessor.transform(GeoIPEnrichmentProcessor.java:1)
        at org.apache.kafka.streams.kstream.internals.TransformerSupplierAdapter$1.transform(TransformerSupplierAdapter.java:47)
        at org.apache.kafka.streams.kstream.internals.TransformerSupplierAdapter$1.transform(TransformerSupplierAdapter.java:36)
        at org.apache.kafka.streams.kstream.internals.KStreamFlatTransform$KStreamFlatTransformProcessor.process(KStreamFlatTransform.java:56)
        at org.apache.kafka.streams.processor.internals.ProcessorNode.process(ProcessorNode.java:117)
        at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:183)
        at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:162)
        at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:122)
        at org.apache.kafka.streams.processor.internals.SourceNode.process(SourceNode.java:87)
        at org.apache.kafka.streams.processor.internals.StreamTask.process(StreamTask.java:364)
        at org.apache.kafka.streams.processor.internals.AssignedStreamsTasks.process(AssignedStreamsTasks.java:199)
        at org.apache.kafka.streams.processor.internals.TaskManager.process(TaskManager.java:420)
        at org.apache.kafka.streams.processor.internals.StreamThread.runOnce(StreamThread.java:890)
        at org.apache.kafka.streams.processor.internals.StreamThread.runLoop(StreamThread.java:805)
        at org.apache.kafka.streams.processor.internals.StreamThread.run(StreamThread.java:774)

1 个答案:

答案 0 :(得分:4)

根据 Matthias 分享的信息,这是一个实际示例:

  • 返回相同的转换器对象(也适用于 Processor 和 ValueTransformer)会导致 IllegalStateException
var transformer = new Transformer();

inputStream
  .transformValues(() -> transformer)
// further operations...

  • 每次返回一个新实例可以防止异常:
inputStream
  .transformValues(() -> new Transformer())
// further operations...

ps:我知道文档很清楚,但希望这可以节省一些滚动时间。