Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache Flink进程函数状态未保持该状态_Java_Stream_Apache Flink_Flink Streaming - Fatal编程技术网

Java Apache Flink进程函数状态未保持该状态

Java Apache Flink进程函数状态未保持该状态,java,stream,apache-flink,flink-streaming,Java,Stream,Apache Flink,Flink Streaming,我正在为Apache Flink 1.4中的processElement函数编写一些代码: public class ProcessFunctionClass extends ProcessFunction<Tuple2<String, String>, Tuple2<String, String>>{ private ListState<String> listState; public void processElement

我正在为Apache Flink 1.4中的processElement函数编写一些代码:

public class ProcessFunctionClass extends ProcessFunction<Tuple2<String, String>, Tuple2<String, String>>{

    private ListState<String> listState;

    public void processElement(Tuple2<String, String> tuple2,  Context context, Collector<Tuple2<String, String>> collector) {

        // if the state is empty, start a timer
        if (listState.get().iterator().hasNext() == false)
            context.timerService().registerEventTimeTimer(10000);

        listState.add("someStringToBeStored");

        // ...
    }

}
当计时器过期时,我有此功能:

public void onTimer(long timestamp, OnTimerContext ctx, Collector<Tuple2<String, String>> out) throws Exception {
    Iterable<String> strings = listState.get();
    int cnt = 0;
    int totalLength = 0;
    Iterator<String> it = strings.iterator();
    while (it.hasNext()) {
        cnt++;
        totalLength += it.next().length();
    }
    LOGGER.info("cnt is:" + cnt);
    LOGGER.info("totalLength is:" + totalLength);

    // clearing the state
    listState.clear();
}

但是,每次运行应用程序时,cnt的值总是1,totalLength的值是当时已处理的特定字符串的长度。系统中似乎没有保存该状态。从这段代码可以清楚地看出我做错了什么吗?

您的ProcessFunction类需要扩展Flink ProcessFunction。

进程函数使用键分区状态,这意味着每个键都有一个单独的列表。我的猜测是,没有一个键在10秒内有多个事件。

是的,我忘记把它放在这里了。编辑。