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(); } } }