初始化状态会导致此错误“;java.lang.NullPointerException:键控状态只能用于';键控流&x27&引用;

初始化状态会导致此错误“;java.lang.NullPointerException:键控状态只能用于';键控流&x27&引用;,java,apache-flink,flink-streaming,flink-cep,Java,Apache Flink,Flink Streaming,Flink Cep,我试图在协处理函数中初始化一个“ListState”,但是它一直抛出这个错误 “”java.lang.NullPointerException:键控状态只能在“键控流”上使用。“” 这是我使用的代码sinnpet dataStream1.keyBy(ele->ele.f1).connect(dataStream2).process( new CoProcessFunction<Tuple2<Long, String>, String, Object

我试图在协处理函数中初始化一个“ListState”,但是它一直抛出这个错误 “”java.lang.NullPointerException:键控状态只能在“键控流”上使用。“”

这是我使用的代码sinnpet

    dataStream1.keyBy(ele->ele.f1).connect(dataStream2).process(
        new CoProcessFunction<Tuple2<Long, String>, String, Object>() {
            ListState stream1ListState;
            
            @Override
            public void open(Configuration parameters) throws Exception {
                LOG.info("inside CoProcess Function Constructor");
                ListStateDescriptor<Tuple2<Long,String>> listStateDescriptor = new
                    ListStateDescriptor<Tuple2<Long,String>>("type1",TypeInformation.of(new TypeHint<Tuple2<Long,String>>(){}));
                stream1ListState = getRuntimeContext().getListState(listStateDescriptor);
            }
            
            @Override
            public void processElement1(Tuple2<Long, String> longStringTuple2, Context context,
                Collector<Object> collector) throws Exception {
                LOG.info("inside stream1 processor");
                LOG.info(longStringTuple2.toString());
                collector.collect(longStringTuple2);
            }
            
            @Override
            public void processElement2(String s, Context context, Collector<Object> collector)
                throws Exception {
                LOG.info("inside stream2 processor");
                LOG.info(s);
            }
        }).print();
错误日志跟踪的其余部分如下所示

Caused by: java.lang.NullPointerException: Keyed state can only be used on a 'keyed stream', i.e., after a 'keyBy()' operation.
    at org.apache.flink.util.Preconditions.checkNotNull(Preconditions.java:76)
    at org.apache.flink.streaming.api.operators.StreamingRuntimeContext.checkPreconditionsAndGetKeyedStateStore(StreamingRuntimeContext.java:232)
    at org.apache.flink.streaming.api.operators.StreamingRuntimeContext.getListState(StreamingRuntimeContext.java:202)

该错误意味着您只能在
KeyedCoProcessFunction
内部使用
ListState
ValueState
等,而这只能在两个流都设置了键时使用。因此,如果第二个流是正常流,并且可以由某个键设置密钥,那么您可以这样做,否则您可能希望参考所描述的广播状态模式

Caused by: java.lang.NullPointerException: Keyed state can only be used on a 'keyed stream', i.e., after a 'keyBy()' operation.
    at org.apache.flink.util.Preconditions.checkNotNull(Preconditions.java:76)
    at org.apache.flink.streaming.api.operators.StreamingRuntimeContext.checkPreconditionsAndGetKeyedStateStore(StreamingRuntimeContext.java:232)
    at org.apache.flink.streaming.api.operators.StreamingRuntimeContext.getListState(StreamingRuntimeContext.java:202)