Return-Path: Delivered-To: apmail-hadoop-chukwa-commits-archive@minotaur.apache.org Received: (qmail 47002 invoked from network); 13 Apr 2009 20:10:02 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 13 Apr 2009 20:10:02 -0000 Received: (qmail 74985 invoked by uid 500); 13 Apr 2009 20:10:02 -0000 Delivered-To: apmail-hadoop-chukwa-commits-archive@hadoop.apache.org Received: (qmail 74974 invoked by uid 500); 13 Apr 2009 20:10:02 -0000 Mailing-List: contact chukwa-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: chukwa-dev@hadoop.apache.org Delivered-To: mailing list chukwa-commits@hadoop.apache.org Received: (qmail 74964 invoked by uid 99); 13 Apr 2009 20:10:02 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 13 Apr 2009 20:10:02 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 13 Apr 2009 20:10:00 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id F3D75238893B; Mon, 13 Apr 2009 20:09:38 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r764598 - /hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/extraction/database/MetricDataLoader.java Date: Mon, 13 Apr 2009 20:09:38 -0000 To: chukwa-commits@hadoop.apache.org From: eyang@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090413200938.F3D75238893B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: eyang Date: Mon Apr 13 20:09:38 2009 New Revision: 764598 URL: http://svn.apache.org/viewvc?rev=764598&view=rev Log: CHUKWA-124. Fixed JDBC connection initialization and closing. Added escapeQuotes utility method. (Eric Yang) Modified: hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/extraction/database/MetricDataLoader.java Modified: hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/extraction/database/MetricDataLoader.java URL: http://svn.apache.org/viewvc/hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/extraction/database/MetricDataLoader.java?rev=764598&r1=764597&r2=764598&view=diff ============================================================================== --- hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/extraction/database/MetricDataLoader.java (original) +++ hadoop/chukwa/trunk/src/java/org/apache/hadoop/chukwa/extraction/database/MetricDataLoader.java Mon Apr 13 20:09:38 2009 @@ -66,6 +66,7 @@ private static ChukwaConfiguration conf = null; private static FileSystem fs = null; + private String jdbc_url = ""; static { conf = new ChukwaConfiguration(); @@ -107,49 +108,32 @@ } } } - String jdbc_url = ""; log.debug("cluster name:" + cluster); if (!cluster.equals("")) { ClusterConfig cc = new ClusterConfig(); jdbc_url = cc.getURL(cluster); } - try { - // The newInstance() call is a work around for some - // broken Java implementations - org.apache.hadoop.chukwa.util.DriverManagerUtil.loadDriver().newInstance(); - log.debug("Initialized JDBC URL: " + jdbc_url); - } catch (Exception ex) { - // handle the error - log.error(ex, ex); - } - try { - conn = org.apache.hadoop.chukwa.util.DriverManagerUtil.getConnection(jdbc_url); - HashMap dbNames = mdlConfig.startWith("report.db.name."); - Iterator ki = dbNames.keySet().iterator(); - dbSchema = new HashMap>(); - DatabaseWriter dbWriter = new DatabaseWriter(cluster); - while (ki.hasNext()) { - String table = dbNames.get(ki.next().toString()); - String query = "select * from " + table + "_template limit 1"; - try { - ResultSet rs = dbWriter.query(query); - ResultSetMetaData rmeta = rs.getMetaData(); - HashMap tableSchema = new HashMap(); - for (int i = 1; i <= rmeta.getColumnCount(); i++) { - tableSchema.put(rmeta.getColumnName(i), rmeta.getColumnType(i)); - } - dbSchema.put(table, tableSchema); - } catch (SQLException ex) { - log - .debug("table: " - + table - + " template does not exist, MDL will not load data for this table."); + HashMap dbNames = mdlConfig.startWith("report.db.name."); + Iterator ki = dbNames.keySet().iterator(); + dbSchema = new HashMap>(); + DatabaseWriter dbWriter = new DatabaseWriter(cluster); + while (ki.hasNext()) { + String table = dbNames.get(ki.next().toString()); + String query = "select * from " + table + "_template limit 1"; + try { + ResultSet rs = dbWriter.query(query); + ResultSetMetaData rmeta = rs.getMetaData(); + HashMap tableSchema = new HashMap(); + for (int i = 1; i <= rmeta.getColumnCount(); i++) { + tableSchema.put(rmeta.getColumnName(i), rmeta.getColumnType(i)); } + dbSchema.put(table, tableSchema); + } catch (SQLException ex) { + log.debug("table: " + table + + " template does not exist, MDL will not load data for this table."); } - dbWriter.close(); - } catch (SQLException ex) { - log.error(ex, ex); } + dbWriter.close(); } public void interrupt() { @@ -166,6 +150,23 @@ } + public static String escapeQuotes( String s ) { + StringBuffer sb = new StringBuffer(); + int index; + int length = s.length(); + char ch; + for( index = 0; index < length; ++index ) { + if(( ch = s.charAt( index )) == '\"' ) { + sb.append( "\\\"" ); + } else if( ch == '\\' ) { + sb.append( "\\\\" ); + } else { + sb.append( ch ); + } + } + return( sb.toString()); + } + public void process(Path source) throws IOException, URISyntaxException, SQLException { @@ -173,6 +174,16 @@ SequenceFile.Reader r = new SequenceFile.Reader(fs, source, conf); + try { + // The newInstance() call is a work around for some + // broken Java implementations + org.apache.hadoop.chukwa.util.DriverManagerUtil.loadDriver().newInstance(); + log.debug("Initialized JDBC URL: " + jdbc_url); + } catch (Exception ex) { + // handle the error + log.error(ex, ex); + } + conn = org.apache.hadoop.chukwa.util.DriverManagerUtil.getConnection(jdbc_url); stmt = conn.createStatement(); conn.setAutoCommit(false); long currentTimeMillis = System.currentTimeMillis(); @@ -322,10 +333,9 @@ sqlValues.append(conversion.get(conversionKey).toString()); } else { sqlValues.append(transformer.get(fieldKey)); - sqlValues.append("=\""); - sqlValues.append(recordSet.get(fieldKey).replaceAll("\"", - "'")); - sqlValues.append("\""); + sqlValues.append("=\'"); + sqlValues.append(escapeQuotes(recordSet.get(fieldKey))); + sqlValues.append("\'"); } } else if (dbSchema.get(dbTables.get(dbKey)).get( transformer.get(fieldKey)) == java.sql.Types.TIMESTAMP) { @@ -385,10 +395,9 @@ sqlValues.append(conversion.get(conversionKey).toString()); } else { sqlValues.append(transformer.get(fieldKey)); - sqlValues.append("=\""); - sqlValues.append(recordSet.get(fieldKey) - .replaceAll("\"", "'")); - sqlValues.append("\""); + sqlValues.append("='"); + sqlValues.append(escapeQuotes(recordSet.get(fieldKey))); + sqlValues.append("'"); } firstValue = false; } catch (NullPointerException ex) { @@ -450,7 +459,14 @@ e.printStackTrace(); } finally { if (batchMode) { - conn.commit(); + try { + conn.commit(); + } catch (SQLException ex) { + log.error(ex, ex); + log.error("SQLException: " + ex.getMessage()); + log.error("SQLState: " + ex.getSQLState()); + log.error("VendorError: " + ex.getErrorCode()); + } } long latencyMillis = System.currentTimeMillis() - currentTimeMillis; int latencySeconds = ((int) (latencyMillis + 500)) / 1000; @@ -479,6 +495,17 @@ } stmt = null; } + if (conn != null) { + try { + conn.close(); + } catch (SQLException ex) { + log.error(ex, ex); + log.error("SQLException: " + ex.getMessage()); + log.error("SQLState: " + ex.getSQLState()); + log.error("VendorError: " + ex.getErrorCode()); + } + conn = null; + } } }