Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.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 ApacheFlink流媒体窗口字数_Java_Apache Flink_Flink Streaming - Fatal编程技术网

Java ApacheFlink流媒体窗口字数

Java ApacheFlink流媒体窗口字数,java,apache-flink,flink-streaming,Java,Apache Flink,Flink Streaming,我有以下代码来计算socketTextStream中的单词。累积字数和时间窗字数都是必需的。该程序存在累积计数始终与窗口计数相同的问题。为什么会出现这个问题?根据加窗计数计算累积计数的正确方法是什么 StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); final HashMap<String, Integer> cumulateCounts = new HashM

我有以下代码来计算socketTextStream中的单词。累积字数和时间窗字数都是必需的。该程序存在累积计数始终与窗口计数相同的问题。为什么会出现这个问题?根据加窗计数计算累积计数的正确方法是什么

StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
final HashMap<String, Integer> cumulateCounts = new HashMap<String, Integer>();

final DataStream<Tuple2<String, Integer>> counts = env
            .socketTextStream("localhost", 9999)
            .flatMap(new Splitter())
            .window(Time.of(5, TimeUnit.SECONDS))
            .groupBy(0).sum(1)
            .flatten();

counts.print();

counts.addSink(new SinkFunction<Tuple2<String, Integer>>() {
    @Override
    public void invoke(Tuple2<String, Integer> value) throws Exception {
        String word = value.f0;
        Integer delta_count = value.f1;
        Integer count = cumulateCounts.get(word);
        if (count == null)
            count = 0;
        count = count + delta_count;
        cumulateCounts.put(word, count);
        System.out.println("(" + word + "," + count.toString() + ")");
    }
});
StreamExecutionEnvironment env=StreamExecutionEnvironment.getExecutionEnvironment();
最终HashMap cumulateCounts=新HashMap();
最终数据流计数=环境
.socketTextStream(“localhost”,9999)
.flatMap(新拆分器())
.窗口(时间(5,时间单位秒))
.groupBy(0).sum(1)
.flatte();
counts.print();
counts.addSink(新的SinkFunction(){
@凌驾
公共void调用(Tuple2值)引发异常{
字符串字=value.f0;
整数delta_count=value.f1;
整数计数=累积计数。获取(字);
如果(计数=null)
计数=0;
计数=计数+增量计数;
累积计数。输入(字,计数);
System.out.println(“(“+word+”,“+count.toString()+”);
}
});

您应该首先分组,并在键控数据流上应用窗口(您的代码适用于Flink 0.9.1,但Flink 0.10.0中的新API对此要求严格):


您应该首先分组,并在键控数据流上应用窗口(您的代码适用于Flink 0.9.1,但Flink 0.10.0中的新API对此要求严格):


谢谢你的回复。你能再解释一下我的解决方案失败的原因吗?在flink cluster中,这些解决方案的数据流有什么不同?一个问题:如何仅获得求和运算的最终结果而不在数据流中“滚动”求和?感谢您的回答。你能再解释一下我的解决方案失败的原因吗?在flink cluster中,这些解决方案之间的数据流有什么不同?一个问题:如何仅获得求和运算的最终结果,而不在数据流中“滚动”求和?
final DataStream<Tuple2<String, Integer>> counts = env
        .socketTextStream("localhost", 9999)
        .flatMap(new Splitter())
        .groupBy(0)
        .window(Time.of(5, TimeUnit.SECONDS)).sum(1)
        .flatten();
counts.groupBy(0).sum(1).print();