[HUDI-2244] Fix database alreadyExists exception while hive sync (#3361)
This commit is contained in:
@@ -150,7 +150,9 @@ public class HiveSyncTool extends AbstractSyncTool {
|
|||||||
// check if the database exists else create it
|
// check if the database exists else create it
|
||||||
if (cfg.autoCreateDatabase) {
|
if (cfg.autoCreateDatabase) {
|
||||||
try {
|
try {
|
||||||
|
if (!hoodieHiveClient.doesDataBaseExist(cfg.databaseName)) {
|
||||||
hoodieHiveClient.createDatabase(cfg.databaseName);
|
hoodieHiveClient.createDatabase(cfg.databaseName);
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// this is harmless since table creation will fail anyways, creation of DB is needed for in-memory testing
|
// this is harmless since table creation will fail anyways, creation of DB is needed for in-memory testing
|
||||||
LOG.warn("Unable to create database", e);
|
LOG.warn("Unable to create database", e);
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ import org.apache.hadoop.fs.FileSystem;
|
|||||||
import org.apache.hadoop.fs.Path;
|
import org.apache.hadoop.fs.Path;
|
||||||
import org.apache.hadoop.hive.conf.HiveConf;
|
import org.apache.hadoop.hive.conf.HiveConf;
|
||||||
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
|
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
|
||||||
import org.apache.hadoop.hive.metastore.api.Database;
|
|
||||||
import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
|
import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
|
||||||
import org.apache.hadoop.hive.metastore.api.Partition;
|
import org.apache.hadoop.hive.metastore.api.Partition;
|
||||||
import org.apache.hadoop.hive.metastore.api.Table;
|
import org.apache.hadoop.hive.metastore.api.Table;
|
||||||
@@ -221,14 +220,14 @@ public class HoodieHiveClient extends AbstractSyncHoodieClient {
|
|||||||
*/
|
*/
|
||||||
public boolean doesDataBaseExist(String databaseName) {
|
public boolean doesDataBaseExist(String databaseName) {
|
||||||
try {
|
try {
|
||||||
Database database = client.getDatabase(databaseName);
|
client.getDatabase(databaseName);
|
||||||
if (database != null && databaseName.equals(database.getName())) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
} catch (NoSuchObjectException noSuchObjectException) {
|
||||||
|
// NoSuchObjectException is thrown when there is no existing database of the name.
|
||||||
|
return false;
|
||||||
} catch (TException e) {
|
} catch (TException e) {
|
||||||
throw new HoodieHiveSyncException("Failed to check if database exists " + databaseName, e);
|
throw new HoodieHiveSyncException("Failed to check if database exists " + databaseName, e);
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createDatabase(String databaseName) {
|
public void createDatabase(String databaseName) {
|
||||||
|
|||||||
@@ -62,8 +62,10 @@ import java.util.Map;
|
|||||||
import static org.apache.hudi.hive.testutils.HiveTestUtil.ddlExecutor;
|
import static org.apache.hudi.hive.testutils.HiveTestUtil.ddlExecutor;
|
||||||
import static org.apache.hudi.hive.testutils.HiveTestUtil.fileSystem;
|
import static org.apache.hudi.hive.testutils.HiveTestUtil.fileSystem;
|
||||||
import static org.apache.hudi.hive.testutils.HiveTestUtil.hiveSyncConfig;
|
import static org.apache.hudi.hive.testutils.HiveTestUtil.hiveSyncConfig;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
public class TestHiveSyncTool {
|
public class TestHiveSyncTool {
|
||||||
@@ -302,6 +304,49 @@ public class TestHiveSyncTool {
|
|||||||
"The last commit that was synced should be 100");
|
"The last commit that was synced should be 100");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@MethodSource({"syncMode"})
|
||||||
|
public void testSyncDataBase(String syncMode) throws Exception {
|
||||||
|
hiveSyncConfig.syncMode = syncMode;
|
||||||
|
HiveTestUtil.hiveSyncConfig.batchSyncNum = 3;
|
||||||
|
String instantTime = "100";
|
||||||
|
HiveTestUtil.createCOWTable(instantTime, 5, true);
|
||||||
|
hiveSyncConfig.databaseName = "database1";
|
||||||
|
|
||||||
|
// while autoCreateDatabase is false and database not exists;
|
||||||
|
hiveSyncConfig.autoCreateDatabase = false;
|
||||||
|
// Lets do the sync
|
||||||
|
assertThrows(Exception.class, () -> {
|
||||||
|
new HiveSyncTool(hiveSyncConfig, HiveTestUtil.getHiveConf(), fileSystem).syncHoodieTable();
|
||||||
|
});
|
||||||
|
|
||||||
|
// while autoCreateDatabase is true and database not exists;
|
||||||
|
hiveSyncConfig.autoCreateDatabase = true;
|
||||||
|
HoodieHiveClient hiveClient =
|
||||||
|
new HoodieHiveClient(HiveTestUtil.hiveSyncConfig, HiveTestUtil.getHiveConf(), fileSystem);
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
new HiveSyncTool(hiveSyncConfig, HiveTestUtil.getHiveConf(), fileSystem).syncHoodieTable();
|
||||||
|
});
|
||||||
|
assertTrue(hiveClient.doesDataBaseExist(hiveSyncConfig.databaseName),
|
||||||
|
"DataBases " + hiveSyncConfig.databaseName + " should exist after sync completes");
|
||||||
|
|
||||||
|
// while autoCreateDatabase is false and database exists;
|
||||||
|
hiveSyncConfig.autoCreateDatabase = false;
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
new HiveSyncTool(hiveSyncConfig, HiveTestUtil.getHiveConf(), fileSystem).syncHoodieTable();
|
||||||
|
});
|
||||||
|
assertTrue(hiveClient.doesDataBaseExist(hiveSyncConfig.databaseName),
|
||||||
|
"DataBases " + hiveSyncConfig.databaseName + " should exist after sync completes");
|
||||||
|
|
||||||
|
// while autoCreateDatabase is true and database exists;
|
||||||
|
hiveSyncConfig.autoCreateDatabase = true;
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
new HiveSyncTool(hiveSyncConfig, HiveTestUtil.getHiveConf(), fileSystem).syncHoodieTable();
|
||||||
|
});
|
||||||
|
assertTrue(hiveClient.doesDataBaseExist(hiveSyncConfig.databaseName),
|
||||||
|
"DataBases " + hiveSyncConfig.databaseName + " should exist after sync completes");
|
||||||
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@MethodSource({"syncDataSourceTableParams"})
|
@MethodSource({"syncDataSourceTableParams"})
|
||||||
public void testSyncCOWTableWithProperties(boolean useSchemaFromCommitMetadata,
|
public void testSyncCOWTableWithProperties(boolean useSchemaFromCommitMetadata,
|
||||||
@@ -1054,6 +1099,8 @@ public class TestHiveSyncTool {
|
|||||||
hiveSyncConfig.syncMode = syncMode;
|
hiveSyncConfig.syncMode = syncMode;
|
||||||
HiveTestUtil.hiveSyncConfig.batchSyncNum = 2;
|
HiveTestUtil.hiveSyncConfig.batchSyncNum = 2;
|
||||||
HiveTestUtil.createCOWTable("100", 5, true);
|
HiveTestUtil.createCOWTable("100", 5, true);
|
||||||
|
// create database.
|
||||||
|
ddlExecutor.runSQL("create database " + hiveSyncConfig.databaseName);
|
||||||
HoodieHiveClient hiveClient =
|
HoodieHiveClient hiveClient =
|
||||||
new HoodieHiveClient(HiveTestUtil.hiveSyncConfig, HiveTestUtil.getHiveConf(), HiveTestUtil.fileSystem);
|
new HoodieHiveClient(HiveTestUtil.hiveSyncConfig, HiveTestUtil.getHiveConf(), HiveTestUtil.fileSystem);
|
||||||
String tableName = HiveTestUtil.hiveSyncConfig.tableName;
|
String tableName = HiveTestUtil.hiveSyncConfig.tableName;
|
||||||
|
|||||||
@@ -138,8 +138,7 @@ public class HiveTestUtil {
|
|||||||
ddlExecutor.runSQL("drop table if exists " + tableName);
|
ddlExecutor.runSQL("drop table if exists " + tableName);
|
||||||
}
|
}
|
||||||
createdTablesSet.clear();
|
createdTablesSet.clear();
|
||||||
ddlExecutor.runSQL("drop database if exists " + hiveSyncConfig.databaseName);
|
ddlExecutor.runSQL("drop database if exists " + hiveSyncConfig.databaseName + " cascade");
|
||||||
ddlExecutor.runSQL("create database " + hiveSyncConfig.databaseName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HiveConf getHiveConf() {
|
public static HiveConf getHiveConf() {
|
||||||
|
|||||||
Reference in New Issue
Block a user