Search in sources :

Example 1 with FunctionBuilder

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

the class TestSchemaToolCatalogOps method moveDatabase.

@Test
public void moveDatabase() throws HiveMetaException, TException {
    String toCatName = "moveDbCat";
    String dbName = "moveDbDb";
    String tableName = "moveDbTable";
    String funcName = "movedbfunc";
    String partVal = "moveDbKey";
    new CatalogBuilder().setName(toCatName).setLocation("file:///tmp").create(client);
    Database db = new DatabaseBuilder().setCatalogName(DEFAULT_CATALOG_NAME).setName(dbName).create(client, conf);
    new FunctionBuilder().inDb(db).setName(funcName).setClass("org.apache.hive.myudf").create(client, conf);
    Table table = new TableBuilder().inDb(db).setTableName(tableName).addCol("a", "int").addPartCol("p", "string").create(client, conf);
    new PartitionBuilder().inTable(table).addValue(partVal).addToTable(client, conf);
    String argsMoveDB = String.format("-moveDatabase %s -fromCatalog %s -toCatalog %s", dbName, DEFAULT_CATALOG_NAME, toCatName);
    execute(new SchemaToolTaskMoveDatabase(), argsMoveDB);
    Database fetchedDb = client.getDatabase(toCatName, dbName);
    Assert.assertNotNull(fetchedDb);
    Assert.assertEquals(toCatName.toLowerCase(), fetchedDb.getCatalogName());
    Function fetchedFunction = client.getFunction(toCatName, dbName, funcName);
    Assert.assertNotNull(fetchedFunction);
    Assert.assertEquals(toCatName.toLowerCase(), fetchedFunction.getCatName());
    Assert.assertEquals(dbName.toLowerCase(), fetchedFunction.getDbName());
    Table fetchedTable = client.getTable(toCatName, dbName, tableName);
    Assert.assertNotNull(fetchedTable);
    Assert.assertEquals(toCatName.toLowerCase(), fetchedTable.getCatName());
    Assert.assertEquals(dbName.toLowerCase(), fetchedTable.getDbName());
    Partition fetchedPart = client.getPartition(toCatName, dbName, tableName, Collections.singletonList(partVal));
    Assert.assertNotNull(fetchedPart);
    Assert.assertEquals(toCatName.toLowerCase(), fetchedPart.getCatName());
    Assert.assertEquals(dbName.toLowerCase(), fetchedPart.getDbName());
    Assert.assertEquals(tableName.toLowerCase(), fetchedPart.getTableName());
}
Also used : DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) Function(org.apache.hadoop.hive.metastore.api.Function) 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) 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) TableBuilder(org.apache.hadoop.hive.metastore.client.builder.TableBuilder) Test(org.junit.Test)

Example 2 with FunctionBuilder

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

the class TestFunctions method testAlterFunction.

@Test
public void testAlterFunction() throws Exception {
    Function newFunction = new FunctionBuilder().setDbName(OTHER_DATABASE).setName("test_function_2").setOwner("Owner2").setOwnerType(PrincipalType.GROUP).setClass("org.apache.hadoop.hive.ql.udf.generic.GenericUDFUpper2").setFunctionType(FunctionType.JAVA).build(metaStore.getConf());
    client.alterFunction(testFunctions[0].getDbName(), testFunctions[0].getFunctionName(), newFunction);
    Function alteredFunction = client.getFunction(newFunction.getDbName(), newFunction.getFunctionName());
    // Currently this method only sets
    // - Database
    // - FunctionName
    // - OwnerName
    // - OwnerType
    // - ClassName
    // - FunctionType
    Assert.assertEquals("Comparing Database", newFunction.getDbName(), alteredFunction.getDbName());
    Assert.assertEquals("Comparing FunctionName", newFunction.getFunctionName(), alteredFunction.getFunctionName());
    Assert.assertEquals("Comparing OwnerName", newFunction.getOwnerName(), alteredFunction.getOwnerName());
    Assert.assertEquals("Comparing OwnerType", newFunction.getOwnerType(), alteredFunction.getOwnerType());
    Assert.assertEquals("Comparing ClassName", newFunction.getClassName(), alteredFunction.getClassName());
    Assert.assertEquals("Comparing FunctionType", newFunction.getFunctionType(), alteredFunction.getFunctionType());
    try {
        client.getFunction(testFunctions[0].getDbName(), testFunctions[0].getDbName());
        Assert.fail("Expected a NoSuchObjectException to be thrown");
    } catch (NoSuchObjectException exception) {
    // Expected exception
    }
    // Test that not changing the database and the function name, but only other parameters, like
    // function class will not cause Exception
    newFunction = testFunctions[1].deepCopy();
    newFunction.setClassName("NewClassName");
    client.alterFunction(testFunctions[1].getDbName(), testFunctions[1].getFunctionName(), newFunction);
    alteredFunction = client.getFunction(newFunction.getDbName(), newFunction.getFunctionName());
    Assert.assertEquals("Comparing functions", newFunction, alteredFunction);
}
Also used : Function(org.apache.hadoop.hive.metastore.api.Function) FunctionBuilder(org.apache.hadoop.hive.metastore.client.builder.FunctionBuilder) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Example 3 with FunctionBuilder

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

