【Flink 问题集】The generic type parameters of ‘Collector‘ are missing

错误展示:

在这里插入图片描述

Exception in thread "main" org.apache.flink.api.common.functions.InvalidTypesException: The return type of function 'main(CollectionDemo.java:33)' 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.
	at org.apache.flink.api.dag.Transformation.getOutputType(Transformation.java:543)
	at org.apache.flink.streaming.api.datastream.DataStream.getType(DataStream.java:192)
	at org.apache.flink.streaming.api.datastream.KeyedStream.<init>(KeyedStream.java:117)
	at org.apache.flink.streaming.api.datastream.DataStream.keyBy(DataStream.java:292)
	at com.example.bigdata.flink.dataStreamApi.source.CollectionDemo.main(CollectionDemo.java:38)
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.
	at org.apache.flink.api.java.typeutils.TypeExtractionUtils.validateLambdaType(TypeExtractionUtils.java:371)
	at org.apache.flink.api.java.typeutils.TypeExtractionUtils.extractTypeFromLambda(TypeExtractionUtils.java:188)
	at org.apache.flink.api.java.typeutils.TypeExtractor.getUnaryOperatorReturnType(TypeExtractor.java:560)
	at org.apache.flink.api.java.typeutils.TypeExtractor.getFlatMapReturnTypes(TypeExtractor.java:177)
	at org.apache.flink.streaming.api.datastream.DataStream.flatMap(DataStream.java:611)
	at com.example.bigdata.flink.dataStreamApi.source.CollectionDemo.main(CollectionDemo.java:33)

错误原因

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.
// 缺少“Collector”的泛型类型参数。在许多情况下,当涉及Java泛型时,lambda方法不能为自动类型提取提供足够的信息

原因描述

java 8 在使用Java API 写 Lambda 的时候,JVM 运行时会擦除类型(泛型类型)
Flink 无法准确获取到数据类型,此时就需要我们手动指定类型

处理方案

source.flatMap(()->{
	...
//手动指定类型
},Types.类型)
  • 案例:
SingleOutputStreamOperator<Tuple2<Object, Integer>> map = flatMap
      .map(word -> Tuple2.of(word, 1)
      ,Types.TUPLE(Types.STRING, Types.INT));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lxtx-0510

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值