sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kathl...@apache.org
Subject git commit: SQOOP-912: Add ability to specify --hive-database parameter
Date Mon, 15 Apr 2013 16:45:41 GMT
Updated Branches:
  refs/heads/trunk c4b0eac2d -> 8b3742ad6


SQOOP-912: Add ability to specify --hive-database parameter

(Jarek Jarcec Cecho via Kate Ting)


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/8b3742ad
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/8b3742ad
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/8b3742ad

Branch: refs/heads/trunk
Commit: 8b3742ad69ad8dfea30c692cf9e8f78192f91890
Parents: c4b0eac
Author: Kate Ting <kathleen@apache.org>
Authored: Mon Apr 15 12:44:32 2013 -0400
Committer: Kate Ting <kathleen@apache.org>
Committed: Mon Apr 15 12:44:32 2013 -0400

----------------------------------------------------------------------
 src/java/org/apache/sqoop/SqoopOptions.java        |    9 ++++++
 src/java/org/apache/sqoop/hive/TableDefWriter.java |   11 ++++++-
 src/java/org/apache/sqoop/tool/BaseSqoopTool.java  |   10 +++++++
 .../cloudera/sqoop/hive/TestTableDefWriter.java    |   22 +++++++++++++++
 4 files changed, 50 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/8b3742ad/src/java/org/apache/sqoop/SqoopOptions.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/sqoop/SqoopOptions.java b/src/java/org/apache/sqoop/SqoopOptions.java
