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的,改过来就正常了。另外,自定义的这个类必须构造无参构造器。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值