【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));
相关推荐
专注于大数据技术栈9 分钟前
java学习--Vector
java·学习
sheji341612 分钟前
【开题答辩全过程】以 基于Java的校内美食推荐系统的设计与实现为例,包含答辩的问题和答案
java·开发语言·美食
白典典15 分钟前
解决iTextPDF生成手册时目录页码与实际页码不匹配问题
java·spring·intellij-idea
静心观复17 分钟前
foreach中使用remove踩坑
java
内存不泄露18 分钟前
基于 Spring Boot 的医院预约挂号系统(全端协同)设计与实现
java·vue.js·spring boot·python·flask
袁慎建@ThoughtWorks21 分钟前
如何发布自定义 Spring Boot Starter
java·spring boot·后端
开开心心_Every29 分钟前
强制打字练习工具:打够百字才可退出
java·游戏·微信·eclipse·pdf·excel·语音识别
xiaolyuh12330 分钟前
Redis 核心业务流程
java·redis·spring
BD_Marathon32 分钟前
MyBatis——封装SqlSessionUtils工具类并测试功能
java·windows·mybatis
浩瀚地学36 分钟前
【Java】集合-Collection
java·开发语言·经验分享·笔记·学习