Search in sources :

Example 16 with CatalogBuilder

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

the class TestPrimaryKey method setUp.

@Before
public void setUp() throws Exception {
    // Get new client
    client = metaStore.getClient();
    // Clean up the database
    client.dropDatabase(OTHER_DATABASE, true, true, true);
    // Drop every table in the default database
    for (String tableName : client.getAllTables(DEFAULT_DATABASE_NAME)) {
        client.dropTable(DEFAULT_DATABASE_NAME, tableName, true, true, true);
    }
    client.dropDatabase(OTHER_CATALOG, DATABASE_IN_OTHER_CATALOG, true, true, true);
    try {
        client.dropCatalog(OTHER_CATALOG);
    } catch (NoSuchObjectException e) {
    // NOP
    }
    // Clean up trash
    metaStore.cleanWarehouseDirs();
    new DatabaseBuilder().setName(OTHER_DATABASE).create(client, metaStore.getConf());
    Catalog cat = new CatalogBuilder().setName(OTHER_CATALOG).setLocation(MetaStoreTestUtils.getTestWarehouseDir(OTHER_CATALOG)).build();
    client.createCatalog(cat);
    // For this one don't specify a location to make sure it gets put in the catalog directory
    inOtherCatalog = new DatabaseBuilder().setName(DATABASE_IN_OTHER_CATALOG).setCatalogName(OTHER_CATALOG).create(client, metaStore.getConf());
    testTables[0] = new TableBuilder().setTableName("test_table_1").addCol("col1", "int").addCol("col2", "varchar(32)").create(client, metaStore.getConf());
    testTables[1] = new TableBuilder().setDbName(OTHER_DATABASE).setTableName("test_table_2").addCol("col1", "int").addCol("col2", "varchar(32)").create(client, metaStore.getConf());
    testTables[2] = new TableBuilder().inDb(inOtherCatalog).setTableName("test_table_3").addCol("col1", "int").addCol("col2", "varchar(32)").create(client, metaStore.getConf());
    // Reload tables from the MetaStore
    for (int i = 0; i < testTables.length; i++) {
        testTables[i] = client.getTable(testTables[i].getCatName(), testTables[i].getDbName(), testTables[i].getTableName());
    }
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) CatalogBuilder(org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) Catalog(org.apache.hadoop.hive.metastore.api.Catalog) Before(org.junit.Before)

Example 17 with CatalogBuilder

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

the class TestFunctions method otherCatalog.

@Test
public void otherCatalog() throws TException {
    String catName = "functions_catalog";
    Catalog cat = new CatalogBuilder().setName(catName).setLocation(MetaStoreTestUtils.getTestWarehouseDir(catName)).build();
    client.createCatalog(cat);
    String dbName = "functions_other_catalog_db";
    Database db = new DatabaseBuilder().setCatalogName(catName).setName(dbName).create(client, metaStore.getConf());
    String functionName = "test_function";
    Function function = new FunctionBuilder().inDb(db).setName(functionName).setClass(TEST_FUNCTION_CLASS).setFunctionType(FunctionType.JAVA).setOwnerType(PrincipalType.ROLE).setOwner("owner").setCreateTime(100).addResourceUri(new ResourceUri(ResourceType.JAR, "hdfs:///tmp/jar1.jar")).addResourceUri(new ResourceUri(ResourceType.FILE, "hdfs:///tmp/file1.txt")).addResourceUri(new ResourceUri(ResourceType.ARCHIVE, "hdfs:///tmp/archive1.tgz")).create(client, metaStore.getConf());
    Function createdFunction = client.getFunction(catName, dbName, functionName);
    // The createTime will be set on the server side, so the comparison should skip it
    function.setCreateTime(createdFunction.getCreateTime());
    Assert.assertEquals("Comparing functions", function, createdFunction);
    String f2Name = "testy_function2";
    Function f2 = new FunctionBuilder().inDb(db).setName(f2Name).setClass(TEST_FUNCTION_CLASS).create(client, metaStore.getConf());
    Set<String> functions = new HashSet<>(client.getFunctions(catName, dbName, "test*"));
    Assert.assertEquals(2, functions.size());
    Assert.assertTrue(functions.contains(functionName));
    Assert.assertTrue(functions.contains(f2Name));
    functions = new HashSet<>(client.getFunctions(catName, dbName, "test_*"));
    Assert.assertEquals(1, functions.size());
    Assert.assertTrue(functions.contains(functionName));
    Assert.assertFalse(functions.contains(f2Name));
    client.dropFunction(function.getCatName(), function.getDbName(), function.getFunctionName());
    try {
        client.getFunction(function.getCatName(), function.getDbName(), function.getFunctionName());
        Assert.fail("Expected a NoSuchObjectException to be thrown");
    } catch (NoSuchObjectException exception) {
    // Expected exception
    }
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) Function(org.apache.hadoop.hive.metastore.api.Function) ResourceUri(org.apache.hadoop.hive.metastore.api.ResourceUri) FunctionBuilder(org.apache.hadoop.hive.metastore.client.builder.FunctionBuilder) 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) Catalog(org.apache.hadoop.hive.metastore.api.Catalog) HashSet(java.util.HashSet) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 18 with CatalogBuilder

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

