zoukankan      html  css  js  c++  java
  • JAVA--导数到Mongodb

    package com.xinsight.server;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.UnknownHostException;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.mongodb.BasicDBObject;
    import com.mongodb.DB;
    import com.mongodb.DBCollection;
    import com.mongodb.DBObject;
    import com.mongodb.Mongo;
    /**
     *将文件中的数据导入到Mongodb中 
     */
    public class InputMongodbServer {
        private static Mongo mg ;
        private static DB db;
        private static int a = 12;
        public static void main(String args[]){
            String path = "E:/a.txt";
            initMongo();
            findData(path);
        }
        
        /**
         *读取数据并导数 
         */
        public static void findData(String path){
            List<BasicDBObject> list = new ArrayList<BasicDBObject>();
            File file = new File(path);
            if(!file.exists()){
                System.err.println("文件不存在");
            }else{
                try {
                    int num = 0;
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    for(String line = new String(br.readLine().getBytes());line != null;line = br.readLine()){
                        num++;
                        System.out.println(line);
                        BasicDBObject basi = new BasicDBObject();
                        String strs[] =line.split("    ");
                        basi.put("name", strs[0]);
                        basi.put("age", strs[1]);
                        basi.put("sex",strs[2]);
                        list.add(basi);
                        if(num % 100000 == 0){
                            insertDBObj(list);
                            list.clear();
                        }
                    }
                    insertDBObj(list);
                    list.clear();
                    System.out.println("导数完成!");
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        /**
         * 导数到数据库
         */
        public static void insertAll(List<BasicDBObject> list){
            List<DBObject> list2 = new ArrayList<DBObject>();
            for(int i = 0;i < list.size();i++){
                DBObject obj = list.get(i);
                list2.add(obj);
            }
            DBCollection dc = db.getCollection("zq2");
            dc.insert(list2);
        }
        
        public static List<BasicDBObject> list2 = new ArrayList<BasicDBObject>();
        /**
         *设置_id为自增 
         */
        public static void insertDBObj(List<BasicDBObject> list){
            for(int i = 0;i < list.size();i++){
                BasicDBObject basi2 = list.get(i);
                basi2.put("_id", a++);
                list2.add(basi2);
            }
            insertAll(list2);
        }
        
        /**
         * 链接Mongodb数据库
         */
        public static void initMongo(){
            try {
                mg = new Mongo("localhost",27017);
                db = mg.getDB("admin");
                boolean auth = db.authenticate("aa", "aa".toCharArray());
                if(auth){
                    db = mg.getDB("zq1");
                    
                }else{
                    System.err.println("链接MongoDB失败");
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    某直聘python岗位 Python爬虫
    出生率创40年来最低,人口几近负增长,微博爬虫爬评论
    关于jieba分词 Python
    爬取某东商品数据 Python基于selenium
    Putty连Ubuntu
    下载些音乐 Python
    抓一下严禁失德艺人曲线复出的评论 Python
    几种方式保存爬虫爬取的数据 Python
    冬天来了,用Python看看哪里的人最喜欢吃火锅
    Pyecharts绘制精美图标并做成可视化大屏 Python
  • 原文地址:https://www.cnblogs.com/zqzdong/p/4838861.html
Copyright © 2011-2022 走看看