好几月没写博客了~~~

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

在公司最近在搞用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链接呢