sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jar...@apache.org
Subject svn commit: r1351062 - in /sqoop/trunk/src: java/org/apache/sqoop/tool/ExportTool.java test/com/cloudera/sqoop/TestExport.java
Date Sun, 17 Jun 2012 06:49:06 GMT
Author: jarcec
Date: Sun Jun 17 06:49:06 2012
New Revision: 1351062

URL: http://svn.apache.org/viewvc?rev=1351062&view=rev
Log:
SQOOP-503: --columns argument support for export.

(Cheolsoo Park via Jarek Jarcec Cecho)

Modified:
    sqoop/trunk/src/java/org/apache/sqoop/tool/ExportTool.java
    sqoop/trunk/src/test/com/cloudera/sqoop/TestExport.java

Modified: sqoop/trunk/src/java/org/apache/sqoop/tool/ExportTool.java
URL: http://svn.apache.org/viewvc/sqoop/trunk/src/java/org/apache/sqoop/tool/ExportTool.java?rev=1351062&r1=1351061&r2=1351062&view=diff
==============================================================================
--- sqoop/trunk/src/java/org/apache/sqoop/tool/ExportTool.java (original)
+++ sqoop/trunk/src/java/org/apache/sqoop/tool/ExportTool.java Sun Jun 17 06:49:06 2012
@@ -133,6 +133,10 @@ public class ExportTool extends com.clou
         .hasArg().withDescription("Table to populate")
         .withLongOpt(TABLE_ARG)
         .create());
+    exportOpts.addOption(OptionBuilder.withArgName("col,col,col...")
+        .hasArg().withDescription("Columns to export to table")
+        .withLongOpt(COLUMNS_ARG)
+        .create());
     exportOpts.addOption(OptionBuilder.withArgName("n")
         .hasArg().withDescription("Use 'n' map tasks to export in parallel")
         .withLongOpt(NUM_MAPPERS_ARG)
@@ -227,6 +231,14 @@ public class ExportTool extends com.clou
         out.setTableName(in.getOptionValue(TABLE_ARG));
       }
 
+      if (in.hasOption(COLUMNS_ARG)) {
+        String[] cols= in.getOptionValue(COLUMNS_ARG).split(",");
+        for (int i=0; i<cols.length; i++) {
+          cols[i] = cols[i].trim();
+        }
+        out.setColumns(cols);
+      }
+
       if (in.hasOption(NUM_MAPPERS_ARG)) {
         out.setNumMappers(Integer.parseInt(in.getOptionValue(NUM_MAPPERS_ARG)));
       }

Modified: sqoop/trunk/src/test/com/cloudera/sqoop/TestExport.java
URL: http://svn.apache.org/viewvc/sqoop/trunk/src/test/com/cloudera/sqoop/TestExport.java?rev=1351062&r1=1351061&r2=1351062&view=diff
==============================================================================
--- sqoop/trunk/src/test/com/cloudera/sqoop/TestExport.java (original)
+++ sqoop/trunk/src/test/com/cloudera/sqoop/TestExport.java Sun Jun 17 06:49:06 2012
@@ -793,4 +793,63 @@ public class TestExport extends ExportJo
     assertColMinAndMax(forIdx(0), genFloat);
     assertColMinAndMax(forIdx(1), genNumeric);
   }
+
+  public void testColumnsExport() throws IOException, SQLException {
+    final int TOTAL_COLUMNS = 3;
+    final int TOTAL_RECORDS = 10;
+
+    // This class is used to generate a column whose entries have unique
+    // sequential values that are computed as follows:
+    //  (row number * total number of columns) + column number
+    class MultiColumnGenerator implements ColumnGenerator {
+      private int col;
+      MultiColumnGenerator(int col) {
+        this.col = col;
+      }
+      public String getExportText(int rowNum) {
+        return new Integer(rowNum * TOTAL_COLUMNS + col).toString();
+      }
+      public String getVerifyText(int rowNum) {
+        return new Integer(rowNum * TOTAL_COLUMNS + col).toString();
+      }
+      public String getType() {
+        return "INTEGER";
+      }
+    }
+
+    // This class is used to generate a null column.
+    class NullColumnGenerator implements ColumnGenerator {
+      public String getExportText(int rowNum) {
+        return null;
+      }
+      public String getVerifyText(int rowNum) {
+        return null;
+      }
+      public String getType() {
+        return "INTEGER";
+      }
+    }
+
+    // Generate an input file that only contains partial columns: col0
+    // and col2. Export these columns to the DB w/ the --columns option.
+    // Finally, verify that these two columns are exported properly. In
+    // addition, verify that the values of col1 in the DB are all null
+    // as they are not exported.
+    ColumnGenerator gen0 = new MultiColumnGenerator(0);
+    ColumnGenerator gen1 = new MultiColumnGenerator(1);
+    ColumnGenerator gen2 = new MultiColumnGenerator(2);
+
+    createTextFile(0, TOTAL_RECORDS, false, gen0, gen2);
+    createTable(gen0, gen1, gen2);
+
+    String columnsStr = "id,msg," + forIdx(0) + "," + forIdx(2);
+    runExport(getArgv(true, 10, 10, "--columns", columnsStr));
+
+    ColumnGenerator genNull = new NullColumnGenerator();
+
+    verifyExport(TOTAL_RECORDS);
+    assertColMinAndMax(forIdx(0), gen0);
+    assertColMinAndMax(forIdx(2), gen2);
+    assertColMinAndMax(forIdx(1), genNull);
+  }
 }



Mime
View raw message