the class TestFunctions 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);
    for (Function function : client.getAllFunctions().getFunctions()) {
        client.dropFunction(function.getDbName(), function.getFunctionName());
    }
    testFunctions[0] = new FunctionBuilder().setDbName(DEFAULT_DATABASE).setName("test_function_to_find_1").setClass(TEST_FUNCTION_CLASS).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")).build(metaStore.getConf());
    testFunctions[1] = new FunctionBuilder().setDbName(DEFAULT_DATABASE).setName("test_function_to_find_2").setClass(TEST_FUNCTION_CLASS).build(metaStore.getConf());
    testFunctions[2] = new FunctionBuilder().setDbName(DEFAULT_DATABASE).setName("test_function_hidden_1").setClass(TEST_FUNCTION_CLASS).build(metaStore.getConf());
    new DatabaseBuilder().setName(OTHER_DATABASE).create(client, metaStore.getConf());
    testFunctions[3] = new FunctionBuilder().setDbName(OTHER_DATABASE).setName("test_function_to_find_1").setClass(TEST_FUNCTION_CLASS).build(metaStore.getConf());
    // Create the functions, and reload them from the MetaStore
    for (int i = 0; i < testFunctions.length; i++) {
        client.createFunction(testFunctions[i]);
        testFunctions[i] = client.getFunction(testFunctions[i].getDbName(), testFunctions[i].getFunctionName());
    }
}
Also used : Function(org.apache.hadoop.hive.metastore.api.Function) ResourceUri(org.apache.hadoop.hive.metastore.api.ResourceUri) DatabaseBuilder(org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder) FunctionBuilder(org.apache.hadoop.hive.metastore.client.builder.FunctionBuilder) Before(org.junit.Before)

Example 4 with FunctionBuilder

use of org.apache.hadoop.hive.metastore.client.builder.FunctionBuilder 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 5 with FunctionBuilder

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

the class TestFunctions method addNoSuchCatalog.

@Test(expected = NoSuchObjectException.class)
public void addNoSuchCatalog() throws TException {
    String functionName = "test_function";
    new FunctionBuilder().setName(functionName).setCatName("nosuch").setDbName(DEFAULT_DATABASE_NAME).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());
}
Also used : ResourceUri(org.apache.hadoop.hive.metastore.api.ResourceUri) FunctionBuilder(org.apache.hadoop.hive.metastore.client.builder.FunctionBuilder) Test(org.junit.Test) MetastoreCheckinTest(org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)

Aggregations

FunctionBuilder (org.apache.hadoop.hive.metastore.client.builder.FunctionBuilder)10 Function (org.apache.hadoop.hive.metastore.api.Function)9 Test (org.junit.Test)9 MetastoreCheckinTest (org.apache.hadoop.hive.metastore.annotation.MetastoreCheckinTest)7 Database (org.apache.hadoop.hive.metastore.api.Database)5 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)5 ResourceUri (org.apache.hadoop.hive.metastore.api.ResourceUri)5 DatabaseBuilder (org.apache.hadoop.hive.metastore.client.builder.DatabaseBuilder)4 HashSet (java.util.HashSet)2 CatalogBuilder (org.apache.hadoop.hive.metastore.client.builder.CatalogBuilder)2 Path (org.apache.hadoop.fs.Path)1 Catalog (org.apache.hadoop.hive.metastore.api.Catalog)1 Partition (org.apache.hadoop.hive.metastore.api.Partition)1 Table (org.apache.hadoop.hive.metastore.api.Table)1 PartitionBuilder (org.apache.hadoop.hive.metastore.client.builder.PartitionBuilder)1 TableBuilder (org.apache.hadoop.hive.metastore.client.builder.TableBuilder)1 Before (org.junit.Before)1