if (!dynamicPartitioningUsed ) {
// regular single-partition write into a partitioned table.
//Move data from temp directory the actual table directory
if (partitionsToAdd.size() > 1){
throw new HCatException(ErrorType.ERROR_PUBLISHING_PARTITION,
"More than one partition to publish in non-dynamic partitioning job");
}
Partition p = partitionsToAdd.get(0);
Path src = new Path(jobInfo.getLocation());
Path dest = new Path(p.getSd().getLocation());
moveTaskOutputs(fs, src, src, dest, true, table.isImmutable());
moveTaskOutputs(fs,src,src,dest,false,table.isImmutable());
if (!src.equals(dest)){
fs.delete(src, true);
}
// Now, we check if the partition already exists. If not, we go ahead.
// If so, we error out if immutable, and if mutable, check that the partition's IF
// matches our current job's IF (table's IF) to check for compatibility. If compatible, we
// ignore and do not add. If incompatible, we error out again.
boolean publishRequired = false;
try {
Partition existingP = client.getPartition(p.getDbName(),p.getTableName(),p.getValues());
if (existingP != null){
if (table.isImmutable()){
throw new HCatException(ErrorType.ERROR_DUPLICATE_PARTITION,
"Attempted duplicate partition publish on to immutable table");
} else {
if (! existingP.getSd().getInputFormat().equals(table.getInputFormatClass().getName())){
throw new HCatException(ErrorType.ERROR_PUBLISHING_PARTITION,
"Attempted partition append, where old partition format was "
+ existingP.getSd().getInputFormat()
+ " and table format was "
+ table.getInputFormatClass().getName());
}
}
} else {
publishRequired = true;
}
} catch (NoSuchObjectException e){
// All good, no such partition exists, move on.
publishRequired = true;
}
if (publishRequired){
client.add_partitions(partitionsToAdd);
partitionsAdded = partitionsToAdd;
}
} else {
// Dynamic partitioning usecase
if (!customDynamicLocationUsed) {
Path src = new Path(ptnRootLocation);
moveTaskOutputs(fs, src, src, tblPath, true, true); // dryRun = true, immutable = true
moveTaskOutputs(fs, src, src, tblPath, false, true);
if (!src.equals(tblPath)){
fs.delete(src, true);
}
} else {
moveCustomLocationTaskOutputs(fs, table, hiveConf);
}
client.add_partitions(partitionsToAdd);
partitionsAdded = partitionsToAdd;
}
}
// Set permissions appropriately for each of the partitions we just created
// so as to have their permissions mimic the table permissions
for (Partition p : partitionsAdded){
applyGroupAndPerms(fs,new Path(p.getSd().getLocation()),tblStat.getPermission(),tblStat.getGroup(),true);
}
}
} catch (Exception e) {
if (partitionsAdded.size() > 0) {
try {
// baseCommitter.cleanupJob failed, try to clean up the
// metastore
for (Partition p : partitionsAdded) {
client.dropPartition(tableInfo.getDatabaseName(),
tableInfo.getTableName(), p.getValues());
}
} catch (Exception te) {
// Keep cause as the original exception
throw new HCatException(
ErrorType.ERROR_PUBLISHING_PARTITION, e);
}
}
if (e instanceof HCatException) {
throw (HCatException) e;
} else {
throw new HCatException(ErrorType.ERROR_PUBLISHING_PARTITION, e);
}
} finally {
HCatUtil.closeHiveClientQuietly(client);
}
}