分享

请大神指点,持久化到MySql中 VoidFunction报错

阿里云 发表于 2017-12-8 23:52:06 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 4 5264
wordcounts.foreachRDD(new VoidFunction <JavaPairRDD<String, Integer> > (){

                        private static final long serialVersionUID = 1L;

                        @Override
                        public void call(JavaPairRDD<String, Integer> wordcountsRDD)
                                        throws Exception {
                                wordcountsRDD.foreachPartition(new VoidFunction<Iterator<Tuple2<String,Integer>>>(){

                                        private static final long serialVersionUID = 1L;

                                        @Override
                                        public void call(Iterator<Tuple2<String, Integer>> wordcounts)
                                                        throws Exception {
                                                Connection conn = ConnectionPool.getConnection();
                                               
                                                Tuple2<String,Integer> wordcount = null;
                                                while(wordcounts.hasNext()){
                                                        wordcount = wordcounts.next();
                                                        String sql = "insert into wordcount(word,count) "
                                                                        + "values('" + wordcount._1 + "'," + wordcount._2 + ")";
                                                        Statement stmt = conn.createStatement();
                                                        stmt.executeUpdate(sql);
                                                }
                                               
                                                ConnectionPool.returnConnection(conn);
                                        }
                                });
                        }
                       
                });

报错:The method foreachRDD(Function<JavaPairRDD<String,Integer>,Void>) in the type
AbstractJavaDStreamLike<Tuple2<String,Integer>,JavaPairDStream<String,Integer>,JavaPairRDD<Stri
ng,Integer>> is not applicable for the arguments (new VoidFunction<JavaPairRDD<String,Integer>>()
{})

已有(4)人评论

跳转到指定楼层
qcbb001 发表于 2017-12-9 09:34:31
函数调用都是void,猜测可能是返回值造成的错误。下面函数是否有返回值
ConnectionPool.returnConnection(conn);
回复

使用道具 举报

starrycheng 发表于 2017-12-9 09:44:50
spark建议使用Scala,编程方便快捷。
这里有Java和Scala版本入库方式,仅供参考
java版本如何将spark的处理结果存入mysql?
http://www.aboutyun.com/forum.php?mod=viewthread&tid=20844


回复

使用道具 举报

阿里云 发表于 2017-12-9 15:32:42
qcbb001 发表于 2017-12-9 09:34
函数调用都是void,猜测可能是返回值造成的错误。下面函数是否有返回值
ConnectionPool.returnConnection( ...

返回值是 void
回复

使用道具 举报

阿里云 发表于 2017-12-9 15:34:06
starrycheng 发表于 2017-12-9 09:44
spark建议使用Scala,编程方便快捷。
这里有Java和Scala版本入库方式,仅供参考
java版本如何将spark的处 ...

嗯嗯, 好的, 谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条