Search in sources :

Example 26 with CatalogBuilder

use of org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder in project hive by apache.

the class TestDatabases method databasesInCatalogs.

@Test
public void databasesInCatalogs() throws TException, URISyntaxException {
    String catName = "mycatalog";
    Catalog cat = new CatalogBuilder().setName(catName).setLocation(MetaStoreTestUtils.getTestWarehouseDir(catName)).build();
    client.createCatalog(cat);
    String[] dbNames = { "db1", "db9" };
    Database[] dbs = new Database[2];
    // For this one don't specify a location to make sure it gets put in the catalog directory
    dbs[0] = new DatabaseBuilder().setName(dbNames[0]).setCatalogName(catName).create(client, metaStore.getConf());
    // For the second one, explicitly set a location to make sure it ends up in the specified place.
    String db1Location = MetaStoreTestUtils.getTestWarehouseDir(dbNames[1]);
    dbs[1] = new DatabaseBuilder().setName(dbNames[1]).setCatalogName(catName).setLocation(db1Location).create(client, metaStore.getConf());
    Database fetched = client.getDatabase(catName, dbNames[0]);
    String expectedLocation = new File(cat.getLocationUri(), dbNames[0] + ".db").toURI().toString();
    Assert.assertEquals(expectedLocation, fetched.getLocationUri() + "/");
    String db0Location = new URI(fetched.getLocationUri()).getPath();
    File dir = new File(db0Location);
    Assert.assertTrue(dir.exists() && dir.isDirectory());
    fetched = client.getDatabase(catName, dbNames[1]);
    Assert.assertEquals(new File(db1Location).toURI().toString(), fetched.getLocationUri() + "/");
    dir = new File(new URI(fetched.getLocationUri()).getPath());
    Assert.assertTrue(dir.exists() && dir.isDirectory());
    Set<String> fetchedDbs = new HashSet<>(client.getAllDatabases(catName));
    Assert.assertEquals(3, fetchedDbs.size());
    for (String dbName : dbNames) Assert.assertTrue(fetchedDbs.contains(dbName));
    fetchedDbs = new HashSet<>(client.getAllDatabases());
    Assert.assertEquals(5, fetchedDbs.size());
    Assert.assertTrue(fetchedDbs.contains(Warehouse.DEFAULT_DATABASE_NAME));
    // Intentionally using the deprecated method to make sure it returns correct results.
    fetchedDbs = new HashSet<>(client.getAllDatabases());
    Assert.assertEquals(5, fetchedDbs.size());
    Assert.assertTrue(fetchedDbs.contains(Warehouse.DEFAULT_DATABASE_NAME));
    fetchedDbs = new HashSet<>(client.getDatabases(catName, "d*"));
    Assert.assertEquals(3, fetchedDbs.size());
    for (String dbName : dbNames) Assert.assertTrue(fetchedDbs.contains(dbName));
    fetchedDbs = new HashSet<>(client.getDatabases("d*"));
    Assert.assertEquals(1, fetchedDbs.size());
    Assert.assertTrue(fetchedDbs.contains(Warehouse.DEFAULT_DATABASE_NAME));
    // Intentionally using the deprecated method to make sure it returns correct results.
    fetchedDbs = new HashSet<>(client.getDatabases("d*"));
    Assert.assertEquals(1, fetchedDbs.size());
    Assert.assertTrue(fetchedDbs.contains(Warehouse.DEFAULT_DATABASE_NAME));
    fetchedDbs = new HashSet<>(client.getDatabases(catName, "*1"));
    Assert.assertEquals(1, fetchedDbs.size());
    Assert.assertTrue(fetchedDbs.contains(dbNames[0]));
    fetchedDbs = new HashSet<>(client.getDatabases("*9"));
    Assert.assertEquals(0, fetchedDbs.size());
    // Intentionally using the deprecated method to make sure it returns correct results.
    fetchedDbs = new HashSet<>(client.getDatabases("*9"));
    Assert.assertEquals(0, fetchedDbs.size());
    fetchedDbs = new HashSet<>(client.getDatabases(catName, "*x"));
    Assert.assertEquals(0, fetchedDbs.size());
    // Check that dropping database from wrong catalog fails
    try {
        client.dropDatabase(dbNames[0], true, false, false);
        Assert.fail();
    } catch (NoSuchObjectException e) {
    // NOP
    }
    // Check that dropping database from wrong catalog fails
    try {
        // Intentionally using deprecated method
        client.dropDatabase(dbNames[0], true, false, false);
        Assert.fail();
    } catch (NoSuchObjectException e) {
    // NOP
    }
    // Drop them from the proper catalog
    client.dropDatabase(catName, dbNames[0], true, false, false);
    dir = new File(db0Location);
    Assert.assertFalse(dir.exists());
    client.dropDatabase(catName, dbNames[1], true, false, false);
    dir = new File(db1Location);
    Assert.assertFalse(dir.exists());
    fetchedDbs = new HashSet<>(client.getAllDatabases(catName));
    Assert.assertEquals(1, fetchedDbs.size());
}
Also used : URI(java.net.URI) Catalog(org.apache.hadoop.hive.metastore.api.Catalog) DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) CatalogBuilder(org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder) Database(org.apache.hadoop.hive.metastore.api.Database) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 27 with CatalogBuilder

