[FLINK-28913][hive] Fix failed to open HiveCatalog when it's for hive3 (#20573)
diff --git a/flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/client/HiveMetastoreClientWrapper.java b/flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/client/HiveMetastoreClientWrapper.java
index 3acc986..b7ca1e1 100644
--- a/flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/client/HiveMetastoreClientWrapper.java
+++ b/flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/client/HiveMetastoreClientWrapper.java
@@ -71,9 +71,9 @@
private static final Logger LOG = LoggerFactory.getLogger(HiveMetastoreClientWrapper.class);
private final IMetaStoreClient client;
- private final Hive hive;
private final HiveConf hiveConf;
private final HiveShim hiveShim;
+ private volatile Hive hive;
public HiveMetastoreClientWrapper(HiveConf hiveConf, String hiveVersion) {
this(hiveConf, HiveShimLoader.loadHiveShim(hiveVersion));
@@ -87,11 +87,6 @@
HiveCatalog.isEmbeddedMetastore(hiveConf)
? createMetastoreClient()
: HiveMetaStoreClient.newSynchronizedClient(createMetastoreClient());
- try {
- this.hive = Hive.get(hiveConf);
- } catch (HiveException e) {
- throw new FlinkHiveException(e);
- }
}
@Override
@@ -349,6 +344,7 @@
public void loadTable(Path loadPath, String tableName, boolean replace, boolean isSrcLocal)
throws HiveException {
+ initHive();
hiveShim.loadTable(hive, loadPath, tableName, replace, isSrcLocal);
}
@@ -359,7 +355,22 @@
boolean isSkewedStoreAsSubdir,
boolean replace,
boolean isSrcLocal) {
+ initHive();
hiveShim.loadPartition(
hive, loadPath, tableName, partSpec, isSkewedStoreAsSubdir, replace, isSrcLocal);
}
+
+ private void initHive() {
+ if (this.hive == null) {
+ synchronized (this) {
+ if (this.hive == null) {
+ try {
+ this.hive = Hive.get(hiveConf);
+ } catch (HiveException e) {
+ throw new FlinkHiveException(e);
+ }
+ }
+ }
+ }
+ }
}