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

错误展示:

java 复制代码
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)

错误原因

shell 复制代码
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 无法准确获取到数据类型,此时就需要我们手动指定类型

处理方案

java 复制代码
source.flatMap(()->{
	...
//手动指定类型
},Types.类型)
  • 案例:
java 复制代码
SingleOutputStreamOperator<Tuple2<Object, Integer>> map = flatMap
      .map(word -> Tuple2.of(word, 1)
      ,Types.TUPLE(Types.STRING, Types.INT));
相关推荐
xgc_java7 分钟前
在Java里把ONNX/OpenCV/FFmpeg跑稳:28篇bytedeco实战小册完整指南
java·opencv·ffmpeg
Lhappy嘻嘻10 分钟前
网络(四)|全网最细网络层原理:IP 协议、IP 地址分类、子网划分、路由转发、NAT 穿透详解
java·笔记·网络协议·计算机网络
大模型码小白24 分钟前
Milvus 架构设计:从向量索引到分布式检索,AI 原生存储引擎的内部机制
java·大数据·人工智能·分布式·深度学习·milvus
稚南城才子,乌衣巷风流25 分钟前
链表(Linked List):数据结构详解与实战应用
java
NWU_LK29 分钟前
【WebFlux】第二篇 —— Project Reactor 核心数据类型
java
三84433 分钟前
笔记:在同一个物理服务器上通过nginx的虚拟主机生成多个不同的web站点
java·前端·nginx
LitchiCheng1 小时前
轻量化微调 Qwen2.5 LoRA 训练全流程详解
大数据·人工智能·spark
2501_932750261 小时前
Android 数据持久化解析
android·java
NWU_LK2 小时前
【WebFlux】第三篇 —— 常用操作符与数据流转换
java
Mr__Miss10 小时前
Java泛型完全指南:从入门到精通
java·开发语言·python