• Flink-Java报错之org.apache.flink.api.common.functions.InvalidTypesException


    在启动Flink作业时报错。可以看到是在实现FlatMapFunction时InvalidTypesException类型无效。提示使用匿名类或者明确指定类型来解决。An easy workaround is to use an (anonymous) class instead that implements the 'org.apache.flink.api.common.functions.FlatMapFunction' interface. Otherwise the type has to be specified explicitly using type information.

    1. Exception in thread "main" org.apache.flink.api.common.functions.InvalidTypesException: The return type of function 'main(WordCount.java:22)' could not be determined automatically, due to type erasure. You can give type information hints by using the returns(...) method on the result of the transformation call, or by letting your function implement the 'ResultTypeQueryable' interface.
    2. at org.apache.flink.api.dag.Transformation.getOutputType(Transformation.java:496)
    3. at org.apache.flink.streaming.api.datastream.DataStream.getType(DataStream.java:193)
    4. at org.apache.flink.streaming.api.datastream.KeyedStream.(KeyedStream.java:116)
    5. at org.apache.flink.streaming.api.datastream.DataStream.keyBy(DataStream.java:293)
    6. at example.WordCount.main(WordCount.java:29)
    7. Caused by: org.apache.flink.api.common.functions.InvalidTypesException: The generic type parameters of 'Collector' are missing. In many cases lambda methods don't provide enough information for automatic type extraction when Java generics are involved. An easy workaround is to use an (anonymous) class instead that implements the 'org.apache.flink.api.common.functions.FlatMapFunction' interface. Otherwise the type has to be specified explicitly using type information.
    8. at org.apache.flink.api.java.typeutils.TypeExtractionUtils.validateLambdaType(TypeExtractionUtils.java:371)
    9. at org.apache.flink.api.java.typeutils.TypeExtractionUtils.extractTypeFromLambda(TypeExtractionUtils.java:188)
    10. at org.apache.flink.api.java.typeutils.TypeExtractor.getUnaryOperatorReturnType(TypeExtractor.java:557)
    11. at org.apache.flink.api.java.typeutils.TypeExtractor.getFlatMapReturnTypes(TypeExtractor.java:174)
    12. at org.apache.flink.streaming.api.datastream.DataStream.flatMap(DataStream.java:612)
    13. at example.WordCount.main(WordCount.java:22)

    可以搜索这个报错,有很多文章提到了解决方法,并给出了示例代码。

    但是这个报错的原因是当涉及Java泛型时,lambda方法不能为自动类型提取提供足够的信息。也就是说由于类型擦除,无法自动确定。所以其实还有一个很简单的解决方案,就是不用Lambda表达式

    1. SingleOutputStreamOperator> sum =
    2. source
    3. .flatMap(
    4. new FlatMapFunction>() {
    5. @Override
    6. public void flatMap(String s, Collector> collector) throws Exception {
    7. for (String string : s.split(" ")) {
    8. collector.collect(new Tuple2<>(string,1L));
    9. }
    10. }
    11. }
    12. )
    13. .keyBy(s->s.f0).sum(1);
    14. sum.print("wc:");

  • 相关阅读:
    2023年09月 C/C++(八级)真题解析#中国电子学会#全国青少年软件编程等级考试
    javaSE -运算符,注释,关键字(复习)
    Pytorch框架学习记录6——torch.nn.Module和torch.nn.functional.conv2d的使用
    Android笔记--应用安装
    操作系统复习:线程
    js 滚动鼠标滑轮放大缩小图片
    TCP三次握手与四次挥手
    GoLang之Go 编译链接过程概述(3)
    发现学习的新契机——广东开放大学电大搜题服务
    5.2 EF Core性能优化
  • 原文地址:https://blog.csdn.net/BugEveryday/article/details/136572298