if (cmgr.supportsStagingForExport()) {
LOG.info("Data will be staged in the table: " + stagingTableName);
tableName = stagingTableName;
stagingEnabled = true;
} else {
throw new ExportException("The active connection manager ("
+ cmgr.getClass().getCanonicalName()
+ ") does not support staging of data for export. "
+ "Please retry without specifying the --staging-table option.");
}
}
String tableClassName =
new TableClassName(options).getClassForTable(outputTableName);
String ormJarFile = context.getJarFile();
LOG.info("Beginning export of " + outputTableName);
loadJars(conf, ormJarFile, tableClassName);
if (stagingEnabled) {
// Prepare the staging table
if (options.doClearStagingTable()) {
try {
// Delete all records from staging table
cmgr.deleteAllRecords(stagingTableName);
} catch (SQLException ex) {
throw new ExportException(
"Failed to empty staging table before export run", ex);
}
} else {
// User has not explicitly specified the clear staging table option.
// Assert that the staging table is empty.
try {
long rowCount = cmgr.getTableRowCount(stagingTableName);
if (rowCount != 0L) {
throw new ExportException("The specified staging table ("
+ stagingTableName + ") is not empty. To force deletion of "
+ "its data, please retry with --clear-staging-table option.");
}
} catch (SQLException ex) {
throw new ExportException(
"Failed to count data rows in staging table: "
+ stagingTableName, ex);
}
}
}
try {
Job job = new Job(conf);
// Set the external jar to use for the job.
job.getConfiguration().set("mapred.jar", ormJarFile);
configureInputFormat(job, tableName, tableClassName, null);
configureOutputFormat(job, tableName, tableClassName);
configureMapper(job, tableName, tableClassName);
configureNumTasks(job);
cacheJars(job, context.getConnManager());
setJob(job);
boolean success = runJob(job);
if (!success) {
throw new ExportException("Export job failed!");
}
} catch (InterruptedException ie) {
throw new IOException(ie);
} catch (ClassNotFoundException cnfe) {
throw new IOException(cnfe);
} finally {
unloadJars();
}
// Unstage the data if needed
if (stagingEnabled) {
// Migrate data from staging table to the output table
try {
LOG.info("Starting to migrate data from staging table to destination.");
cmgr.migrateData(stagingTableName, outputTableName);
} catch (SQLException ex) {
LOG.error("Failed to move data from staging table ("
+ stagingTableName + ") to target table ("
+ outputTableName + ")", ex);
throw new ExportException(
"Failed to move data from staging table", ex);
}
}
}