JAVA-导数到Mongodb (Mongodb中有List集合)
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(" "); List<Object> list2 = getlists(strs); basi.put("_id",strs[0]); basi.put("p", j); basi.put("l", list2); list.add(basi); if(num % 100000 == 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("root123", "test123".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); } }