Oracle Massive data import MongoDB uses Java reflection-penghao

Source: Internet
Author: User
Tags object object

First of all the personnel table data has a total of 100W, the beginning of the first is to use PL/SQL export to XML format, and then parse XML in sax, later found that XML more than 10W when parsing will be problematic, especially CPU resources, and then use Java program directly connected to Oracle and MONGO , the table has more than 50 full-size letters, if you first get the column name and value of the Oracle table, and then write the condition to determine if (column name = = Class Property) {New instance assignment}, to write more than 50 if code block. Think it's a little more concise with reflection.


Patmaster The Name field in the entity class

String Name;public string GetNAME () {return NAME;} public void SetNAME (String name) {name = name;}

class<patmaster> C = patmaster.class;

Patmaster patmaster = C.newinstance ();

Method m1 = C.getdeclaredmethod ("Set" + column, String.class);
M1.setaccessible (TRUE);
M1.invoke (patmaster, value);

column is the field columns obtained by Oracle, String.class means that the set method needs to pass in a String, then M1 gets to the set method, if it is not the public method, it needs to setaccessible (true); Then invoke the value of the field in the instance and database.


All code: Directly copy the past can be used, you change the IP definition of your entity class just fine, if you can't add me QQ468165108

Jar Package used: Ojdbc14.jar,mongo-2.5.3.jar

Jar:http://pan.baidu.com/s/1ktgnvoj Password: 69FZ,

public void Odbctomongo () throws Exception {Mongo Mongo = new Mongo ("192.168.1.3", 30000);D b DB = mongo.getdb ("test");d B.A Uthenticate ("User", "User". ToCharArray ());D bcollection DBC = db.getcollection ("Patmasterindex");// Custom data dictionary Fieldfutils fu = new fieldfutils (); hashmap<string, string> map = Fu. Getminfield (); Class.forName ("Oracle.jdbc.driver.OracleDriver"); String url = "JDBC:ORACLE:THIN:@192.168.1.111:1521:DBA"; String user = "User"; String password = "pwd"; Connection conn = drivermanager.getconnection (URL, user, password); PreparedStatement PS = conn.preparestatement ("SELECT * from Pat_master_index"); ResultSet rs = ps.executequery (); int num = 0; ResultSetMetaData RSMD = Rs.getmetadata (); int colcount = Rsmd.getcolumncount (); while (Rs.next ()) {//Counter imports 1W rows per input num++ ; if (num%10000==0) {System.out.println (num);} class<patmaster> C = patmaster.class; Patmaster patmaster = C.newinstance (); basicdbobject bdo = new Basicdbobject (); for (int i = 1; I <= colcount; i++) {String column = Rsmd.getColumnName (i); object object = Rs.getobject (i); String value = ""; if (object = = null) {} else {value = (String) object.tostring ();} Method m1 = C.getdeclaredmethod ("Set" + column, String.class); m1.setaccessible (true); M1.invoke (patmaster, value); String minname = map.get (column); Bdo.put (minname, value);} Dbc.insert (BDO);} Rs.close ();p s.close (); Conn.close ();}
}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Oracle Massive data import MongoDB uses Java reflection-penghao

Related Article

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.