5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-12 15:01:45 +08:00

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

This commit is contained in:
Keegan Witt 2014-08-07 19:58:03 -07:00 committed by Abraham Elmahrek
parent 11b1c91739
commit c859a5a15d
4 changed files with 41 additions and 0 deletions

View File

@ -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
^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -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 void exportTable(com.cloudera.sqoop.manager.ExportJobContext context)
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 @@ void parseExtraArgs(String[] args) throws ParseException {
this.tableHints = hints;
}
identityInserts = cmdLine.hasOption(IDENTITY_INSERT);
}
/**
@ -307,6 +323,10 @@ private RelatedOptions getExtraOptions() {
.withDescription("Optional table hints to use")
.withLongOpt(TABLE_HINTS).create());
extraOptions.addOption(OptionBuilder
.withDescription("Allow identity inserts")
.withLongOpt(IDENTITY_INSERT).create());
return extraOptions;
}

View File

@ -123,6 +123,11 @@ protected void executeStatement(PreparedStatement stmt,
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);

View File

@ -62,6 +62,11 @@ public SqlServerExportBatchRecordWriter(TaskAttemptContext context)
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);