Kaynağa Gözat

Merge -r738476:738479 from trunk to 0.19 branch. Fixes Hadoop-4955.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/branches/branch-0.19@738485 13f79535-47bb-0310-9956-ffa450edef68
Enis Soztutar 16 yıl önce
ebeveyn
işleme
301c7e3e3e

+ 3 - 0
CHANGES.txt

@@ -70,6 +70,9 @@ Release 0.19.1 - Unreleased
     verification log unclosed. (hairong)
 
     HADOOP-5086. Use the appropriate FileSystem for trash URIs. (cdouglas)
+    
+    HADOOP-4955. Make DBOutputFormat us column names from setOutput().
+    (Kevin Peterson via enis) 
 
 Release 0.19.0 - 2008-11-18
 

+ 29 - 4
src/mapred/org/apache/hadoop/mapred/lib/db/DBOutputFormat.java

@@ -99,10 +99,31 @@ implements OutputFormat<K,V> {
 
   /**
    * Constructs the query used as the prepared statement to insert data.
+   * 
+   * @param table
+   *          the table to insert into
+   * @param fieldNames
+   *          the fields to insert into. If field names are unknown, supply an
+   *          array of nulls.
    */
   protected String constructQuery(String table, String[] fieldNames) {
+    if(fieldNames == null) {
+      throw new IllegalArgumentException("Field names may not be null");
+    }
+
     StringBuilder query = new StringBuilder();
-    query.append("INSERT INTO ").append(table);      
+    query.append("INSERT INTO ").append(table);
+
+    if (fieldNames.length > 0 && fieldNames[0] != null) {
+      query.append(" (");
+      for (int i = 0; i < fieldNames.length; i++) {
+        query.append(fieldNames[i]);
+        if (i != fieldNames.length - 1) {
+          query.append(",");
+        }
+      }
+      query.append(")");
+    }
     query.append(" VALUES (");
 
     for (int i = 0; i < fieldNames.length; i++) {
@@ -145,9 +166,13 @@ implements OutputFormat<K,V> {
   /**
    * Initializes the reduce-part of the job with the appropriate output settings
    * 
-   * @param job The job
-   * @param tableName The table to insert data into
-   * @param fieldNames The field names in the table
+   * @param job
+   *          The job
+   * @param tableName
+   *          The table to insert data into
+   * @param fieldNames
+   *          The field names in the table. If unknown, supply the appropriate
+   *          number of nulls.
    */
   public static void setOutput(JobConf job, String tableName, String... fieldNames) {
     job.setOutputFormat(DBOutputFormat.class);

+ 19 - 0
src/test/org/apache/hadoop/mapred/lib/db/TestConstructQuery.java

@@ -0,0 +1,19 @@
+package org.apache.hadoop.mapred.lib.db;
+
+import org.apache.hadoop.io.NullWritable;
+
+import junit.framework.TestCase;
+
+public class TestConstructQuery extends TestCase {
+  public void testConstructQuery() {
+    DBOutputFormat<DBWritable, NullWritable> format = new DBOutputFormat<DBWritable, NullWritable>();
+    String expected = "INSERT INTO hadoop_output (id,name,value) VALUES (?,?,?);";
+    String[] fieldNames = new String[] { "id", "name", "value" };
+    String actual = format.constructQuery("hadoop_output", fieldNames);
+    assertEquals(expected, actual);
+    expected = "INSERT INTO hadoop_output VALUES (?,?,?);";
+    fieldNames = new String[] { null, null, null };
+    actual = format.constructQuery("hadoop_output", fieldNames);
+    assertEquals(expected, actual);
+  }
+}