You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by en...@apache.org on 2009/01/28 14:28:06 UTC
svn commit: r738477 - in /hadoop/core/trunk: CHANGES.txt
src/mapred/org/apache/hadoop/mapred/lib/db/DBOutputFormat.java
src/test/org/apache/hadoop/mapred/lib/db/TestConstructQuery.java
Author: enis
Date: Wed Jan 28 13:28:06 2009
New Revision: 738477
URL: http://svn.apache.org/viewvc?rev=738477&view=rev
Log:
HADOOP-4955. Make DBOutputFormat us column names from setOutput(). Contributed by Kevin Peterson.
Added:
hadoop/core/trunk/src/test/org/apache/hadoop/mapred/lib/db/TestConstructQuery.java
Modified:
hadoop/core/trunk/CHANGES.txt
hadoop/core/trunk/src/mapred/org/apache/hadoop/mapred/lib/db/DBOutputFormat.java
Modified: hadoop/core/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/CHANGES.txt?rev=738477&r1=738476&r2=738477&view=diff
==============================================================================
--- hadoop/core/trunk/CHANGES.txt (original)
+++ hadoop/core/trunk/CHANGES.txt Wed Jan 28 13:28:06 2009
@@ -68,7 +68,9 @@
HADOOP-5079. HashFunction inadvertently destroys some randomness
(Jonathan Ellis via stack)
-
+
+ HADOOP-4955. Make DBOutputFormat us column names from setOutput().
+ (Kevin Peterson via enis)
Release 0.20.0 - Unreleased
Modified: hadoop/core/trunk/src/mapred/org/apache/hadoop/mapred/lib/db/DBOutputFormat.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/mapred/org/apache/hadoop/mapred/lib/db/DBOutputFormat.java?rev=738477&r1=738476&r2=738477&view=diff
==============================================================================
--- hadoop/core/trunk/src/mapred/org/apache/hadoop/mapred/lib/db/DBOutputFormat.java (original)
+++ hadoop/core/trunk/src/mapred/org/apache/hadoop/mapred/lib/db/DBOutputFormat.java Wed Jan 28 13:28:06 2009
@@ -99,10 +99,31 @@
/**
* 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 @@
/**
* 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);
Added: hadoop/core/trunk/src/test/org/apache/hadoop/mapred/lib/db/TestConstructQuery.java
URL: http://svn.apache.org/viewvc/hadoop/core/trunk/src/test/org/apache/hadoop/mapred/lib/db/TestConstructQuery.java?rev=738477&view=auto
==============================================================================
--- hadoop/core/trunk/src/test/org/apache/hadoop/mapred/lib/db/TestConstructQuery.java (added)
+++ hadoop/core/trunk/src/test/org/apache/hadoop/mapred/lib/db/TestConstructQuery.java Wed Jan 28 13:28:06 2009
@@ -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);
+ }
+}