Search in sources :

Example 6 with Database

use of org.apache.hadoop.hive.metastore.api.Database in project hive by apache.

the class TestMetastoreAuthorizationProvider method testSimplePrivileges.

public void testSimplePrivileges() throws Exception {
    if (!isTestEnabled()) {
        System.out.println("Skipping test " + this.getClass().getName());
        return;
    }
    String dbName = getTestDbName();
    String tblName = getTestTableName();
    String userName = setupUser();
    allowCreateDatabase(userName);
    CommandProcessorResponse ret = driver.run("create database " + dbName);
    assertEquals(0, ret.getResponseCode());
    Database db = msc.getDatabase(dbName);
    String dbLocn = db.getLocationUri();
    validateCreateDb(db, dbName);
    disallowCreateInDb(dbName, userName, dbLocn);
    disallowCreateDatabase(userName);
    driver.run("use " + dbName);
    ret = driver.run(String.format("create table %s (a string) partitioned by (b string)", tblName));
    assertEquals(1, ret.getResponseCode());
    // Even if table location is specified table creation should fail
    String tblNameLoc = tblName + "_loc";
    String tblLocation = new Path(dbLocn).getParent().toUri() + "/" + tblNameLoc;
    driver.run("use " + dbName);
    ret = driver.run(String.format("create table %s (a string) partitioned by (b string) location '" + tblLocation + "'", tblNameLoc));
    assertEquals(1, ret.getResponseCode());
    // failure from not having permissions to create table
    ArrayList<FieldSchema> fields = new ArrayList<FieldSchema>(2);
    fields.add(new FieldSchema("a", serdeConstants.STRING_TYPE_NAME, ""));
    Table ttbl = new Table();
    ttbl.setDbName(dbName);
    ttbl.setTableName(tblName);
    StorageDescriptor sd = new StorageDescriptor();
    ttbl.setSd(sd);
    sd.setCols(fields);
    sd.setParameters(new HashMap<String, String>());
    sd.getParameters().put("test_param_1", "Use this for comments etc");
    sd.setSerdeInfo(new SerDeInfo());
    sd.getSerdeInfo().setName(ttbl.getTableName());
    sd.getSerdeInfo().setParameters(new HashMap<String, String>());
    sd.getSerdeInfo().getParameters().put(org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_FORMAT, "1");
    sd.getSerdeInfo().setSerializationLib(org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe.class.getName());
    sd.setInputFormat(HiveInputFormat.class.getName());
    sd.setOutputFormat(HiveOutputFormat.class.getName());
    ttbl.setPartitionKeys(new ArrayList<FieldSchema>());
    MetaException me = null;
    try {
        msc.createTable(ttbl);
    } catch (MetaException e) {
        me = e;
    }
    assertNoPrivileges(me);
    allowCreateInDb(dbName, userName, dbLocn);
    driver.run("use " + dbName);
    ret = driver.run(String.format("create table %s (a string) partitioned by (b string)", tblName));
    // now it succeeds.
    assertEquals(0, ret.getResponseCode());
    Table tbl = msc.getTable(dbName, tblName);
    validateCreateTable(tbl, tblName, dbName);
    // Table creation should succeed even if location is specified
    driver.run("use " + dbName);
    ret = driver.run(String.format("create table %s (a string) partitioned by (b string) location '" + tblLocation + "'", tblNameLoc));
    assertEquals(0, ret.getResponseCode());
    Table tblLoc = msc.getTable(dbName, tblNameLoc);
    validateCreateTable(tblLoc, tblNameLoc, dbName);
    String fakeUser = "mal";
    List<String> fakeGroupNames = new ArrayList<String>();
    fakeGroupNames.add("groupygroup");
    InjectableDummyAuthenticator.injectUserName(fakeUser);
    InjectableDummyAuthenticator.injectGroupNames(fakeGroupNames);
    InjectableDummyAuthenticator.injectMode(true);
    ret = driver.run(String.format("create table %s (a string) partitioned by (b string)", tblName + "mal"));
    assertEquals(1, ret.getResponseCode());
    ttbl.setTableName(tblName + "mal");
    me = null;
    try {
        msc.createTable(ttbl);
    } catch (MetaException e) {
        me = e;
    }
    assertNoPrivileges(me);
    disallowCreateInTbl(tbl.getTableName(), userName, tbl.getSd().getLocation());
    ret = driver.run("alter table " + tblName + " add partition (b='2011')");
    assertEquals(1, ret.getResponseCode());
    List<String> ptnVals = new ArrayList<String>();
    ptnVals.add("b=2011");
    Partition tpart = new Partition();
    tpart.setDbName(dbName);
    tpart.setTableName(tblName);
    tpart.setValues(ptnVals);
    tpart.setParameters(new HashMap<String, String>());
    tpart.setSd(tbl.getSd().deepCopy());
    tpart.getSd().setSerdeInfo(tbl.getSd().getSerdeInfo().deepCopy());
    tpart.getSd().setLocation(tbl.getSd().getLocation() + "/tpart");
    me = null;
    try {
        msc.add_partition(tpart);
    } catch (MetaException e) {
        me = e;
    }
    assertNoPrivileges(me);
    InjectableDummyAuthenticator.injectMode(false);
    allowCreateInTbl(tbl.getTableName(), userName, tbl.getSd().getLocation());
    ret = driver.run("alter table " + tblName + " add partition (b='2011')");
    assertEquals(0, ret.getResponseCode());
    allowDropOnTable(tblName, userName, tbl.getSd().getLocation());
    allowDropOnDb(dbName, userName, db.getLocationUri());
    ret = driver.run("drop database if exists " + getTestDbName() + " cascade");
    assertEquals(0, ret.getResponseCode());
    InjectableDummyAuthenticator.injectUserName(userName);
    InjectableDummyAuthenticator.injectGroupNames(Arrays.asList(ugi.getGroupNames()));
    InjectableDummyAuthenticator.injectMode(true);
    allowCreateDatabase(userName);
    driver.run("create database " + dbName);
    allowCreateInDb(dbName, userName, dbLocn);
    tbl.setTableType("EXTERNAL_TABLE");
    msc.createTable(tbl);
    disallowDropOnTable(tblName, userName, tbl.getSd().getLocation());
    ret = driver.run("drop table " + tbl.getTableName());
    assertEquals(1, ret.getResponseCode());
}
Also used : Path(org.apache.hadoop.fs.Path) Partition(org.apache.hadoop.hive.metastore.api.Partition) Table(org.apache.hadoop.hive.metastore.api.Table) CommandProcessorResponse(org.apache.hadoop.hive.ql.processors.CommandProcessorResponse) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) SerDeInfo(org.apache.hadoop.hive.metastore.api.SerDeInfo) ArrayList(java.util.ArrayList) StorageDescriptor(org.apache.hadoop.hive.metastore.api.StorageDescriptor) HiveOutputFormat(org.apache.hadoop.hive.ql.io.HiveOutputFormat) HiveInputFormat(org.apache.hadoop.hive.ql.io.HiveInputFormat) Database(org.apache.hadoop.hive.metastore.api.Database) MetaException(org.apache.hadoop.hive.metastore.api.MetaException)

