private void createDefaultDB_core(RawStore ms) throws MetaException, InvalidObjectException { try { ms.getDatabase(DEFAULT_CATALOG_NAME, DEFAULT_DATABASE_NAME); } catch (NoSuchObjectException e) { Database db = new Database(DEFAULT_DATABASE_NAME, DEFAULT_DATABASE_COMMENT, wh.getDefaultDatabasePath(DEFAULT_DATABASE_NAME).toString(), null); db.setOwnerName(PUBLIC); db.setOwnerType(PrincipalType.ROLE); db.setCatalogName(DEFAULT_CATALOG_NAME); long time = System.currentTimeMillis() / 1000; db.setCreateTime((int) time); ms.createDatabase(db); } }
private void makeLocationQualified(Database database) throws HiveException { if (database.isSetLocationUri()) { database.setLocationUri(Utilities.getQualifiedPath(conf, new Path(database.getLocationUri()))); } else { // Location is not set we utilize METASTOREWAREHOUSE together with database name database.setLocationUri( Utilities.getQualifiedPath(conf, new Path(HiveConf.getVar(conf, HiveConf.ConfVars.METASTOREWAREHOUSE), database.getName().toLowerCase() + DATABASE_PATH_SUFFIX))); } }
/** * Create a Database * @param db * @param crtDb * @return Always returns 0 * @throws HiveException */ private int createDatabase(Hive db, CreateDatabaseDesc crtDb) throws HiveException { Database database = new Database(); database.setName(crtDb.getName()); database.setDescription(crtDb.getComment()); database.setLocationUri(crtDb.getLocationUri()); database.setParameters(crtDb.getDatabaseProperties()); database.setOwnerName(SessionState.getUserFromAuthenticator()); database.setOwnerType(PrincipalType.USER); try { makeLocationQualified(database); db.createDatabase(database, crtDb.getIfNotExists()); } catch (AlreadyExistsException ex) { //it would be better if AlreadyExistsException had an errorCode field.... throw new HiveException(ex, ErrorMsg.DATABASE_ALREADY_EXISTS, crtDb.getName()); } return 0; }
HCatDatabase(Database db) { this.dbName = db.getName(); this.props = db.getParameters(); this.dbLocation = db.getLocationUri(); this.comment = db.getDescription(); }
Database toHiveDb() { Database hiveDB = new Database(); hiveDB.setDescription(this.comment); hiveDB.setLocationUri(this.locationUri); hiveDB.setName(this.dbName); hiveDB.setParameters(this.dbProperties); return hiveDB; }
public TableBuilder inDb(Database db) { this.dbName = db.getName(); this.catName = db.getCatalogName(); return this; }
@Test public void testDataDeletion() throws HiveException, IOException, TException { Database db = new Database(); db.setName(dbName); hive.createDatabase(db); Table table = new Table(dbName, tableName); table.setDbName(dbName); table.setInputFormatClass(TextInputFormat.class); table.setOutputFormatClass(HiveIgnoreKeyTextOutputFormat.class); table.setPartCols(partCols); hive.createTable(table); table = hive.getTable(dbName, tableName); Path fakeTable = table.getPath().getParent().suffix( Path.SEPARATOR + "faketable"); fs = fakeTable.getFileSystem(hive.getConf()); fs.mkdirs(fakeTable); fs.deleteOnExit(fakeTable); Path fakePart = new Path(table.getDataLocation().toString(), "fakepartition=fakevalue"); fs.mkdirs(fakePart); fs.deleteOnExit(fakePart); hive.dropTable(dbName, tableName, true, true); assertFalse(fs.exists(fakePart)); hive.dropDatabase(dbName); assertFalse(fs.exists(fakeTable)); }
private void initTable() throws Exception { client.dropTable(dbName, tblName); } catch (Exception e) { client.dropDatabase(dbName); } catch (Exception e) { client.createDatabase(new Database(dbName, "", null, null)); assertNotNull((client.getDatabase(dbName).getLocationUri())); Table tbl = new Table(); tbl.setDbName(dbName); tbl.setTableName(tblName); StorageDescriptor sd = new StorageDescriptor(); sd.setCols(Lists.newArrayList(new FieldSchema("data_column", serdeConstants.STRING_TYPE_NAME, ""))); tbl.setSd(sd); sd.setInputFormat(RCFileInputFormat.class.getName()); sd.setOutputFormat(RCFileOutputFormat.class.getName()); sd.setParameters(new HashMap<String, String>()); client.createTable(tbl); Path tblPath = new Path(client.getTable(dbName, tblName).getSd().getLocation()); assertTrue(tblPath.getFileSystem(hiveConf).mkdirs(new Path(tblPath, "colname=p1")));
if (!uniqueTableNames.isEmpty()) { throw new InvalidOperationException( "Database " + db.getName() + " is not empty. One or more tables exist."); "Database " + db.getName() + " is not empty. One or more functions exist."); Path path = new Path(db.getLocationUri()).getParent(); if (!wh.isWritable(path)) { throw new MetaException("Database not dropped since " + if (materializedView.getSd().getLocation() != null) { Path materializedViewPath = wh.getDnsPath(new Path(materializedView.getSd().getLocation())); if (!wh.isWritable(materializedViewPath.getParent())) { throw new MetaException("Database metadata not deleted since table: " + materializedView.getTableName() + " has a parent location " + materializedViewPath.getParent() + " which is not writable by " + SecurityUtils.getUser()); if (table.getSd().getLocation() != null && tableDataShouldBeDeleted) { tablePath = wh.getDnsPath(new Path(table.getSd().getLocation())); if (!wh.isWritable(tablePath.getParent())) { wh.deleteDir(new Path(db.getLocationUri()), true, db); } catch (Exception e) { LOG.error("Failed to delete database directory: " + db.getLocationUri() + " " + e.getMessage());
.setLocation(dbLocation) .create(client, conf); Database db = client.getDatabase(dbName); .create(client, conf); tbl = client.getTable(dbName, tblName_1); Path path = new Path(tbl.getSd().getLocation()); System.err.println("Table's location " + path + ", Database's location " + db.getLocationUri()); assertEquals("Table location is not a subset of the database location", path.getParent().toString(), db.getLocationUri());
.create(client, conf); Database db = client.getDatabase(TEST_DB1_NAME); TEST_DB1_NAME, db.getName()); assertEquals("location of the returned db is different from that of inserted db", warehouse.getDnsPath(new Path(dbLocation)).toString(), db.getLocationUri()); client.dropDatabase(TEST_DB1_NAME); silentDropDatabase(TEST_DB1_NAME); client.getDatabase(TEST_DB1_NAME); } catch (NoSuchObjectException e) { objectNotExist = true; FileSystem fs = FileSystem.get(new Path(dbLocation).toUri(), conf); fs.createNewFile(new Path(dbLocation)); fs.deleteOnExit(new Path(dbLocation));
@Test public void testDropDatabaseWithTableCascade() throws Exception { Database database = testDatabases[0]; Table testTable = new TableBuilder() .setDbName(database.getName()) .setTableName("test_table") .addCol("test_col", "int") .create(client, metaStore.getConf()); client.dropDatabase(database.getName(), true, true, true); Assert.assertFalse("The directory should be removed", metaStore.isPathExists(new Path(database.getLocationUri()))); }
void processTable(Database dbObj, String tableName, boolean modifyDefaultManagedLocation) throws HiveException, IOException, TException { String dbName = dbObj.getName(); LOG.debug("Processing table {}", getQualifiedName(dbName, tableName)); Table tableObj = hms.getTable(dbName, tableName); TableType tableType = TableType.valueOf(tableObj.getTableType()); getHiveUpdater(), hms, conf); if (!failedValidationChecks && (TableType.valueOf(tableObj.getTableType()) == TableType.MANAGED_TABLE)) { Path tablePath = new Path(tableObj.getSd().getLocation()); if (modifyDefaultManagedLocation && shouldModifyTableLocation(dbObj, tableObj)) { Path newTablePath = wh.getDnsPath( new Path(wh.getDefaultDatabasePath(dbName), MetaStoreUtils.encodeTableName(tableName.toLowerCase()))); moveTableData(dbObj, tableObj, newTablePath); FileSystem fs = tablePath.getFileSystem(conf); if (isHdfs(fs)) {
@Test public void testDBLocationChange() throws IOException, TException { final String dbName = "alterDbLocation"; String defaultUri = MetastoreConf.getVar(conf, ConfVars.WAREHOUSE) + "/default_location.db"; String newUri = MetastoreConf.getVar(conf, ConfVars.WAREHOUSE) + "/new_location.db"; new DatabaseBuilder() .setName(dbName) .setLocation(defaultUri) .create(client, conf); Database db = client.getDatabase(dbName); assertEquals("Incorrect default location of the database", warehouse.getDnsPath(new Path(defaultUri)).toString(), db.getLocationUri()); db.setLocationUri(newUri); client.alterDatabase(dbName, db); db = client.getDatabase(dbName); assertEquals("Incorrect new location of the database", warehouse.getDnsPath(new Path(newUri)).toString(), db.getLocationUri()); client.dropDatabase(dbName); silentDropDatabase(dbName); }
/** * Get the path specified by the database. In the case of the default database the root of the * warehouse is returned. * @param db database to get the path of * @return path to the database directory * @throws MetaException when the file path cannot be properly determined from the configured * file system. */ public Path getDatabasePath(Database db) throws MetaException { if (db.getCatalogName().equalsIgnoreCase(DEFAULT_CATALOG_NAME) && db.getName().equalsIgnoreCase(DEFAULT_DATABASE_NAME)) { return getWhRoot(); } return new Path(db.getLocationUri()); }
@Override public HiveResourceACLs getResourceACLs(HivePrivilegeObject hiveObject) { HiveResourceACLs acls = null; try { switch (hiveObject.getType()) { case DATABASE: Database db = Hive.get().getDatabase(hiveObject.getDbname()); acls = getResourceACLs(new Path(db.getLocationUri())); break; case TABLE_OR_VIEW: case COLUMN: Table table = Hive.get().getTable(hiveObject.getDbname(), hiveObject.getObjectName()); acls = getResourceACLs(new Path(table.getTTable().getSd().getLocation())); break; default: // Shall never happen throw new RuntimeException("Unknown request type:" + hiveObject.getType()); } } catch (Exception e) { } return acls; }
@Test public void testDefaultDatabaseData() throws Exception { Database database = client.getDatabase(DEFAULT_DATABASE); Assert.assertEquals("Default database name", "default", database.getName()); Assert.assertEquals("Default database description", "Default Hive database", database.getDescription()); Assert.assertEquals("Default database location", metaStore.getWarehouseRoot(), new Path(database.getLocationUri())); Assert.assertEquals("Default database parameters", new HashMap<String, String>(), database.getParameters()); Assert.assertEquals("Default database owner", "public", database.getOwnerName()); Assert.assertEquals("Default database owner type", PrincipalType.ROLE, database.getOwnerType()); Assert.assertNull("Default database privileges", database.getPrivileges()); Assert.assertTrue("database create time should be set", database.isSetCreateTime()); Assert.assertTrue("Database create time should be non-zero", database.getCreateTime() > 0); }
public Path getDefaultTablePath(Database db, String tableName, boolean isExternal) throws MetaException { Path dbPath = null; if (isExternal && hasExternalWarehouseRoot()) { dbPath = getDefaultExternalDatabasePath(db.getName()); } else { dbPath = getDatabasePath(db); } return getDnsPath( new Path(dbPath, MetaStoreUtils.encodeTableName(tableName.toLowerCase()))); }
private String doComputeName() { switch (typ) { case DATABASE: return "database:" + database.getName(); case TABLE: return t.getDbName() + "@" + t.getTableName(); case PARTITION: return t.getDbName() + "@" + t.getTableName() + "@" + p.getName(); case DUMMYPARTITION: return p.getName(); case FUNCTION: if (database != null) { return database.getName() + "." + stringObject; } return stringObject; case SERVICE_NAME: return stringObject; default: return d.toString(); } }
String[] partNames, String dbLocation) throws Exception { IMetaStoreClient client = new HiveMetaStoreClient(conf); try { Database db = new Database(); db.setName(databaseName); db.setLocationUri(dbLocation); client.createDatabase(db); Table tbl = new Table(); tbl.setDbName(databaseName); tbl.setTableName(tableName); tbl.setTableType(TableType.MANAGED_TABLE.toString()); StorageDescriptor sd = new StorageDescriptor(); sd.setCols(getTableColumns(colNames, colTypes)); sd.setNumBuckets(1); sd.setLocation(dbLocation + Path.SEPARATOR + tableName); if (partNames != null && partNames.length != 0) {