Exception in thread "main" org.apache.flink.api.common.InvalidProgramException: This type (GenericTy

在用flink做word_count练习时,代码如下:

public class WordCount_Java {
    public static void main(String[] args) throws Exception{
        //设置服务器地址和端口号,在master上执行命令“nc -lk 6666”,就能作为数据输入的端口
        int port = 6666;
        String hostName = "master";

        //初始化env对象,相当于spark的上下文对象
        StreamExecutionEnvironment env =
                StreamExecutionEnvironment.getExecutionEnvironment();

        //对接服务器及端口,以获取数据
        DataStreamSource<String> data = env.socketTextStream(hostName, port);

        //开始计算
        //生成一个个集合:(word,1)
        SingleOutputStreamOperator<WordWithCount> wordWithNum = data.flatMap(
                new FlatMapFunction<String, WordWithCount>() {
                    public void flatMap(String s,
                                        Collector<WordWithCount> out) throws Exception {
                        //这里的Collector是flink.util.Collector的
                        String[] words = s.split("\\s");
                        for (String word : words) {
                            out.collect(new WordWithCount(word, 1L));
                        }
                    }
                }
        );

        //将元组按照key进行分组
        KeyedStream<WordWithCount, Tuple> grouped = wordWithNum.keyBy("word");

        //用分组后的对象调用窗口操作
        //设置窗口大小和滑动间隔
        WindowedStream<WordWithCount, Tuple, TimeWindow> window =
                grouped.timeWindow(Time.seconds(2), Time.seconds(1));

        SingleOutputStreamOperator<WordWithCount> countsRes =
                window.sum("count");

        countsRes.print().setParallelism(1);

        env.execute();

    }

    private static class WordWithCount {
        public String word;
        public long count;
        public WordWithCount(){

        }
        public WordWithCount(String word,long count){
            this.word = word;
            this.count = count;
        }

        @Override
        public String toString() {
            return "WordWithCount{" +
                    "word='" + word + '\'' +
                    ", count=" + count +
                    '}';
        }
    }
}

运行后报错:

Exception in thread "main" org.apache.flink.api.common.InvalidProgramException: 
This type (GenericType<com.cbq.flink_test_na1906.WordCount_Java.WordWithCount>) cannot be used as key.

百思不得其解,辗转几次,发现定义的类必须是public的,改过来就正常了。另外,自定义的这个类必须构造无参构造器。

java.lang.NoClassDefFoundError: org/apache/flink/api/common/serialization/DeserializationSchema 是一个错误信息,表示在运行时找不到对应的类定义。 这个错误常常是由于缺少相关的依赖包或者版本不匹配导致的。 具体地说,这个错误信息表明缺少 org.apache.flink.api.common.serialization.DeserializationSchema 这个类的定义。可能是由于 flink-core 依赖包的版本不匹配或者没有正确引入所导致的。 解决这个问题的方法有两种: 1. 确保正确引入了 flink-core 依赖包,并且版本与项目所需的版本匹配。可以在项目的 pom.xml 或者 build.gradle 文件中检查依赖配置,确保 flink-core 的版本号正确且与项目要求的版本相符。同时也需要确保其他相关的 flink 依赖包也正确引入。 2. 如果依赖包的版本已经正确配置并且引入了,但仍然出现这个错误,可以尝试更改依赖包的 scope 或者调整 IDE 的配置。有时候将 flink-core 的 scope 修改为 provided 可能会解决这个问题。另外,如果使用的是 IntelliJ IDEA 等 IDE,可以尝试重新构建项目或者重启 IDE,以确保依赖包的正确加载。 综上所述,解决 java.lang.NoClassDefFoundError: org/apache/flink/api/common/serialization/DeserializationSchema 的方法包括检查 flink-core 的版本配置和引入,以及调整依赖包的 scope 或者 IDE 的配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [flink运行报错:java.lang.NoClassDefFoundError: org/apache/flink/api/java/......](https://blog.csdn.net/u011110301/article/details/117249082)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Exception in threadmain“ java.lang.NoClassDefFoundError: org/apache/flink/](https://blog.csdn.net/smileyan9/article/details/126661885)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值