Example 7 with Database

use of org.apache.hadoop.hive.metastore.api.Database in project hive by apache.

the class TestStorageBasedMetastoreAuthorizationDrops method dropTableByOtherUser.

/**
   * @param perm dir permission for database dir
   * @param expectedRet expected return code on drop table
   * @throws Exception
   */
public void dropTableByOtherUser(String perm, int expectedRet) throws Exception {
    String dbName = getTestDbName();
    String tblName = getTestTableName();
    setPermissions(clientHiveConf.getVar(ConfVars.METASTOREWAREHOUSE), "-rwxrwxrwx");
    CommandProcessorResponse resp = driver.run("create database " + dbName);
    Assert.assertEquals(0, resp.getResponseCode());
    Database db = msc.getDatabase(dbName);
    validateCreateDb(db, dbName);
    setPermissions(db.getLocationUri(), perm);
    String dbDotTable = dbName + "." + tblName;
    resp = driver.run("create table " + dbDotTable + "(i int)");
    Assert.assertEquals(0, resp.getResponseCode());
    InjectableDummyAuthenticator.injectMode(true);
    resp = driver.run("drop table " + dbDotTable);
    Assert.assertEquals(expectedRet, resp.getResponseCode());
}
Also used : CommandProcessorResponse(org.apache.hadoop.hive.ql.processors.CommandProcessorResponse) Database(org.apache.hadoop.hive.metastore.api.Database)

