• JAVA -导数到MongoDB(含MAP集合)


    package com.MyTestMongo;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import com.mongodb.BasicDBObject;
    import com.mongodb.DB;
    import com.mongodb.DBCollection;
    import com.mongodb.DBObject;
    import com.mongodb.Mongo;
    /**
     *将数据存入MongoDB中,有一列包含List集合 
     */
    public class ReadFile {
        private static DB db;
        private static Mongo mg;
        public static List<BasicDBObject> list = new ArrayList<BasicDBObject>();
        private static int num = 0;
        
        /**
         * 对找出来的每一个文件进行读取
         * @param path
         */
        public static void fileData(String path,int j){
            File file = new File(path);
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(file));
                for(String line = new String(br.readLine().getBytes(),"utf-8");line != null;line = br.readLine()){
                    num++;
                    BasicDBObject basi = new BasicDBObject();
                    String strs[] = line.split("    ");
                    basi.put("_id",strs[0]);
                    int a = 0;
                    for(int i = 1;i < strs.length;i = i + 2){
                        a++;
                        Map<String,Object> map = new HashMap<String, Object>();
                        map.put("n" + a, strs[i]);
                        map.put("t" + a, Integer.parseInt(strs[i+1]));
                        basi.put("n" + a, map);
                    }
                    list.add(basi);
                    if(num % 1000 == 0){
                        insertbeach(list);
                        System.out.println("已导入" + num + "条数据");
                        list.clear();
                    }
                }
                insertbeach(list);
                System.out.println("已导入完成!");
                list.clear();
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        
        public static List<DBObject> list2 = new ArrayList<DBObject>();
        /**
         * 将List集合中的数据导入MongoDB
         * @param list
         */
        public static void insertbeach(List<BasicDBObject> list){
            for(int j = 0;j < list.size();j++){
                DBObject obj = list.get(j);
                list2.add(obj);
            }
            DBCollection dd = db.getCollection("zq4");
            dd.insert(list2);
            list2.clear();
        }
        
        /**
         * 链接Mongodb
         */
        public static void initMongo(){
            try {
                mg = new Mongo("localhost",27017);
                db = mg.getDB("admin");
                boolean auth = db.authenticate("aa", "aa".toCharArray());
                if(!auth){
                    System.err.println("Error");
                }else{
                    db = mg.getDB("zq1");
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
        
        public static List<Object> getlists(String strs[]){
            List<Object> list = new ArrayList<Object>();
            for(int i = 1;i < strs.length;i = i + 2){
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("z", strs[0]);
                map.put("n", strs[i]);
                map.put("t", Integer.parseInt(strs[i+1]));
                list.add(map);
            }
            return list;
        }
        /**
         *main函数入口 
         */
        public static void main(String args[]){
            initMongo();
            fileData("E:/aa", 1);
        }
    }
  • 相关阅读:
    MySQL循环插入语法
    查看linux机子的配置
    extends与implements的不区别
    Git基本命令
    VirtualBox配置 以及文件传输
    zookeeper应用场景
    oracle学习
    linux学习
    Mysql优化
    线程3
  • 原文地址:https://www.cnblogs.com/zqzdong/p/4839000.html
Copyright © 2020-2023  润新知