欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Type EnumTypeInfo<xxxxx> cannot be used as key. Contained UNSUPPORTED key types: EnumTypeInfo<xxxxx>

发布时间:2023/12/31 编程问答 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Type EnumTypeInfo<xxxxx> cannot be used as key. Contained UNSUPPORTED key types: EnumTypeInfo<xxxxx> 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

事情起因

https://github.com/chaoxxx/learn-flink-stream-api/blob/master/src/main/java/fun/cosmozhu/session16/main/StreamTest.java

这个repository被我clone下来后运行很顺利.

想复制到自己的另外一个工程里面,结果报错了

报错内容

22:45:50,226 INFO  org.apache.flink.api.java.typeutils.TypeExtractor  - class fun.cosmozhu.session16.pojo.ExchangeRateInfo is missing a default constructor so it cannot be used as a POJO type and must be processed as GenericType. 

Please read the Flink documentation on "Data Types & Serialization" for details of the effect on performance.
Exception in thread "main" org.apache.flink.api.common.InvalidProgramException: Type EnumTypeInfo<fun.cosmozhu.session16.pojo.CurrencyType> cannot be used as key. Contained UNSUPPORTED key types: EnumTypeInfo<fun.cosmozhu.session16.pojo.CurrencyType>. Look at the keyBy() documentation for the conditions a type has to satisfy in order to be eligible for a key.
    at org.apache.flink.streaming.api.datastream.KeyedStream.validateKeyType(KeyedStream.java:195)
    at org.apache.flink.streaming.api.datastream.KeyedStream.<init>(KeyedStream.java:162)
    at org.apache.flink.streaming.api.datastream.KeyedStream.<init>(KeyedStream.java:131)
    at org.apache.flink.streaming.api.datastream.KeyedStream.<init>(KeyedStream.java:118)
    at org.apache.flink.streaming.api.datastream.DataStream.keyBy(DataStream.java:296)
    at fun.cosmozhu.session16.main.StreamTest.main(StreamTest.java:57)

Process finished with exit code 1

调试技巧

到这里的时候,我基本上花了好几个小时都卡在这个问题上了.

最后是通过在原始的repository修改pom.xml中的flink版本从1.9.0改为1.11.2然后触发了上面的错误.

说明Flink 1.9.0-1.11.2对 pojo的格式要求已经发生了很大的变化.

然后注意,

application.properties终于logo的警告等级必须是INFO,不能是WARN,否则上面的提示信息你就看不到了.

22:45:50,226 INFO  org.apache.flink.api.java.typeutils.TypeExtractor  - class fun.cosmozhu.session16.pojo.ExchangeRateInfo is missing a default constructor so it cannot be used as a POJO type and must be processed as GenericType. 

application.properties示范:

log4j.rootLogger=INFO, consolelog4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p %-60c %x - %m%n

 

解决方案

package fun.cosmozhu.session16.pojo; /*** 货币类型* @author cosmozhu* @mail zhuchao1103@gmail.com* @site http://www.cosmozhu.fun*/ public enum CurrencyType {USD,CNY,EUR,AUD}

改为:

package fun.cosmozhu.session16.pojo; /*** 货币类型* @author cosmozhu* @mail zhuchao1103@gmail.com* @site http://www.cosmozhu.fun*/public enum CurrencyType {USD("美元"),CNY("人民币"),EUR("欧元"),AUD("澳元");private final String name;private CurrencyType(String name) {this.name = name;}public String getName() {return name;} }

 

总结

以上是生活随笔为你收集整理的Type EnumTypeInfo<xxxxx> cannot be used as key. Contained UNSUPPORTED key types: EnumTypeInfo<xxxxx>的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。