Example 8 with Database

use of org.apache.hadoop.hive.metastore.api.Database in project hive by apache.

the class TestHBaseStoreIntegration method getAllTables.

@Test
public void getAllTables() throws Exception {
    // named to match getAllDbs so we get the
    String[] dbNames = new String[] { "db0", "db1" };
    // right number of databases in that test.
    String[] tableNames = new String[] { "curly", "larry", "moe" };
    for (int i = 0; i < dbNames.length; i++) {
        store.createDatabase(new Database(dbNames[i], "no description", "file:///tmp", emptyParameters));
    }
    for (int i = 0; i < dbNames.length; i++) {
        for (int j = 0; j < tableNames.length; j++) {
            int startTime = (int) (System.currentTimeMillis() / 1000);
            List<FieldSchema> cols = new ArrayList<FieldSchema>();
            cols.add(new FieldSchema("col1", "int", "nocomment"));
            SerDeInfo serde = new SerDeInfo("serde", "seriallib", null);
            StorageDescriptor sd = new StorageDescriptor(cols, "file:/tmp", "input", "output", false, 0, serde, null, null, emptyParameters);
            Table table = new Table(tableNames[j], dbNames[i], "me", startTime, startTime, 0, sd, null, emptyParameters, null, null, null);
            store.createTable(table);
        }
    }
    List<String> fetchedNames = store.getAllTables(dbNames[0]);
    Assert.assertEquals(3, fetchedNames.size());
    String[] sortedFetchedNames = fetchedNames.toArray(new String[fetchedNames.size()]);
    Arrays.sort(sortedFetchedNames);
    Assert.assertArrayEquals(tableNames, sortedFetchedNames);
    List<String> regexNames = store.getTables(dbNames[0], "*y");
    Assert.assertEquals(2, regexNames.size());
    String[] sortedRegexNames = regexNames.toArray(new String[regexNames.size()]);
    Arrays.sort(sortedRegexNames);
    Assert.assertArrayEquals(Arrays.copyOfRange(tableNames, 0, 2), sortedRegexNames);
    List<Table> fetchedTables = store.getTableObjectsByName(dbNames[1], Arrays.asList(Arrays.copyOfRange(tableNames, 1, 3)));
    Assert.assertEquals(2, fetchedTables.size());
    sortedFetchedNames = new String[fetchedTables.size()];
    for (int i = 0; i < fetchedTables.size(); i++) {
        sortedFetchedNames[i] = fetchedTables.get(i).getTableName();
    }
    Arrays.sort(sortedFetchedNames);
    Assert.assertArrayEquals(Arrays.copyOfRange(tableNames, 1, 3), sortedFetchedNames);
}
Also used : Table(org.apache.hadoop.hive.metastore.api.Table) FieldSchema(org.apache.hadoop.hive.metastore.api.FieldSchema) SerDeInfo(org.apache.hadoop.hive.metastore.api.SerDeInfo) Database(org.apache.hadoop.hive.metastore.api.Database) ArrayList(java.util.ArrayList) StorageDescriptor(org.apache.hadoop.hive.metastore.api.StorageDescriptor) Test(org.junit.Test)

Example 9 with Database

use of org.apache.hadoop.hive.metastore.api.Database in project hive by apache.

the class TestHBaseStoreIntegration method getDbsRegex.

@Test
public void getDbsRegex() throws Exception {
    String[] dbNames = new String[3];
    for (int i = 0; i < dbNames.length; i++) {
        dbNames[i] = "db" + i;
        Database db = new Database(dbNames[i], "no description", "file:///tmp", emptyParameters);
        store.createDatabase(db);
    }
    List<String> dbs = store.getDatabases("db1|db2");
    Assert.assertEquals(2, dbs.size());
    String[] namesFromStore = dbs.toArray(new String[2]);
    Arrays.sort(namesFromStore);
    Assert.assertArrayEquals(Arrays.copyOfRange(dbNames, 1, 3), namesFromStore);
    dbs = store.getDatabases("db*");
    Assert.assertEquals(3, dbs.size());
    namesFromStore = dbs.toArray(new String[3]);
    Arrays.sort(namesFromStore);
    Assert.assertArrayEquals(dbNames, namesFromStore);
}
Also used : Database(org.apache.hadoop.hive.metastore.api.Database) Test(org.junit.Test)