the class TestGetTableMeta method tablesInDifferentCatalog.

@Test
public void tablesInDifferentCatalog() throws TException {
    String catName = "get_table_meta_catalog";
    Catalog cat = new CatalogBuilder().setName(catName).setLocation(MetaStoreTestUtils.getTestWarehouseDir(catName)).build();
    client.createCatalog(cat);
    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).setCatalogName(catName).create(client, metaStore.getConf());
    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++) {
        client.createTable(new TableBuilder().inDb(db).setTableName(tableNames[i]).addCol("id", "int").addCol("name", "string").build(metaStore.getConf()));
        Table table = client.getTable(catName, dbName, tableNames[i]);
        TableMeta tableMeta = new TableMeta(dbName, tableNames[i], table.getTableType());
        tableMeta.setCatName(catName);
        expected.add(tableMeta);
    }
    List<String> typesList = Lists.newArrayList(TableType.MANAGED_TABLE.name(), TableType.EXTERNAL_TABLE.name());
    List<String> types = Collections.unmodifiableList(typesList);
    List<TableMeta> actual = client.getTableMeta(catName, dbName, "*", types);
    assertTableMetas(expected, actual, 0, 1, 2);
    actual = client.getTableMeta(catName, "*", "table_*", types);
    assertTableMetas(expected, actual, 0, 1);
    actual = client.getTableMeta(dbName, "table_in_other_catalog_*", types);
    assertTableMetas(expected, actual);
}
Also used : Table(org.apache.hadoop.hive.metastore.api.Table) ArrayList(java.util.ArrayList) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) 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) TableMeta(org.apache.hadoop.hive.metastore.api.TableMeta) MetastoreUnitTest(org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 19 with CatalogBuilder

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

the class TestDefaultConstraint method setUp.

@Before
public void setUp() throws Exception {
    // Get new client
    client = metaStore.getClient();
    // Clean up the database
    client.dropDatabase(OTHER_DATABASE, true, true, true);
    // Drop every table in the default database
    for (String tableName : client.getAllTables(DEFAULT_DATABASE_NAME)) {
        client.dropTable(DEFAULT_DATABASE_NAME, tableName, true, true, true);
    }
    client.dropDatabase(OTHER_CATALOG, DATABASE_IN_OTHER_CATALOG, true, true, true);
    try {
        client.dropCatalog(OTHER_CATALOG);
    } catch (NoSuchObjectException e) {
    // NOP
    }
    // Clean up trash
    metaStore.cleanWarehouseDirs();
    new DatabaseBuilder().setName(OTHER_DATABASE).create(client, metaStore.getConf());
    Catalog cat = new CatalogBuilder().setName(OTHER_CATALOG).setLocation(MetaStoreTestUtils.getTestWarehouseDir(OTHER_CATALOG)).build();
    client.createCatalog(cat);
    // For this one don't specify a location to make sure it gets put in the catalog directory
    inOtherCatalog = new DatabaseBuilder().setName(DATABASE_IN_OTHER_CATALOG).setCatalogName(OTHER_CATALOG).create(client, metaStore.getConf());
    testTables[0] = new TableBuilder().setTableName("test_table_1").addCol("col1", "int").addCol("col2", "varchar(32)").create(client, metaStore.getConf());
    testTables[1] = new TableBuilder().setDbName(OTHER_DATABASE).setTableName("test_table_2").addCol("col1", "int").addCol("col2", "varchar(32)").create(client, metaStore.getConf());
    testTables[2] = new TableBuilder().inDb(inOtherCatalog).setTableName("test_table_3").addCol("col1", "int").addCol("col2", "varchar(32)").create(client, metaStore.getConf());
    // Reload tables from the MetaStore
    for (int i = 0; i < testTables.length; i++) {
        testTables[i] = client.getTable(testTables[i].getCatName(), testTables[i].getDbName(), testTables[i].getTableName());
    }
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) CatalogBuilder(org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) Catalog(org.apache.hadoop.hive.metastore.api.Catalog) SQLDefaultConstraint(org.apache.hadoop.hive.metastore.api.SQLDefaultConstraint) Before(org.junit.Before)

Example 20 with CatalogBuilder

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

the class TestPartitionManagement method createMetadata.

private List<String> createMetadata(String catName, String dbName, String tableName, List<String> partKeys, List<String> partKeyTypes, List<List<String>> partVals, Map<String, Column> colMap, boolean isOrc) throws TException {
    if (!DEFAULT_CATALOG_NAME.equals(catName)) {
        Catalog cat = new CatalogBuilder().setName(catName).setLocation(MetaStoreTestUtils.getTestWarehouseDir(catName)).build();
        client.createCatalog(cat);
    }
    Database db;
    if (!DEFAULT_DATABASE_NAME.equals(dbName)) {
        DatabaseBuilder dbBuilder = new DatabaseBuilder().setName(dbName);
        dbBuilder.setCatalogName(catName);
        db = dbBuilder.create(client, conf);
    } else {
        db = client.getDatabase(DEFAULT_CATALOG_NAME, DEFAULT_DATABASE_NAME);
    }
    TableBuilder tb = new TableBuilder().inDb(db).setTableName(tableName);
    if (isOrc) {
        tb.setInputFormat("org.apache.hadoop.hive.ql.io.orc.OrcInputFormat").setOutputFormat("org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat");
    }
    for (Column col : colMap.values()) {
        tb.addCol(col.colName, col.colType);
    }
    if (partKeys != null) {
        if (partKeyTypes == null) {
            throw new IllegalArgumentException("partKeyTypes cannot be null when partKeys is non-null");
        }
        if (partKeys.size() != partKeyTypes.size()) {
            throw new IllegalArgumentException("partKeys and partKeyTypes size should be same");
        }
        if (partVals.isEmpty()) {
            throw new IllegalArgumentException("partVals cannot be empty for patitioned table");
        }
        for (int i = 0; i < partKeys.size(); i++) {
            tb.addPartCol(partKeys.get(i), partKeyTypes.get(i));
        }
    }
    Table table = tb.create(client, conf);
    if (partKeys != null) {
        for (List<String> partVal : partVals) {
            new PartitionBuilder().inTable(table).setValues(partVal).addToTable(client, conf);
        }
    }
    List<String> partNames = new ArrayList<>();
    if (partKeys != null) {
        for (int i = 0; i < partKeys.size(); i++) {
            String partKey = partKeys.get(i);
            for (String partVal : partVals.get(i)) {
                String partName = partKey + "=" + partVal;
                partNames.add(partName);
            }
        }
    }
    client.flushCache();
    return partNames;
}
Also used : Table(org.apache.hadoop.hive.metastore.api.Table) ArrayList(java.util.ArrayList) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) Catalog(org.apache.hadoop.hive.metastore.api.Catalog) DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) 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)

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