use of org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder in project hive by apache.

the class TestAlterPartitions method otherCatalog.

@Test
@ConditionalIgnoreOnSessionHiveMetastoreClient
public void otherCatalog() throws TException {
    String catName = "alter_partition_catalog";
    Catalog cat = new CatalogBuilder().setName(catName).setLocation(MetaStoreTestUtils.getTestWarehouseDir(catName)).build();
    client.createCatalog(cat);
    String dbName = "alter_partition_database_in_other_catalog";
    Database db = new DatabaseBuilder().setName(dbName).setCatalogName(catName).create(client, metaStore.getConf());
    String tableName = "table_in_other_catalog";
    Table table = new TableBuilder().inDb(db).setTableName(tableName).addCol("id", "int").addCol("name", "string").addPartCol("partcol", "string").create(client, metaStore.getConf());
    Partition[] parts = new Partition[5];
    for (int i = 0; i < 5; i++) {
        parts[i] = new PartitionBuilder().inTable(table).addValue("a" + i).setLocation(MetaStoreTestUtils.getTestWarehouseDir("b" + i)).build(metaStore.getConf());
    }
    client.add_partitions(Arrays.asList(parts));
    Partition newPart = client.getPartition(catName, dbName, tableName, Collections.singletonList("a0"));
    newPart.getParameters().put("test_key", "test_value");
    client.alter_partition(catName, dbName, tableName, newPart);
    Partition fetched = client.getPartition(catName, dbName, tableName, Collections.singletonList("a0"));
    Assert.assertEquals(catName, fetched.getCatName());
    Assert.assertEquals("test_value", fetched.getParameters().get("test_key"));
    newPart = client.getPartition(catName, dbName, tableName, Collections.singletonList("a1"));
    newPart.setLastAccessTime(3);
    Partition newPart1 = client.getPartition(catName, dbName, tableName, Collections.singletonList("a2"));
    newPart1.getSd().setLocation(MetaStoreTestUtils.getTestWarehouseDir("somewhere"));
    client.alter_partitions(catName, dbName, tableName, Arrays.asList(newPart, newPart1));
    fetched = client.getPartition(catName, dbName, tableName, Collections.singletonList("a1"));
    Assert.assertEquals(catName, fetched.getCatName());
    Assert.assertEquals(3L, fetched.getLastAccessTime());
    fetched = client.getPartition(catName, dbName, tableName, Collections.singletonList("a2"));
    Assert.assertEquals(catName, fetched.getCatName());
    Assert.assertTrue(fetched.getSd().getLocation().contains("somewhere"));
    newPart = client.getPartition(catName, dbName, tableName, Collections.singletonList("a4"));
    newPart.getParameters().put("test_key", "test_value");
    EnvironmentContext ec = new EnvironmentContext();
    ec.setProperties(Collections.singletonMap("a", "b"));
    client.alter_partition(catName, dbName, tableName, newPart, ec);
    fetched = client.getPartition(catName, dbName, tableName, Collections.singletonList("a4"));
    Assert.assertEquals(catName, fetched.getCatName());
    Assert.assertEquals("test_value", fetched.getParameters().get("test_key"));
    client.dropDatabase(catName, dbName, true, true, true);
    client.dropCatalog(catName);
}
Also used : EnvironmentContext(org.apache.hadoop.hive.metastore.api.EnvironmentContext) DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) Partition(org.apache.hadoop.hive.metastore.api.Partition) Table(org.apache.hadoop.hive.metastore.api.Table) PartitionBuilder(org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder) CatalogBuilder(org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder) Database(org.apache.hadoop.hive.metastore.api.Database) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) Catalog(org.apache.hadoop.hive.metastore.api.Catalog) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 28 with CatalogBuilder

use of org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder in project hive by apache.

the class TestAddPartitions method addPartitionOtherCatalog.

