sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject git commit: SQOOP-1405: Add arg to enable SQL Server identity insert on export
Date Fri, 08 Aug 2014 03:11:17 GMT
Repository: sqoop
Updated Branches:
  refs/heads/trunk 11b1c9173 -> c859a5a15


SQOOP-1405: Add arg to enable SQL Server identity insert on export


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

Branch: refs/heads/trunk
Commit: c859a5a15d3127ea742e1947768fb08373503d76
Parents: 11b1c91
Author: Keegan Witt <keeganwitt@gmail.com>
Authored: Thu Aug 7 19:58:03 2014 -0700
Committer: Abraham Elmahrek <abraham@elmahrek.com>
Committed: Thu Aug 7 20:05:23 2014 -0700

----------------------------------------------------------------------
 src/docs/user/connectors.txt                    | 11 +++++++++++
 .../apache/sqoop/manager/SQLServerManager.java  | 20 ++++++++++++++++++++
 .../mapreduce/SQLServerExportDBExecThread.java  |  5 +++++
 .../SqlServerExportBatchOutputFormat.java       |  5 +++++
 4 files changed, 41 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/c859a5a1/src/docs/user/connectors.txt
----------------------------------------------------------------------
diff --git a/src/docs/user/connectors.txt b/src/docs/user/connectors.txt
index bba946a..9aa2f44 100644
--- a/src/docs/user/connectors.txt
+++ b/src/docs/user/connectors.txt
@@ -125,6 +125,8 @@ List of all extra arguments supported by Microsoft SQL Connector is shown
below:
 `----------------------------------------`---------------------------------------
 Argument                                 Description
 ---------------------------------------------------------------------------------
++\--identity-insert                      Set IDENTITY_INSERT to ON before \
+                                         export insert.
 +\--non-resilient+                       Don't attempt to recover failed \
                                          export operations.
 +\--schema <name>+                       Scheme name that sqoop should use. \
@@ -133,6 +135,15 @@ Argument                                 Description
                                          data movement.
 ---------------------------------------------------------------------------------
 
+Allow identity inserts
+^^^^^^^^^^^^^^^^^^^^^^
+
+You can allow inserts on columns that have identity. For example:
+
+----
+$ sqoop export ... --export-dir custom_dir --table custom_table -- --identity-insert
+----
+
 Non-resilient operations
 ^^^^^^^^^^^^^^^^^^^^^^^^
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/c859a5a1/src/java/org/apache/sqoop/manager/SQLServerManager.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/sqoop/manager/SQLServerManager.java b/src/java/org/apache/sqoop/manager/SQLServerManager.java
index 534c0cc..625f7cd 100644
--- a/src/java/org/apache/sqoop/manager/SQLServerManager.java
+++ b/src/java/org/apache/sqoop/manager/SQLServerManager.java
@@ -60,6 +60,11 @@ public class SQLServerManager
   // Option set in extra-arguments to disable resiliency and use default mode
   public static final String NON_RESILIENT_OPTION = "non-resilient";
 
+  // Option to allow inserts on identity columns
+  public static final String IDENTITY_INSERT = "identity-insert";
+  public static final String IDENTITY_INSERT_PROP =
+      "org.apache.sqoop.manager.sqlserver.table.identity";
+
   // driver class to ensure is loaded when making db connection.
   private static final String DRIVER_CLASS =
       "com.microsoft.sqlserver.jdbc.SQLServerDriver";
@@ -77,6 +82,11 @@ public class SQLServerManager
    */
   private String tableHints;
 
+  /**
+   * Whether to allow identity inserts.
+   */
+  private boolean identityInserts;
+
   public SQLServerManager(final SqoopOptions opts) {
     super(DRIVER_CLASS, opts);
 
@@ -159,6 +169,10 @@ public class SQLServerManager
     if (tableHints != null) {
       configuration.set(TABLE_HINTS_PROP, tableHints);
     }
+
+    // Propagate whether to allow identity inserts to job
+    configuration.setBoolean(IDENTITY_INSERT_PROP, identityInserts);
+
     JdbcExportJob exportJob;
     if (isNonResilientOperation()) {
       exportJob = new JdbcExportJob(context, null, null,
@@ -286,6 +300,8 @@ public class SQLServerManager
 
       this.tableHints = hints;
     }
+
+    identityInserts = cmdLine.hasOption(IDENTITY_INSERT);
   }
 
   /**
@@ -307,6 +323,10 @@ public class SQLServerManager
       .withDescription("Optional table hints to use")
       .withLongOpt(TABLE_HINTS).create());
 
+    extraOptions.addOption(OptionBuilder
+      .withDescription("Allow identity inserts")
+      .withLongOpt(IDENTITY_INSERT).create());
+
     return extraOptions;
   }
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/c859a5a1/src/java/org/apache/sqoop/mapreduce/SQLServerExportDBExecThread.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/sqoop/mapreduce/SQLServerExportDBExecThread.java b/src/java/org/apache/sqoop/mapreduce/SQLServerExportDBExecThread.java
index 1810ba8..7d58f1b 100644
--- a/src/java/org/apache/sqoop/mapreduce/SQLServerExportDBExecThread.java
+++ b/src/java/org/apache/sqoop/mapreduce/SQLServerExportDBExecThread.java
@@ -123,6 +123,11 @@ public class SQLServerExportDBExecThread extends
   protected String getInsertStatement(int numRows) {
     StringBuilder sb = new StringBuilder();
 
+    if (getConf().getBoolean(SQLServerManager.IDENTITY_INSERT_PROP, false)) {
+      LOG.info("Enabling identity inserts");
+      sb.append("SET IDENTITY_INSERT ").append(tableName).append(" ON ");
+    }
+
     sb.append("INSERT INTO " + tableName + " ");
 
     String tableHints = getConf().get(SQLServerManager.TABLE_HINTS_PROP);

http://git-wip-us.apache.org/repos/asf/sqoop/blob/c859a5a1/src/java/org/apache/sqoop/mapreduce/sqlserver/SqlServerExportBatchOutputFormat.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/sqoop/mapreduce/sqlserver/SqlServerExportBatchOutputFormat.java
b/src/java/org/apache/sqoop/mapreduce/sqlserver/SqlServerExportBatchOutputFormat.java
index cc69d77..e583bbb 100644
--- a/src/java/org/apache/sqoop/mapreduce/sqlserver/SqlServerExportBatchOutputFormat.java
+++ b/src/java/org/apache/sqoop/mapreduce/sqlserver/SqlServerExportBatchOutputFormat.java
@@ -62,6 +62,11 @@ public class SqlServerExportBatchOutputFormat<K extends SqoopRecord,
V>
     protected String getInsertStatement(int numRows) {
       StringBuilder sb = new StringBuilder();
 
+      if (getConf().getBoolean(SQLServerManager.IDENTITY_INSERT_PROP, false)) {
+        LOG.info("Enabling identity inserts");
+        sb.append("SET IDENTITY_INSERT ").append(tableName).append(" ON ");
+      }
+
       sb.append("INSERT INTO " + tableName + " ");
 
       String tableHints = getConf().get(SQLServerManager.TABLE_HINTS_PROP);


Mime
View raw message