/** * Get the database by name. * @param dbName the name of the database. * @return a Database object if this database exists, null otherwise. * @throws HiveException */ public Database getDatabase(String dbName) throws HiveException { try { return getMSC().getDatabase(dbName); } catch (NoSuchObjectException e) { return null; } catch (Exception e) { throw new HiveException(e); } }
Database fetched = client.getDatabase(dbNames[0]); String expectedLocation = new File(expectedBaseDir(), dbNames[0] + ".db").toURI().toString(); Assert.assertEquals(expectedCatalog(), fetched.getCatalogName()); Assert.assertEquals(expectedCatalog(), fetched.getCatalogName()); fetched = client.getDatabase(dbNames[1]); Assert.assertEquals(new File(db1Location).toURI().toString(), fetched.getLocationUri() + "/"); dir = new File(new URI(fetched.getLocationUri()).getPath());
/** * Get the database by name. * @param catName catalog name * @param dbName the name of the database. * @return a Database object if this database exists, null otherwise. * @throws HiveException */ public Database getDatabase(String catName, String dbName) throws HiveException { try { return getMSC().getDatabase(catName, dbName); } catch (NoSuchObjectException e) { return null; } catch (Exception e) { throw new HiveException(e); } }
/** * Get the database by name. * @param dbName the name of the database. * @return a Database object if this database exists, null otherwise. * @throws HiveException */ public Database getDatabase(String dbName) throws HiveException { try { return getMSC().getDatabase(dbName); } catch (NoSuchObjectException e) { return null; } catch (Exception e) { throw new HiveException(e); } }
@Test(expected = NoSuchObjectException.class) public void fetchDatabaseInNonExistentCatalog() throws TException { client.getDatabase("nosuch", Warehouse.DEFAULT_DATABASE_NAME); }
@Test(expected = MetaException.class) public void testGetDatabaseNullName() throws Exception { // Missing database name in the query client.getDatabase(null); }
@Test(expected = NoSuchObjectException.class) public void testGetDatabaseNoSuchDatabase() throws Exception { client.getDatabase("no_such_database"); }
@Test public void testGetDatabaseCaseInsensitive() throws Exception { Database database = testDatabases[0]; // Test in upper case Database resultUpper = client.getDatabase(database.getName().toUpperCase()); Assert.assertEquals("Comparing databases", database, resultUpper); // Test in mixed case Database resultMix = client.getDatabase("teST_dAtABase_1"); Assert.assertEquals("Comparing databases", database, resultMix); }
@Override public HCatDatabase getDatabase(String dbName) throws HCatException { HCatDatabase db = null; try { Database hiveDB = hmsClient.getDatabase(checkDB(dbName)); if (hiveDB != null) { db = new HCatDatabase(hiveDB); } } catch (NoSuchObjectException exp) { throw new ObjectNotFoundException( "NoSuchObjectException while fetching database", exp); } catch (MetaException exp) { throw new HCatException("MetaException while fetching database", exp); } catch (TException exp) { throw new ConnectionFailureException( "TException while fetching database", exp); } return db; }
public void dropDatabaseIfExists(String dbName) throws MetaException, TException { try { this.getLocalMetastoreClient().getDatabase(dbName); this.getLocalMetastoreClient().dropDatabase(dbName, false, true, true); } catch (NoSuchObjectException e) { // No need to drop } }
@Test public void testAlterDatabaseCaseInsensitive() throws Exception { Database originalDatabase = testDatabases[0]; Database newDatabase = originalDatabase.deepCopy(); newDatabase.setDescription("Altered database"); // Test in upper case client.alterDatabase(originalDatabase.getName().toUpperCase(), newDatabase); Database alteredDatabase = client.getDatabase(newDatabase.getName()); Assert.assertEquals("Comparing databases", newDatabase, alteredDatabase); // Test in mixed case originalDatabase = testDatabases[2]; newDatabase = originalDatabase.deepCopy(); newDatabase.setDescription("Altered database 2"); client.alterDatabase("TeST_daTAbaSe_TO_FiNd_2", newDatabase); alteredDatabase = client.getDatabase(newDatabase.getName()); Assert.assertEquals("Comparing databases", newDatabase, alteredDatabase); }
@Test public void testCreate() throws TException { HiveConf hiveConf = new HiveConf(); HiveMetaStoreClientFactory factory = new HiveMetaStoreClientFactory(hiveConf); // Since we havE a specified hive-site in the classpath, so have to null it out here to proceed the test // The original value it will get if no local hive-site is placed, will be an empty string. hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, ""); hiveConf.set(HIVE_METASTORE_TOKEN_SIGNATURE, ""); IMetaStoreClient msc = factory.create(); String dbName = "test_db"; String description = "test database"; String location = "file:/tmp/" + dbName; Database db = new Database(dbName, description, location, null); msc.dropDatabase(dbName, true, true); msc.createDatabase(db); db = msc.getDatabase(dbName); Assert.assertEquals(db.getName(), dbName); Assert.assertEquals(db.getDescription(), description); Assert.assertEquals(db.getLocationUri(), location); } }
@Test public void testDatabaseCreateTime() throws Exception { // create db without specifying createtime Database testDb = new DatabaseBuilder().setName("test_create_time").create(client, metaStore.getConf()); Database database = client.getDatabase("test_create_time"); Assert.assertTrue("Database create time should have been set", database.getCreateTime() > 0); }
/** * This test creates and queries a database and then drops it. Good for testing the happy path. */ @Test public void testCreateGetDeleteDatabase() throws Exception { Database database = getDatabaseWithAllParametersSet(); client.createDatabase(database); Database createdDatabase = client.getDatabase(database.getName()); // The createTime will be set on the server side, so the comparison should skip it database.setCreateTime(createdDatabase.getCreateTime()); Assert.assertEquals("Comparing databases", database, createdDatabase); Assert.assertTrue("The directory should be created", metaStore.isPathExists( new Path(database.getLocationUri()))); client.dropDatabase(database.getName()); Assert.assertFalse("The directory should be removed", metaStore.isPathExists(new Path(database.getLocationUri()))); try { client.getDatabase(database.getName()); Assert.fail("Expected a NoSuchObjectException to be thrown"); } catch (NoSuchObjectException exception) { // Expected exception } }
@Test public void testDbCreateTimeOverride() throws Exception { // create db by providing a create time. Should be overridden, create time should // always be set by metastore Database testDb = new DatabaseBuilder().setName("test_create_time") .setCreateTime(1) .create(client, metaStore.getConf()); Database database = client.getDatabase("test_create_time"); Assert.assertTrue("Database create time should have been set", database.getCreateTime() > 0); Assert.assertTrue("Database create time should have been reset by metastore", database.getCreateTime() != 1); }
@Test public void testDefaultDatabaseData() throws Exception { Database database = client.getDatabase(DEFAULT_DATABASE); Assert.assertEquals("Default database name", "default", database.getName()); Assert.assertEquals("Default database description", "Default Hive database", database.getDescription()); Assert.assertEquals("Default database location", metaStore.getWarehouseRoot(), new Path(database.getLocationUri())); Assert.assertEquals("Default database parameters", new HashMap<String, String>(), database.getParameters()); Assert.assertEquals("Default database owner", "public", database.getOwnerName()); Assert.assertEquals("Default database owner type", PrincipalType.ROLE, database.getOwnerType()); Assert.assertNull("Default database privileges", database.getPrivileges()); Assert.assertTrue("database create time should be set", database.isSetCreateTime()); Assert.assertTrue("Database create time should be non-zero", database.getCreateTime() > 0); }
@Test public void testAlterDatabaseNotNullableFields() throws Exception { Database database = getDatabaseWithAllParametersSet(); client.createDatabase(database); Database originalDatabase = client.getDatabase(database.getName()); Database newDatabase = new Database(); newDatabase.setName("new_name"); newDatabase.setCatalogName(DEFAULT_CATALOG_NAME); client.alterDatabase(originalDatabase.getName(), newDatabase); // The name should not be changed, so reload the db with the original name Database alteredDatabase = client.getDatabase(originalDatabase.getName()); Assert.assertEquals("Database name should not change", originalDatabase.getName(), alteredDatabase.getName()); Assert.assertEquals("Database description should not change", originalDatabase.getDescription(), alteredDatabase.getDescription()); Assert.assertEquals("Database location should not change", originalDatabase.getLocationUri(), alteredDatabase.getLocationUri()); Assert.assertEquals("Database parameters should be empty", new HashMap<String, String>(), alteredDatabase.getParameters()); Assert.assertNull("Database owner should be empty", alteredDatabase.getOwnerName()); Assert.assertEquals("Database owner type should not change", originalDatabase.getOwnerType(), alteredDatabase.getOwnerType()); Assert.assertNull("Database privileges should be empty", alteredDatabase.getPrivileges()); }
@Test public void testCreateDatabaseDefaultValues() throws Exception { Database database = new DatabaseBuilder() .setName("dummy") .create(client, metaStore.getConf()); Database createdDatabase = client.getDatabase(database.getName()); Assert.assertNull("Comparing description", createdDatabase.getDescription()); Assert.assertEquals("Comparing location", metaStore.getWarehouseRoot() + "/" + createdDatabase.getName() + ".db", createdDatabase.getLocationUri()); Assert.assertEquals("Comparing parameters", new HashMap<String, String>(), createdDatabase.getParameters()); Assert.assertNull("Comparing privileges", createdDatabase.getPrivileges()); Assert.assertEquals("Comparing owner name", SecurityUtils.getUser(), createdDatabase.getOwnerName()); Assert.assertEquals("Comparing owner type", PrincipalType.USER, createdDatabase.getOwnerType()); }
@Before public void setUp() throws Exception { // Get new client client = metaStore.getClient(); // Clean up the databases for(String databaseName : client.getAllDatabases()) { if (!databaseName.equals(DEFAULT_DATABASE)) { client.dropDatabase(databaseName, true, true, true); } } testDatabases[0] = new DatabaseBuilder().setName("test_database_1").create(client, metaStore.getConf()); testDatabases[1] = new DatabaseBuilder().setName("test_database_to_find_1").create(client, metaStore.getConf()); testDatabases[2] = new DatabaseBuilder().setName("test_database_to_find_2").create(client, metaStore.getConf()); testDatabases[3] = new DatabaseBuilder().setName("test_database_hidden_1").create(client, metaStore.getConf()); // Create the databases, and reload them from the MetaStore for (int i=0; i < testDatabases.length; i++) { testDatabases[i] = client.getDatabase(testDatabases[i].getName()); } }
@Test public void testAlterDatabase() throws Exception { Database originalDatabase = testDatabases[0]; Database newDatabase = new DatabaseBuilder() // The database name is not changed during alter .setName(originalDatabase.getName()) .setOwnerType(PrincipalType.GROUP) .setOwnerName("owner2") .setLocation(metaStore.getWarehouseRoot() + "/database_location_2") .setDescription("dummy description 2") .addParam("param_key_1", "param_value_1_2") .addParam("param_key_2_3", "param_value_2_3") .setCreateTime(originalDatabase.getCreateTime()) .build(metaStore.getConf()); client.alterDatabase(originalDatabase.getName(), newDatabase); Database alteredDatabase = client.getDatabase(newDatabase.getName()); Assert.assertEquals("Comparing Databases", newDatabase, alteredDatabase); }