본문 바로가기

spark - python - R

[SPARK] Mongo Mysql foreachRDD

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Data to Mongo

 

             line_data.foreachRDD(new Function<JavaRDD<String>, Void>() {
                  private static final long serialVersionUID = 1L;
                  Mongo mongo = new Mongo("몽고URL", 27017);
               DB db = mongo.getDB("DB명");
                  public Void call(JavaRDD<String> data) throws Exception {
                    DBCollection collection = db.getCollection("컬렉션명");
                       
                    if(data!=null){
                        List<String>result = data.collect();
                        for (String temp :result) {                             
                            //mongo
                            DBObject dbObject = (DBObject) JSON.parse(temp.toString());
                            collection.insert(dbObject);
                        }
                    }else {
                        System.out.println("Empty");
                    }
                    return null;
                } 
            });




Data to Mysql

 

             line_data.foreachRDD(new Function<JavaPairRDD<String, String>, Void>() {           
                private static final long serialVersionUID = 1L;
                public Void call(JavaPairRDD<String, String> rdd) throws Exception {
                     rdd.foreach(
                                new VoidFunction<Tuple2<String,String>>() {
                                    private static final long serialVersionUID = 1L;
                                    public void call(Tuple2<String,String> data) {
                                               //mysql mybatis 
                                    }
                                }
                            );
                            return null;
                    } 
            });