java往MongDB導入大量數據


好幾月沒寫博客了~~~

---------------------

在公司最近在搞用java往MongDB導入數據 現在是我剛導入2000W條數據了 所以就先寫上吧,廢話也不多說了

MongDB 我本機上沒有  我往服務器里面的MongDB導入的 只有URL鏈接

首先是導入 我是不是用的批量導入   用的是單條導入    比較快   

 1  void testPost(String urlStr) {  
 2         try {  
 3             URL url = new URL(urlStr);  
 4             URLConnection con = url.openConnection();  
 5             con.setDoOutput(true);  
 6             con.setRequestProperty("Pragma:", "no-cache");  
 7             con.setRequestProperty("Cache-Control", "no-cache");  
 8             con.setRequestProperty("Content-Type", "text/xml");  
 9           //實例化Mongo對象,連接27017端口           
10             Mongo mongo = new Mongo("這里寫IP地址你需要從哪個IP里讀取數據的IP地址", 27017); 
11             //連接名為yourdb的數據庫,假如數據庫不存在的話,mongodb會自動建立           
12             DB db = mongo.getDB("yourdb");
13             
14             // Get collection from MongoDB, database named "yourDB"
15             //從Mongodb中獲得名為yourColleection的數據集合,如果該數據集合不存在,Mongodb會為其新建立 
16             DBCollection collection = db.getCollection("dl_hotelimage");
17             OutputStreamWriter out = new OutputStreamWriter(con  
18                     .getOutputStream());      
19             String xmlInfo = getXmlInfo();  
20             out.write(new String(xmlInfo.getBytes("UTF-8")));
21             out.flush();  
22             out.close();  
23             BufferedReader br = new BufferedReader(new InputStreamReader(con  
24                     .getInputStream()));   
25             String line = ""; 
26             int i =1;
27             BasicDBObject document=new BasicDBObject();
28             for (line = br.readLine(); line != null; line = br.readLine()) {  
29                     String[] str=line.split("\\|",-1);
30                     document = new BasicDBObject();
31                         if(str.length!=0){
32                             document.put("id", str[0]);
33                         }
34                         if(str.length!=1){
35                             document.put("ImageCaption", str[1]);
36                         }
37                         if(str.length!=2){
38                             document.put("ImageUrl", str[2]);
39                         } 
40                         if(str.length!=3){
41                             document.put("ImageOrder", str[3]);
42                         }
43                     collection.save(document);
44                     //BasicDBObject searchQuery = new BasicDBObject();
45                     //searchQuery.put("id", str[0]);
46                     // 使用collection的find方法查找document
47                     //DBCursor cursor = collection.find(searchQuery);
48                     //循環輸出結果            
49                     System.out.println(i+++"條-----"+collection.count()); 
50                 }
51         } catch (MalformedURLException e) {  
52             e.printStackTrace();  
53         } catch (IOException e) {  
54             e.printStackTrace();    
55         }
56     }
57   //XML文件的配置 其實不用XML文件的
58     private String getXmlInfo() {  
59         StringBuilder sb = new StringBuilder();  
60         sb.append("<GetStaticInformationRQ>");
61         sb.append("<Header>");
62         sb.append("<ClientID>BJMAIGESHI_API</ClientID>");
63         sb.append("<LicenseKey>BJMAIGESHI_API</LicenseKey>");
64         sb.append("</Header>");
65         sb.append("<StaticType>這里需要你寫你要導入哪些數據的型號英文</StaticType>");
66         sb.append("</GetStaticInformationRQ>");
67         return sb.toString();
68     }  
69   
70     public static void main(String[] args) {  
71         String url = "這里就是你的URL鏈接地址了需要從這個URL里讀取數據";
72         new HttpPostTest().testPost(url);
73     }
導入

這個是我已經導入的數據 目前還在導入

 

附贈你們一個查詢

 1  void testPost(String urlStr) {  
 2         try {  
 3             URL url = new URL(urlStr);  
 4             URLConnection con = url.openConnection();  
 5             con.setDoOutput(true);  
 6             con.setRequestProperty("Pragma:", "no-cache");  
 7             con.setRequestProperty("Cache-Control", "no-cache");  
 8             con.setRequestProperty("Content-Type", "text/xml");  
 9   
10             OutputStreamWriter out = new OutputStreamWriter(con  
11                     .getOutputStream());      
12             String xmlInfo = getXmlInfo();  
13             out.write(new String(xmlInfo.getBytes("ISO-8859-1")));
14             out.flush();  
15             out.close();  
16             BufferedReader br = new BufferedReader(new InputStreamReader(con  
17                     .getInputStream()));  
18             String line = "";  
19             int i=0;
20             for (line = br.readLine(); line != null; line = br.readLine()) {  
21                 String str[]=line.split("\\|+");
22                 System.out.println(i+++"條----"+line);
23             }  
24         } catch (MalformedURLException e) {  
25             e.printStackTrace();  
26         } catch (IOException e) {  
27             e.printStackTrace();
28         }  
29     }  
30   
31     private String getXmlInfo() {  
32         StringBuilder sb = new StringBuilder();
33         sb.append("<GetStaticInformationRQ>");
34         sb.append("<Header>");
35         sb.append("<ClientID>BJMAIGESHI_API</ClientID>");
36         sb.append("<LicenseKey>BJMAIGESHI_API</LicenseKey>");
37         sb.append("</Header>");
38         sb.append("<StaticType>HotelImage</StaticType>");
39         sb.append("</GetStaticInformationRQ>");
40         return sb.toString();
41     }  
42   
43     public static void main(String[] args) {  
44         String url = "URL鏈接地址  你查詢的";
45         new HttpPostTest().testPost(url);  
46     }  
查詢

 

在附贈一個查詢數據庫

 1 public static void main(String[] args) {         
 2         try {            
 3             //實例化Mongo對象,連接27017端口           
 4             Mongo mongo = new Mongo("導入的IP地址", 27017); 
 5             //連接名為yourdb的數據庫,假如數據庫不存在的話,mongodb會自動建立           
 6             DB db = mongo.getDB("yourdb");
 7             
 8             // Get collection from MongoDB, database named "yourDB"
 9             //從Mongodb中獲得名為yourColleection的數據集合,如果該數據集合不存在,Mongodb會為其新建立 
10             DBCollection collection = db.getCollection("dl_hotelimage");
11             // 使用BasicDBObject對象創建一個mongodb的document,並給予賦值。
12                      
13             // 創建要查詢的document            
14             BasicDBObject searchQuery = new BasicDBObject();
15             // 使用collection的find方法查找document
16             DBCursor cursor = collection.find(searchQuery);
17             //循環輸出結果            
18             int i=0;
19             while (cursor.hasNext()) {
20                 System.out.println(i+++","+cursor.next());             
21             }             
22                 System.out.println("Done");          
23             } catch (MongoException e) {             
24                 e.printStackTrace();         
25             }    
26         
27         
28         }
查詢數據庫

好了  我本機上沒有安裝MongDB數據庫用的IP地址和URL鏈接呢

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM