[HUDI-2520] Fix drop partition issue when sync to hive (#5147)
This commit is contained in:
@@ -238,8 +238,11 @@ public class HMSDDLExecutor implements DDLExecutor {
|
|||||||
LOG.info("Drop partitions " + partitionsToDrop.size() + " on " + tableName);
|
LOG.info("Drop partitions " + partitionsToDrop.size() + " on " + tableName);
|
||||||
try {
|
try {
|
||||||
for (String dropPartition : partitionsToDrop) {
|
for (String dropPartition : partitionsToDrop) {
|
||||||
String partitionClause = HivePartitionUtil.getPartitionClauseForDrop(dropPartition, partitionValueExtractor, syncConfig);
|
if (HivePartitionUtil.partitionExists(client, tableName, dropPartition, partitionValueExtractor, syncConfig)) {
|
||||||
client.dropPartition(syncConfig.databaseName, tableName, partitionClause, false);
|
String partitionClause =
|
||||||
|
HivePartitionUtil.getPartitionClauseForDrop(dropPartition, partitionValueExtractor, syncConfig);
|
||||||
|
client.dropPartition(syncConfig.databaseName, tableName, partitionClause, false);
|
||||||
|
}
|
||||||
LOG.info("Drop partition " + dropPartition + " on " + tableName);
|
LOG.info("Drop partition " + dropPartition + " on " + tableName);
|
||||||
}
|
}
|
||||||
} catch (TException e) {
|
} catch (TException e) {
|
||||||
|
|||||||
@@ -137,8 +137,12 @@ public class HiveQueryDDLExecutor extends QueryBasedDDLExecutor {
|
|||||||
LOG.info("Drop partitions " + partitionsToDrop.size() + " on " + tableName);
|
LOG.info("Drop partitions " + partitionsToDrop.size() + " on " + tableName);
|
||||||
try {
|
try {
|
||||||
for (String dropPartition : partitionsToDrop) {
|
for (String dropPartition : partitionsToDrop) {
|
||||||
String partitionClause = HivePartitionUtil.getPartitionClauseForDrop(dropPartition, partitionValueExtractor, config);
|
if (HivePartitionUtil.partitionExists(metaStoreClient, tableName, dropPartition, partitionValueExtractor,
|
||||||
metaStoreClient.dropPartition(config.databaseName, tableName, partitionClause, false);
|
config)) {
|
||||||
|
String partitionClause =
|
||||||
|
HivePartitionUtil.getPartitionClauseForDrop(dropPartition, partitionValueExtractor, config);
|
||||||
|
metaStoreClient.dropPartition(config.databaseName, tableName, partitionClause, false);
|
||||||
|
}
|
||||||
LOG.info("Drop partition " + dropPartition + " on " + tableName);
|
LOG.info("Drop partition " + dropPartition + " on " + tableName);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -18,15 +18,22 @@
|
|||||||
|
|
||||||
package org.apache.hudi.hive.util;
|
package org.apache.hudi.hive.util;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
|
||||||
|
import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
|
||||||
|
import org.apache.hadoop.hive.metastore.api.Partition;
|
||||||
import org.apache.hudi.common.util.PartitionPathEncodeUtils;
|
import org.apache.hudi.common.util.PartitionPathEncodeUtils;
|
||||||
import org.apache.hudi.common.util.ValidationUtils;
|
import org.apache.hudi.common.util.ValidationUtils;
|
||||||
import org.apache.hudi.hive.HiveSyncConfig;
|
import org.apache.hudi.hive.HiveSyncConfig;
|
||||||
|
import org.apache.hudi.hive.HoodieHiveSyncException;
|
||||||
import org.apache.hudi.hive.PartitionValueExtractor;
|
import org.apache.hudi.hive.PartitionValueExtractor;
|
||||||
|
import org.apache.log4j.LogManager;
|
||||||
import java.util.ArrayList;
|
import org.apache.log4j.Logger;
|
||||||
import java.util.List;
|
import org.apache.thrift.TException;
|
||||||
|
|
||||||
public class HivePartitionUtil {
|
public class HivePartitionUtil {
|
||||||
|
private static final Logger LOG = LogManager.getLogger(HivePartitionUtil.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build String, example as year=2021/month=06/day=25
|
* Build String, example as year=2021/month=06/day=25
|
||||||
@@ -48,4 +55,19 @@ public class HivePartitionUtil {
|
|||||||
}
|
}
|
||||||
return String.join("/", partBuilder);
|
return String.join("/", partBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Boolean partitionExists(IMetaStoreClient client, String tableName, String partitionPath,
|
||||||
|
PartitionValueExtractor partitionValueExtractor, HiveSyncConfig config) {
|
||||||
|
Partition newPartition;
|
||||||
|
try {
|
||||||
|
List<String> partitionValues = partitionValueExtractor.extractPartitionValuesInPath(partitionPath);
|
||||||
|
newPartition = client.getPartition(config.databaseName, tableName, partitionValues);
|
||||||
|
} catch (NoSuchObjectException ignored) {
|
||||||
|
newPartition = null;
|
||||||
|
} catch (TException e) {
|
||||||
|
LOG.error("Failed to get partition " + partitionPath, e);
|
||||||
|
throw new HoodieHiveSyncException("Failed to get partition " + partitionPath, e);
|
||||||
|
}
|
||||||
|
return newPartition != null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user