Example 10 with Database

use of org.apache.hadoop.hive.metastore.api.Database in project hive by apache.

the class TestHBaseImport method importOneFunc.

@Test
public void importOneFunc() throws Exception {
    RawStore rdbms;
    rdbms = new ObjectStore();
    rdbms.setConf(conf);
    String[] dbNames = new String[] { "onefuncdb1", "onefuncdb2" };
    String[] roles = new String[] { "onefuncrole1", "onefuncrole2" };
    String[] tokenIds = new String[] { "onefunctokenid1", "onefunctokenid2" };
    String[] tokens = new String[] { "onefunctoken1", "onefunctoken2" };
    String[] masterKeys = new String[] { "onefuncmk1", "onefuncmk2" };
    int now = (int) System.currentTimeMillis() / 1000;
    setupObjectStore(rdbms, roles, dbNames, tokenIds, tokens, masterKeys, now);
    int baseNumRoles = store.listRoleNames() == null ? 0 : store.listRoleNames().size();
    int baseNumDbs = store.getAllDatabases() == null ? 0 : store.getAllDatabases().size();
    int baseNumToks = store.getAllTokenIdentifiers() == null ? 0 : store.getAllTokenIdentifiers().size();
    int baseNumKeys = store.getMasterKeys() == null ? 0 : store.getMasterKeys().length;
    // Create the database so I can put the function in it.
    store.createDatabase(new Database(dbNames[0], "no description", "file:/tmp", emptyParameters));
    HBaseImport importer = new HBaseImport("-f", dbNames[0] + "." + funcNames[0]);
    importer.setConnections(rdbms, store);
    importer.run();
    // Make sure there aren't any extra roles
    Assert.assertEquals(baseNumRoles, store.listRoleNames().size());
    Database db = store.getDatabase(dbNames[0]);
    Assert.assertNotNull(db);
    Assert.assertEquals(0, store.getAllTables(dbNames[0]).size());
    Assert.assertEquals(1, store.getFunctions(dbNames[0], "*").size());
    Assert.assertNotNull(store.getFunction(dbNames[0], funcNames[0]));
    Assert.assertNull(store.getFunction(dbNames[0], funcNames[1]));
    Assert.assertEquals(baseNumDbs + 1, store.getAllDatabases().size());
    Assert.assertEquals(baseNumToks, store.getAllTokenIdentifiers().size());
    String[] hbaseKeys = store.getMasterKeys();
    Assert.assertEquals(baseNumKeys, hbaseKeys.length);
}
Also used : TestObjectStore(org.apache.hadoop.hive.metastore.TestObjectStore) ObjectStore(org.apache.hadoop.hive.metastore.ObjectStore) Database(org.apache.hadoop.hive.metastore.api.Database) RawStore(org.apache.hadoop.hive.metastore.RawStore) Test(org.junit.Test)

Aggregations

Database (org.apache.hadoop.hive.metastore.api.Database)153 Table (org.apache.hadoop.hive.metastore.api.Table)49 Test (org.junit.Test)46 ArrayList (java.util.ArrayList)42 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)30 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)29 StorageDescriptor (org.apache.hadoop.hive.metastore.api.StorageDescriptor)29 SerDeInfo (org.apache.hadoop.hive.metastore.api.SerDeInfo)28 NoSuchObjectException (org.apache.hadoop.hive.metastore.api.NoSuchObjectException)24 Path (org.apache.hadoop.fs.Path)23 Partition (org.apache.hadoop.hive.metastore.api.Partition)21 AlreadyExistsException (org.apache.hadoop.hive.metastore.api.AlreadyExistsException)18 InvalidOperationException (org.apache.hadoop.hive.metastore.api.InvalidOperationException)18 HashMap (java.util.HashMap)17 TException (org.apache.thrift.TException)17 InvalidObjectException (org.apache.hadoop.hive.metastore.api.InvalidObjectException)16 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)15 IOException (java.io.IOException)14 SQLException (java.sql.SQLException)13 HiveInputFormat (org.apache.hadoop.hive.ql.io.HiveInputFormat)13