Advertisement
Guest User

Untitled

a guest
May 24th, 2011
2,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.77 KB | None | 0 0
  1.  
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.OutputStreamWriter;
  6. import java.io.Writer;
  7. import java.sql.PreparedStatement;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10.  
  11. import java.sql.DriverManager;
  12. import java.sql.Connection;
  13. import java.sql.SQLException;
  14.  
  15. import javax.xml.parsers.ParserConfigurationException;
  16. import javax.xml.parsers.SAXParser;
  17. import javax.xml.parsers.SAXParserFactory;
  18.  
  19. import org.xml.sax.Attributes;
  20. import org.xml.sax.SAXException;
  21. import org.xml.sax.helpers.DefaultHandler;
  22.  
  23.  
  24. public class Inserter extends DefaultHandler {
  25.  
  26.         HashMap fieldnames = new HashMap();
  27.         HashMap values = new HashMap();
  28.         String tableName = "movie";
  29.         Connection c = null;
  30.         PreparedStatement ps = null;
  31.         static final   String       sNEWLINE   = System.getProperty( "line.separator" );
  32.         static private Writer       out        = null;
  33.         private        StringBuffer textBuffer = null;
  34.         int     numErrors = 0;
  35.         long numInserts = 0;
  36.  
  37.  
  38.         public void run(String [] args) {
  39.                 SAXParser saxParser;
  40.  
  41.  
  42.                 tableName = args[0];
  43.                 String file = args[1];
  44.  
  45.  
  46.                 try {
  47.                         try {
  48.                                 Class.forName("org.postgresql.Driver");
  49.                         } catch (ClassNotFoundException cnfe) {
  50.                                 System.out.println("Couldn't find the driver!");
  51.                                 System.out.println("Let's print a stack trace, and exit.");
  52.                                 cnfe.printStackTrace();
  53.                                 System.exit(1);
  54.                         }
  55.  
  56.                         c = DriverManager.getConnection("jdbc:postgresql://hostname/databasename",
  57.                                         "xxxxx", "xxxx");
  58.                         saxParser = SAXParserFactory.newInstance().newSAXParser();
  59.                         long timePre = System.currentTimeMillis();
  60.                         saxParser.parse( new File( file ), this );
  61.                         long timeAfter = System.currentTimeMillis();
  62.                         long timeTaken = (timeAfter - timePre)/1000;
  63.                         long insertsPerSecond = numInserts/timeTaken;
  64.                         System.out.println("Errors: " + numErrors);
  65.                         System.out.println("Inserts: " + numInserts);
  66.                         System.out.println("Inserts per second: " + insertsPerSecond);
  67.                         System.out.println("Seconds taken: " + (System.currentTimeMillis() - timePre)/1000);
  68.  
  69.  
  70.                 } catch (ParserConfigurationException e) {
  71.                         // TODO Auto-generated catch block
  72.                         e.printStackTrace();
  73.                 } catch (SAXException e) {
  74.                         // TODO Auto-generated catch block
  75.                         e.printStackTrace();
  76.                 } catch (IOException e) {
  77.                         // TODO Auto-generated catch block
  78.                         e.printStackTrace();
  79.                 } catch (SQLException e) {
  80.                         // TODO Auto-generated catch block
  81.                         e.printStackTrace();
  82.                 }
  83.  
  84.         }
  85.  
  86.         public static void main(String [] args) {
  87.                 new Inserter().run(args);
  88.  
  89.         }
  90.  
  91. //      ---- SAX DefaultHandler methods ----
  92.  
  93.         public void startDocument()
  94.         throws SAXException
  95.         {
  96.                 //echoString( sNEWLINE + "<?xml ...?>" + sNEWLINE + sNEWLINE );
  97.         }
  98.  
  99.         public void endDocument()
  100.         throws SAXException
  101.         {
  102.                 echoString( sNEWLINE );
  103.         }
  104.  
  105.         public void startElement( String namespaceURI,
  106.                         String localName,   // local name
  107.                         String qName,       // qualified name
  108.                         Attributes attrs )  throws SAXException {
  109.                 echoTextBuffer();
  110.                 String eName = ( "".equals( localName ) ) ? qName : localName;
  111.  
  112.                 if(eName.equalsIgnoreCase("FIELD")) {
  113.                         fieldnames.put(attrs.getValue("FieldName"), attrs.getValue("FieldType"));
  114.                         System.out.println("Field:" + attrs.getValue("FieldName"));
  115.                 } else if(eName.equalsIgnoreCase("ROW")) {
  116.                         try {
  117.                                 // for each field prepare statement
  118.                                 Iterator itFields = fieldnames.keySet().iterator();
  119.                                 int fieldIdx = 1;
  120.                                 while(itFields.hasNext()) {
  121.                                         String fieldName = itFields.next().toString();
  122.                                         String type = fieldnames.get(fieldName).toString();
  123.                                         String value = attrs.getValue(fieldName);
  124.  
  125.                                         if(type.equalsIgnoreCase("Integer")) {
  126.                                                 try {
  127.                                                         ps.setInt(fieldIdx, Integer.parseInt(value));
  128.                                                 } catch (NumberFormatException e) {
  129.                                                         e.printStackTrace();
  130.                                                         numErrors++;
  131.                                                 }
  132.                                         } else {
  133.                                                 ps.setString(fieldIdx, value);
  134.                                         }
  135.                                         fieldIdx++;
  136.                                 } // end while
  137.  
  138.                                 ps.execute();
  139.                                 if(numInserts % 50000 == 0) {
  140.                                         System.out.println(numInserts + " inserts made.");
  141.                                 }
  142.                                 numInserts++;
  143.  
  144.                                 ps.clearParameters();
  145.                         } catch (SQLException e) {
  146.                                 numErrors++;
  147.                                 e.printStackTrace();
  148.                         }
  149.                 }
  150.  
  151.         }
  152.  
  153.         public void endElement( String namespaceURI,
  154.                         String localName,     // local name
  155.                         String qName )        // qualified name
  156.         throws SAXException
  157.         {
  158.  
  159.                 String eName = ( "".equals( localName ) ) ? qName : localName;
  160.                 if(eName.equalsIgnoreCase("METADATA")) {
  161.                         // build sql
  162.                         StringBuffer buf = new StringBuffer();
  163.                         for(int i = 0; i < fieldnames.size(); i++) {
  164.                                 buf.append("?");
  165.                                 if(i < (fieldnames.size()-1)) {
  166.                                         buf.append(",");
  167.                                 }
  168.                         }
  169.  
  170.  
  171.                         try {
  172.                                 ps = c.prepareStatement("INSERT INTO " + tableName + " VALUES (" + buf.toString() + ")");
  173.                                 System.out.println("INSERT INTO " + tableName + " VALUES (" + buf.toString() + ")");
  174.                         } catch (SQLException e) {
  175.                                 // TODO Auto-generated catch block
  176.                                 e.printStackTrace();
  177.                         }
  178.  
  179.                 }
  180.  
  181.  
  182. //              echoTextBuffer();
  183.  
  184. //              echoString( "</" + eName + ">" );           // element name
  185.         }
  186.  
  187.         public void characters( char[] buf, int offset, int len )
  188.         throws SAXException
  189.         {
  190. //              String s = new String( buf, offset, len );
  191. //              if( textBuffer == null )
  192. //              textBuffer = new StringBuffer( s );
  193. //              else
  194. //              textBuffer.append( s );
  195.         }
  196.  
  197.         // ---- Helper methods ----
  198.  
  199.         // Display text accumulated in the character buffer
  200.         private void echoTextBuffer()
  201.         throws SAXException
  202.         {
  203.                 if( textBuffer == null )  return;
  204.                 echoString( textBuffer.toString() );
  205.                 textBuffer = null;
  206.         }
  207.  
  208.         // Wrap I/O exceptions in SAX exceptions, to
  209.         // suit handler signature requirements
  210.         private void echoString( String s )
  211.         throws SAXException
  212.         {
  213.                 try {
  214.                         if( null == out )
  215.                                 out = new OutputStreamWriter( System.out, "UTF8" );
  216.                         out.write( s );
  217.                         out.flush();
  218.                 } catch( IOException ex ) {
  219.                         throw new SAXException( "I/O error", ex );
  220.                 }
  221.         }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement