java往MongDB匯入大量資料,javamongdb匯入

來源:互聯網
上載者:User

java往MongDB匯入大量資料,javamongdb匯入

好幾月沒寫部落格了~~~

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

在公司最近在搞用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方法尋找document47                     //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方法尋找document16             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連結呢

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.