/** * Build the database, create it in the metastore, and then return the db object. * @param client metastore client * @param conf configuration file * @return new database object * @throws TException comes from {@link #build(Configuration)} or * {@link IMetaStoreClient#createDatabase(Database)}. */ public Database create(IMetaStoreClient client, Configuration conf) throws TException { Database db = build(conf); client.createDatabase(db); return db; } }
/** * Create a database * @param db * @param ifNotExist if true, will ignore AlreadyExistsException exception * @throws AlreadyExistsException * @throws HiveException */ public void createDatabase(Database db, boolean ifNotExist) throws AlreadyExistsException, HiveException { try { getMSC().createDatabase(db); } catch (AlreadyExistsException e) { if (!ifNotExist) { throw e; } } catch (Exception e) { throw new HiveException(e); } }
/** * Create a database * @param db * @param ifNotExist if true, will ignore AlreadyExistsException exception * @throws AlreadyExistsException * @throws HiveException */ public void createDatabase(Database db, boolean ifNotExist) throws AlreadyExistsException, HiveException { try { getMSC().createDatabase(db); } catch (AlreadyExistsException e) { if (!ifNotExist) { throw e; } } catch (Exception e) { throw new HiveException(e); } }
@Test(expected = AlreadyExistsException.class) public void testCreateDatabaseAlreadyExists() throws Exception { Database database = testDatabases[0]; // Already existing database client.createDatabase(database); }
public void createTestDb(String dbName) throws Exception { Database db = new Database(dbName, "Some description", "/tmp/" + dbName, new HashMap<String, String>()); try { this.localMetastoreClient.createDatabase(db); } catch (AlreadyExistsException e) { log.warn(dbName + " already exits"); } }
@Test(expected = InvalidObjectException.class) public void testCreateDatabaseInvalidName() throws Exception { Database database = testDatabases[0]; // Invalid character in new database name database.setName("test_database_1;"); client.createDatabase(database); }
@Test(expected = InvalidObjectException.class) public void testCreateDatabaseEmptyName() throws Exception { Database database = testDatabases[0]; // Empty new database name database.setName(""); client.createDatabase(database); // Throwing InvalidObjectException would be more appropriate, but we do not change the API }
@Test(expected = MetaException.class) public void testCreateDatabaseNullName() throws Exception { Database database = testDatabases[0]; // Missing class setting field database.setName(null); client.createDatabase(database); // Throwing InvalidObjectException would be more appropriate, but we do not change the API }
@Override public void createDatabase(HCatCreateDBDesc dbInfo) throws HCatException { try { hmsClient.createDatabase(dbInfo.toHiveDb()); } catch (AlreadyExistsException exp) { if (!dbInfo.getIfNotExists()) { throw new HCatException( "AlreadyExistsException while creating database", exp); } } catch (InvalidObjectException exp) { throw new HCatException( "InvalidObjectException while creating database", exp); } catch (MetaException exp) { throw new HCatException("MetaException while creating database", exp); } catch (TException exp) { throw new ConnectionFailureException( "TException while creating database", exp); } }
@Test public void testDropDatabaseCaseInsensitive() throws Exception { Database database = testDatabases[0]; // Test in upper case client.dropDatabase(database.getName().toUpperCase()); List<String> allDatabases = client.getAllDatabases(); Assert.assertEquals("All databases size", 4, allDatabases.size()); // Test in mixed case client.createDatabase(database); client.dropDatabase("TesT_DatABaSe_1"); allDatabases = client.getAllDatabases(); Assert.assertEquals("All databases size", 4, allDatabases.size()); }
public Database dropAndCreate(IMetaStoreClient metaStoreClient) throws Exception { if (metaStoreClient == null) { throw new IllegalArgumentException(); } try { for (String table : metaStoreClient.listTableNamesByFilter(database.getName(), "", (short) -1)) { metaStoreClient.dropTable(database.getName(), table, true, true); } metaStoreClient.dropDatabase(database.getName()); } catch (TException e) { } metaStoreClient.createDatabase(database); return database; }
@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 getTableMeta() throws TException { String dbName = "db9"; // For this one don't specify a location to make sure it gets put in the catalog directory Database db = new DatabaseBuilder() .setName(dbName) .build(conf); db.unsetCatalogName(); client.createDatabase(db); String[] tableNames = {"table_in_other_catalog_1", "table_in_other_catalog_2", "random_name"}; List<TableMeta> expected = new ArrayList<>(tableNames.length); for (int i = 0; i < tableNames.length; i++) { Table table = new TableBuilder() .inDb(db) .setTableName(tableNames[i]) .addCol("id", "int") .addCol("name", "string") .build(conf); table.unsetCatName(); client.createTable(table); TableMeta tableMeta = new TableMeta(dbName, tableNames[i], TableType.MANAGED_TABLE.name()); tableMeta.setCatName(expectedCatalog()); expected.add(tableMeta); } List<String> types = Collections.singletonList(TableType.MANAGED_TABLE.name()); List<TableMeta> actual = client.getTableMeta(dbName, "*", types); Assert.assertEquals(new TreeSet<>(expected), new TreeSet<>(actual)); actual = client.getTableMeta("*", "table_*", types); Assert.assertEquals(expected.subList(0, 2), actual.subList(0, 2)); }
@Test public void testDropDatabaseDeleteData() throws Exception { Database database = testDatabases[0]; Path dataFile = new Path(database.getLocationUri() + "/dataFile"); metaStore.createFile(dataFile, "100"); // Do not delete the data client.dropDatabase(database.getName(), false, false); // Check that the data still exist Assert.assertTrue("The data file should still exist", metaStore.isPathExists(dataFile)); // Recreate the database client.createDatabase(database); Assert.assertTrue("The data file should still exist", metaStore.isPathExists(dataFile)); // Delete the data client.dropDatabase(database.getName(), true, false); // Check that the data is removed Assert.assertFalse("The data file should not exist", metaStore.isPathExists(dataFile)); }
@Test public void tablesGetExists() throws TException { String dbName = "db_in_other_catalog"; // For this one don't specify a location to make sure it gets put in the catalog directory Database db = new DatabaseBuilder() .setName(dbName) .build(conf); db.unsetCatalogName(); client.createDatabase(db); String[] tableNames = new String[4]; for (int i = 0; i < tableNames.length; i++) { tableNames[i] = "table_in_other_catalog_" + i; Table table = new TableBuilder() .inDb(db) .setTableName(tableNames[i]) .addCol("col1_" + i, ColumnType.STRING_TYPE_NAME) .addCol("col2_" + i, ColumnType.INT_TYPE_NAME) .build(conf); table.unsetCatName(); client.createTable(table); } Set<String> tables = new HashSet<>(client.getTables(dbName, "*e_in_other_*")); Assert.assertEquals(4, tables.size()); for (String tableName : tableNames) Assert.assertTrue(tables.contains(tableName)); List<String> fetchedNames = client.getTables(dbName, "*_3"); Assert.assertEquals(1, fetchedNames.size()); Assert.assertEquals(tableNames[3], fetchedNames.get(0)); Assert.assertTrue("Table exists", client.tableExists(dbName, tableNames[0])); Assert.assertFalse("Table not exists", client.tableExists(dbName, "non_existing_table")); }
db.setName(databaseName); db.setLocationUri(dbLocation); client.createDatabase(db);
@Test public void testTxNWithKeyWrongPrefix() throws Exception { String dbName = "mydbKeyValueWrongPrefix"; String tblName = "mytable"; List<Long> tids = client.openTxns("me", 1).getTxn_ids(); Assert.assertEquals(1L, (long) tids.get(0)); try { Database db = new DatabaseBuilder().setName(dbName).build(conf); db.unsetCatalogName(); client.createDatabase(db); Table tbl = new TableBuilder().setDbName(dbName).setTableName(tblName) .addCol("id", "int").addCol("name", "string") .setType(TableType.MANAGED_TABLE.name()).build(conf); client.createTable(tbl); tbl = client.getTable(dbName, tblName); client.commitTxnWithKeyValue(1, tbl.getId(), "mykey", "myvalue"); Assert.fail("Should have raised exception"); } catch (IllegalArgumentException e) { Assert.assertTrue(e.getMessage().contains("key=mykey")); Assert.assertTrue(e.getMessage().contains("value=myvalue")); Assert.assertTrue(e.getMessage().contains("key should start with")); } finally { client.dropTable(dbName, tblName); client.dropDatabase(dbName); } ValidTxnList validTxns = client.getValidTxns(1); Assert.assertTrue(validTxns.isTxnValid(1)); }
@Test public void tablesList() throws TException { String dbName = "db_in_other_catalog"; // For this one don't specify a location to make sure it gets put in the catalog directory Database db = new DatabaseBuilder() .setName(dbName) .build(conf); db.unsetCatalogName(); client.createDatabase(db); String[] tableNames = new String[4]; for (int i = 0; i < tableNames.length; i++) { tableNames[i] = "table_in_other_catalog_" + i; TableBuilder builder = new TableBuilder() .inDb(db) .setTableName(tableNames[i]) .addCol("col1_" + i, ColumnType.STRING_TYPE_NAME) .addCol("col2_" + i, ColumnType.INT_TYPE_NAME); if (i == 0) builder.addTableParam("the_key", "the_value"); Table table = builder.build(conf); table.unsetCatName(); client.createTable(table); } String filter = hive_metastoreConstants.HIVE_FILTER_FIELD_PARAMS + "the_key=\"the_value\""; List<String> fetchedNames = client.listTableNamesByFilter(dbName, filter, (short)-1); Assert.assertEquals(1, fetchedNames.size()); Assert.assertEquals(tableNames[0], fetchedNames.get(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 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()); }