Java imports a large amount of data to MongDB, and javamongdb imports

Source: Internet
Author: User

Java imports a large amount of data to MongDB, and javamongdb imports

I haven't written a blog for several months ~~~

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

I recently used java to import data to MongDB. Now I just imported million pieces of data, so I should write it first.

MongDB on my local machine, I have not imported MongDB to the server. Only URL links are available.

First of all, it is faster to import a single file.

1 void testPost (String urlStr) {2 try {3 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 // instantiate a Mongo object, connect Port 27017 10 Mongo mongo = new Mongo ("the IP address from which you want to read data", 27017); 11 // The connection name is yourdb If the database does not exist, mongodb automatically creates 12 DB = mongo. getDB ("yourdb"); 13 14 // Get collection from MongoDB, database named "yourDB" 15 // obtain a data set named yourColleection from Mongodb. If the data set does not exist, mongodb will create a new 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. getBy ("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 // use the find method of collection to find document47 // DBCursor cursor = collection. find (searchQuery); 48 // loop output result 49 System. out. println (I ++ "bar -----" + collection. count (); 50} 51} catch (MalformedURLException e) {52 e. printStackTrace (); 53} catch (IOException e) {54 e. printStackTrace (); 55} 56} 57 // XML file configuration does not actually need 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> here you need to write the model of the data you want to import in English </StaticType>"); 66 sb. append ("</GetStaticInformationRQ>"); 67 return sb. toString (); 68} 69 70 public static void main (String [] args) {71 String url = "here is your URL link address. You need to read data from this URL"; 72 new HttpPostTest (). testPost (url); 73}
Import

The imported data is still being imported.

 

A query is provided.

1 void testPost (String urlStr) {2 try {3 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 = get XmlInfo (); 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 ++ "bar ----" + 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> Custom image </StaticType>"); 39 sb. append ("</GetStaticInformationRQ>"); 40 return sb. toString (); 41} 42 43 public static void main (String [] args) {44 String url = "URL url You queried"; 45 new HttpPostTest (). testPost (url); 46}
Query

 

A query database is provided

1 public static void main (String [] args) {2 try {3 // instantiate the Mongo object and connect to port 4 Mongo mongo = new Mongo ("imported IP Address ", 27017); 5 // connect to the database named yourdb. If the database does not exist, mongodb automatically creates 6 DB = mongo. getDB ("yourdb"); 7 8 // Get collection from MongoDB, database named "yourDB" 9 // obtain a data set named yourColleection from Mongodb. If the data set does not exist, mongodb will create a 10 DBCollection collection = db for its new database. getCollection ("dl_hotelimage"); 11 // use BasicDBObj The ect object creates a document for mongodb and assigns a value to it. 12 13 // create the document 14 BasicDBObject searchQuery = new BasicDBObject (); 15 // use the find method of collection to find document16 DBCursor cursor = collection. find (searchQuery); 17 // loop output result 18 int I = 0; 19 while (cursor. hasNext () {20 System. out. println (I ++ "," + cursor. next (); 21} 22 System. out. println ("Done"); 23} catch (except exception e) {24 e. printStackTrace (); 25} 26 27 28}
Query a database

Okay. I have not installed the IP address and URL link for the MongDB database on my local machine.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.