index f423c0f..f18d43e 100644
--- a/src/java/org/apache/sqoop/SqoopOptions.java
+++ b/src/java/org/apache/sqoop/SqoopOptions.java
@@ -145,6 +145,7 @@ public class SqoopOptions implements Cloneable {
   @StoredAsProperty("hive.fail.table.exists")
   private boolean failIfHiveTableExists;
   @StoredAsProperty("hive.table.name") private String hiveTableName;
+  @StoredAsProperty("hive.database.name") private String hiveDatabaseName;
   @StoredAsProperty("hive.drop.delims") private boolean hiveDropDelims;
   @StoredAsProperty("hive.delims.replacement")
   private String hiveDelimsReplacement;
@@ -1618,6 +1619,14 @@ public class SqoopOptions implements Cloneable {
     this.hiveTableName = name;
   }
 
+  public String getHiveDatabaseName() {
+    return this.hiveDatabaseName;
+  }
+
+  public void setHiveDatabaseName(String name) {
+    this.hiveDatabaseName = name;
+  }
+
   public String getHivePartitionKey() {
     return hivePartitionKey;
   }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/8b3742ad/src/java/org/apache/sqoop/hive/TableDefWriter.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/sqoop/hive/TableDefWriter.java b/src/java/org/apache/sqoop/hive/TableDefWriter.java
index 3563c67..c9962e9 100644
--- a/src/java/org/apache/sqoop/hive/TableDefWriter.java
+++ b/src/java/org/apache/sqoop/hive/TableDefWriter.java
@@ -132,12 +132,16 @@ public class TableDefWriter {
     String [] colNames = getColumnNames();
     StringBuilder sb = new StringBuilder();
     if (options.doFailIfHiveTableExists()) {
-      sb.append("CREATE TABLE `").append(outputTableName).append("` ( ");
+      sb.append("CREATE TABLE `");
     } else {
       sb.append("CREATE TABLE IF NOT EXISTS `");
-      sb.append(outputTableName).append("` ( ");
     }
 
+    if(options.getHiveDatabaseName() != null) {
+      sb.append(options.getHiveDatabaseName()).append("`.`");
+    }
+    sb.append(outputTableName).append("` ( ");
+
     // Check that all explicitly mapped columns are present in result set
     for(Object column : userMapping.keySet()) {
       boolean found = false;
@@ -232,6 +236,9 @@ public class TableDefWriter {
       sb.append(" OVERWRITE");
     }
     sb.append(" INTO TABLE `");
+    if(options.getHiveDatabaseName() != null) {
+      sb.append(options.getHiveDatabaseName()).append("`.`");
+    }
     sb.append(outputTableName);
     sb.append('`');
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/8b3742ad/src/java/org/apache/sqoop/tool/BaseSqoopTool.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/sqoop/tool/BaseSqoopTool.java b/src/java/org/apache/sqoop/tool/BaseSqoopTool.java
index c457272..c46a7b7 100644
--- a/src/java/org/apache/sqoop/tool/BaseSqoopTool.java
+++ b/src/java/org/apache/sqoop/tool/BaseSqoopTool.java
@@ -99,6 +99,7 @@ public abstract class BaseSqoopTool extends com.cloudera.sqoop.tool.SqoopTool
{
   public static final String FMT_AVRODATAFILE_ARG = "as-avrodatafile";
   public static final String HIVE_IMPORT_ARG = "hive-import";
   public static final String HIVE_TABLE_ARG = "hive-table";
+  public static final String HIVE_DATABASE_ARG = "hive-database";
   public static final String HIVE_OVERWRITE_ARG = "hive-overwrite";
   public static final String HIVE_DROP_DELIMS_ARG = "hive-drop-import-delims";
   public static final String HIVE_DELIMS_REPLACEMENT_ARG =
@@ -445,6 +446,11 @@ public abstract class BaseSqoopTool extends com.cloudera.sqoop.tool.SqoopTool
{
         .withDescription("Sets the table name to use when importing to hive")
         .withLongOpt(HIVE_TABLE_ARG)
         .create());
+    hiveOpts.addOption(OptionBuilder.withArgName("database-name")
+      .hasArg()
+      .withDescription("Sets the database name to use when importing to hive")
+      .withLongOpt(HIVE_DATABASE_ARG)
+      .create());
     hiveOpts.addOption(OptionBuilder
         .withDescription("Drop Hive record \\0x01 and row delimiters "
           + "(\\n\\r) from imported string fields")
@@ -814,6 +820,10 @@ public abstract class BaseSqoopTool extends com.cloudera.sqoop.tool.SqoopTool
{
       out.setHiveTableName(in.getOptionValue(HIVE_TABLE_ARG));
     }
 
+    if(in.hasOption(HIVE_DATABASE_ARG)) {
+      out.setHiveDatabaseName(in.getOptionValue(HIVE_DATABASE_ARG));
+    }
+
     if (in.hasOption(HIVE_DROP_DELIMS_ARG)) {
       out.setHiveDropDelims(true);
     }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/8b3742ad/src/test/com/cloudera/sqoop/hive/TestTableDefWriter.java
----------------------------------------------------------------------
diff --git a/src/test/com/cloudera/sqoop/hive/TestTableDefWriter.java b/src/test/com/cloudera/sqoop/hive/TestTableDefWriter.java
index 6610b75..55e572e 100644
--- a/src/test/com/cloudera/sqoop/hive/TestTableDefWriter.java
+++ b/src/test/com/cloudera/sqoop/hive/TestTableDefWriter.java
@@ -212,4 +212,26 @@ public class TestTableDefWriter extends TestCase {
       // Expected, ok
     }
   }
+
+  public void testHiveDatabase() throws Exception {
+    String[] args = {
+        "--hive-database", "db",
+    };
+    Configuration conf = new Configuration();
+    SqoopOptions options =
+      new ImportTool().parseArguments(args, null, null, false);
+    TableDefWriter writer = new TableDefWriter(options,
+        null, HsqldbTestServer.getTableName(), "outputTable", conf, false);
+
+    Map<String, Integer> colTypes = new SqlTypeMap<String, Integer>();
+    writer.setColumnTypes(colTypes);
+
+    String createTable = writer.getCreateTableStmt();
+    assertNotNull(createTable);
+    assertTrue(createTable.contains("`db`.`outputTable`"));
+
+    String loadStmt = writer.getLoadDataStmt();
+    assertNotNull(loadStmt);
+    assertTrue(createTable.contains("`db`.`outputTable`"));
+  }
 }


Mime
View raw message