【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));
相关推荐
Elastic 中国社区官方博客12 小时前
一个查询,无限 Elasticsearch Serverless 项目:跨项目搜索介绍
大数据·elasticsearch·搜索引擎·信息可视化·云原生·serverless·全文检索
西凉的悲伤12 小时前
SpringBoot WebClient 介绍
java·spring boot·后端·webclient
Simon5231412 小时前
mybatis执行流程、关联映射、注解开发
java·开发语言·mybatis
冷雨夜中漫步12 小时前
SQLite 深度解析:在 Java/Spring 中的使用与H2、Derby对比
java·spring·sqlite
laufing12 小时前
Java 模板引擎 FreeMarker 入门教程:语法、内建函数与常用案例
java·freemarker
wengqidaifeng12 小时前
C++从菜鸟到强手:2.类和对象(上)—— 从结构体到类的跨越
java·开发语言·c++
自律懒人12 小时前
2026年AI编程工具横评:Trae、Cursor、Claude Code、Copilot X,同一需求谁更强?
java·copilot·ai编程
夕除12 小时前
spring boot 13
java·mysql·spring
marlondu12 小时前
ScopedValue:Java 21 引入的结构化作用域值
java
samFuB12 小时前
【数据集】中国已签署双边投资协定(BIT)数据(2000-2025年)
大数据