Apache Flink - 错误:方法适用不适用于参数(WindowFunction)

时间:2016-11-03 13:29:37

标签: apache-flink flink-streaming

我是使用Apache Flink的新手。我从Apache Kafka源读取数据并需要转换DataStream

在最后一步中,我尝试应用WindowFunction

DataStream<Tuple8<Double, Double, String, Double, Double, Double, Double, Double>> dataStream = 
                     env
                    .addSource(new FlinkKafkaConsumer08<>(
                                    parameterTool.getRequired("topic"),
                                    new SimpleStringSchema(), 
                                    parameterTool.getProperties()))
                    .flatMap(new SplitIntoRecordsString())
                    .flatMap(new SplitIntoTuples())
                    .keyBy(1)
                    .countWindow(5)
                    .apply(new windowApplyFunction());

    public class windowApplyFunction implements WindowFunction<
                                                            Tuple8<Double, Double, String, Double, Double, Double, Double, Double>, 
                                                            String,  
                                                            Double, 
                                                            Window>{

    public void apply(Double key, Window window,
            Iterable<Tuple8<Double, Double, String, Double, Double, Double, Double, Double>> values,
            Collector<String> out)
            throws Exception {      
        out.collect("MyResult");
    }
}

不幸的是我收到了以下错误,并且不知道如何解决它:

The method apply(WindowFunction<Tuple8<Double,Double,String,Double,Double,Double,Double,Double>,R,Tuple,GlobalWindow>) in the type WindowedStream<Tuple8<Double,Double,String,Double,Double,Double,Double,Double>,Tuple,GlobalWindow> is not applicable for the arguments (FlinkManager.windowApplyFunction)

如果我用预定义的函数替换apply(new windowApplyFunction()),那么一切正常。 sum(1)

2 个答案:

答案 0 :(得分:0)

您的WindowFunction应该是

的类型
WindowFunction<
    Tuple8<Double, Double, String, Double, Double, Double, Double, Double>,
    String,
    Double,
    GlobalWindow>

countWindow()会返回 GlobalWindow 类型。

试一试。

答案 1 :(得分:0)

感谢你的暗示vanekjar!在纠正了这个错误后,我改变了另一个小东西,它现在有效! 正确的代码:

public static class windowApplyFunction implements WindowFunction<
                                                            Tuple8<Double, Double, String, Double, Double, Double, Double, Double>, 
                                                            Tuple8<Double, Double, String, Double, Double, Double, Double, Double>, 
                                                            Tuple, 
                                                            GlobalWindow>{

    public void apply(Tuple key, GlobalWindow window,
            Iterable<Tuple8<Double, Double, String, Double, Double, Double, Double, Double>> values,
            Collector<Tuple8<Double, Double, String, Double, Double, Double, Double, Double>> out)
            throws Exception {      
        out.collect(new Tuple8<Double, Double, String, Double, Double, Double, Double, Double>());
    }
}