@Test
@ConditionalIgnoreOnSessionHiveMetastoreClient
public void addPartitionOtherCatalog() throws TException {
    String catName = "add_partition_catalog";
    Catalog cat = new CatalogBuilder().setName(catName).setLocation(MetaStoreTestUtils.getTestWarehouseDir(catName)).build();
    client.createCatalog(cat);
    String dbName = "add_partition_database_in_other_catalog";
    Database db = new DatabaseBuilder().setName(dbName).setCatalogName(catName).create(client, metaStore.getConf());
    String tableName = "table_in_other_catalog";
    Table table = new TableBuilder().inDb(db).setTableName(tableName).addCol("id", "int").addCol("name", "string").addPartCol("partcol", "string").create(client, metaStore.getConf());
    Partition[] parts = new Partition[5];
    for (int i = 0; i < parts.length; i++) {
        parts[i] = new PartitionBuilder().inTable(table).addValue("a" + i).build(metaStore.getConf());
    }
    client.add_partition(parts[0]);
    Assert.assertEquals(2, client.add_partitions(Arrays.asList(parts[1], parts[2])));
    client.add_partitions(Arrays.asList(parts), true, false);
    for (int i = 0; i < parts.length; i++) {
        Partition fetched = client.getPartition(catName, dbName, tableName, Collections.singletonList("a" + i));
        Assert.assertEquals(catName, fetched.getCatName());
        Assert.assertEquals(dbName, fetched.getDbName());
        Assert.assertEquals(tableName, fetched.getTableName());
    }
    client.dropDatabase(catName, dbName, true, true, true);
    client.dropCatalog(catName);
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) Partition(org.apache.hadoop.hive.metastore.api.Partition) Table(org.apache.hadoop.hive.metastore.api.Table) PartitionBuilder(org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder) CatalogBuilder(org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder) Database(org.apache.hadoop.hive.metastore.api.Database) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) Catalog(org.apache.hadoop.hive.metastore.api.Catalog) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 29 with CatalogBuilder

use of org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder in project hive by apache.

the class TestCatalogs method createCatalogWithBadLocation.

@Test(expected = MetaException.class)
// TODO This test passes fine locally but fails on Linux, not sure why
@Ignore
public void createCatalogWithBadLocation() throws TException {
    Catalog cat = new CatalogBuilder().setName("goodluck").setLocation("/nosuch/nosuch").build();
    client.createCatalog(cat);
}
Also used : CatalogBuilder(org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder) Catalog(org.apache.hadoop.hive.metastore.api.Catalog) Ignore(org.junit.Ignore) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 30 with CatalogBuilder

use of org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder in project hive by apache.

the class TestSchemaToolCatalogOps method moveTable.

@Test
public void moveTable() throws TException, HiveMetaException {
    String toCatName = "moveTableCat";
    String toDbName = "moveTableDb";
    String tableName = "moveTableTable";
    String partVal = "moveTableKey";
    new CatalogBuilder().setName(toCatName).setLocation("file:///tmp").create(client);
    new DatabaseBuilder().setCatalogName(toCatName).setName(toDbName).create(client, conf);
    Table table = new TableBuilder().setTableName(tableName).addCol("a", "int").addPartCol("p", "string").create(client, conf);
    new PartitionBuilder().inTable(table).addValue(partVal).addToTable(client, conf);
    String argsMoveTable = String.format("-moveTable %s -fromCatalog %s -toCatalog %s -fromDatabase %s -toDatabase %s", tableName, DEFAULT_CATALOG_NAME, toCatName, DEFAULT_DATABASE_NAME, toDbName);
    execute(new SchemaToolTaskMoveTable(), argsMoveTable);
    Table fetchedTable = client.getTable(toCatName, toDbName, tableName);
    Assert.assertNotNull(fetchedTable);
    Assert.assertEquals(toCatName.toLowerCase(), fetchedTable.getCatName());
    Assert.assertEquals(toDbName.toLowerCase(), fetchedTable.getDbName());
    Partition fetchedPart = client.getPartition(toCatName, toDbName, tableName, Collections.singletonList(partVal));
    Assert.assertNotNull(fetchedPart);
    Assert.assertEquals(toCatName.toLowerCase(), fetchedPart.getCatName());
    Assert.assertEquals(toDbName.toLowerCase(), fetchedPart.getDbName());
    Assert.assertEquals(tableName.toLowerCase(), fetchedPart.getTableName());
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) Partition(org.apache.hadoop.hive.metastore.api.Partition) Table(org.apache.hadoop.hive.metastore.api.Table) PartitionBuilder(org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder) CatalogBuilder(org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) Test(org.junit.Test)

Aggregations

CatalogBuilder (org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder)43 Catalog (org.apache.hadoop.hive.metastore.api.Catalog)34 DatabaseBuilder (org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder)30 Test (org.junit.Test)28 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)23 TableBuilder (org.apache.hadoop.hive.metastore.client.builder.TableBuilder)23 Database (org.apache.hadoop.hive.metastore.api.Database)19 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)13 Table (org.apache.hadoop.hive.metastore.api.Table)10 PartitionBuilder (org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder)10 Before (org.junit.Before)8 Partition (org.apache.hadoop.hive.metastore.api.Partition)6 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 File (java.io.File)4 ISchema (org.apache.hadoop.hive.metastore.api.ISchema)4 ISchemaBuilder (org.apache.hadoop.hive.metastore.client.builder.ISchemaBuilder)4 SchemaVersion (org.apache.hadoop.hive.metastore.api.SchemaVersion)3 SchemaVersionBuilder (org.apache.hadoop.hive.metastore.client.builder.SchemaVersionBuilder)3 URI (java.net.URI)2