/** * Get the location of the entity. */ public URI getLocation() throws Exception { if (typ == Type.DATABASE) { String location = database.getLocationUri(); return location == null ? null : new URI(location); } if (typ == Type.TABLE) { Path path = t.getDataLocation(); return path == null ? null : path.toUri(); } if (typ == Type.PARTITION) { Path path = p.getDataLocation(); return path == null ? null : path.toUri(); } if (typ == Type.DFS_DIR || typ == Type.LOCAL_DIR) { return d.toUri(); } return null; }
/** * 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()); }
private int showCreateDatabase(Hive db, DataOutputStream outStream, String databaseName) throws Exception { Database database = db.getDatabase(databaseName); StringBuilder createDb_str = new StringBuilder(); createDb_str.append("CREATE DATABASE `").append(database.getName()).append("`\n"); if (database.getDescription() != null) { createDb_str.append("COMMENT\n '"); createDb_str.append( HiveStringUtils.escapeHiveCommand(database.getDescription())).append("'\n"); } createDb_str.append("LOCATION\n '"); createDb_str.append(database.getLocationUri()).append("'\n"); String propertiesToString = propertiesToString(database.getParameters(), null); if (!propertiesToString.isEmpty()) { createDb_str.append("WITH DBPROPERTIES (\n"); createDb_str.append(propertiesToString).append(")\n"); } outStream.write(createDb_str.toString().getBytes("UTF-8")); return 0; }
HCatDatabase(Database db) { this.dbName = db.getName(); this.props = db.getParameters(); this.dbLocation = db.getLocationUri(); this.comment = db.getDescription(); }
boolean shouldModifyDatabaseLocation(Database dbObj) throws IOException, MetaException { String dbName = dbObj.getName(); if (runOptions.shouldModifyManagedTableLocation) { // Check if the database location is in the default location based on the old warehouse root. // If so then change the database location to the default based on the current warehouse root. String dbLocation = dbObj.getLocationUri(); Path oldDefaultDbLocation = oldWh.getDefaultDatabasePath(dbName); if (arePathsEqual(conf, dbLocation, oldDefaultDbLocation.toString())) { if (hasEquivalentEncryption(encryptionShim, oldDefaultDbLocation, curWhRootPath)) { return true; } else { LOG.info("{} and {} are on different encryption zones. Will not change database location for {}", oldDefaultDbLocation, curWhRootPath, dbName); } } } return false; }
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))); } }
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))); } }
@Override public synchronized void createDatabase(Database database) { requireNonNull(database, "database is null"); File directory; if (database.getLocationUri() != null) { directory = new File(URI.create(database.getLocationUri())); } else { // use Hive default naming convention directory = new File(baseDirectory, database.getName() + ".db"); database = database.deepCopy(); database.setLocationUri(directory.toURI().toString()); } checkArgument(!directory.exists(), "Database directory already exists"); checkArgument(isParentDir(directory, baseDirectory), "Database directory must be inside of the metastore base directory"); checkArgument(directory.mkdirs(), "Could not create database directory"); if (databases.putIfAbsent(database.getName(), database) != null) { throw new SchemaAlreadyExistsException(database.getName()); } }
private void validateCreateDb(Database expectedDb, Database actualDb) { assertEquals(expectedDb.getName(), actualDb.getName()); assertEquals(expectedDb.getLocationUri(), actualDb.getLocationUri()); }
protected Path getDbLocation(Database db) throws HiveException { try { initWh(); String location = db.getLocationUri(); if (location == null) { return wh.getDefaultDatabasePath(db.getName()); } else { return wh.getDnsPath(wh.getDatabasePath(db)); } } catch (MetaException ex) { throw hiveException(ex); } }
public static Database fromMetastoreApiDatabase(org.apache.hadoop.hive.metastore.api.Database database) { String ownerName = "PUBLIC"; PrincipalType ownerType = PrincipalType.ROLE; if (database.getOwnerName() != null) { ownerName = database.getOwnerName(); ownerType = fromMetastoreApiPrincipalType(database.getOwnerType()); } Map<String, String> parameters = database.getParameters(); if (parameters == null) { parameters = ImmutableMap.of(); } return Database.builder() .setDatabaseName(database.getName()) .setLocation(Optional.ofNullable(database.getLocationUri())) .setOwnerName(ownerName) .setOwnerType(ownerType) .setComment(Optional.ofNullable(database.getDescription())) .setParameters(parameters) .build(); }
public TableBuilder name(String name) { sd.setLocation(database.getLocationUri() + Path.SEPARATOR + name); table.setTableName(name); serDeInfo.setName(name); return this; }
protected Path getDbLocation(Database db) throws HiveException { try { initWh(); String location = db.getLocationUri(); if (location == null) { return wh.getDefaultDatabasePath(db.getName()); } else { return wh.getDnsPath(wh.getDatabasePath(db)); } } catch (MetaException ex) { throw hiveException(ex); } }
@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 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); }
@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()))); }
@Test public void testDropDatabaseWithFunctionCascade() throws Exception { Database database = testDatabases[0]; Function testFunction = new FunctionBuilder() .setDbName(database.getName()) .setName("test_function") .setClass("org.apache.hadoop.hive.ql.udf.generic.GenericUDFUpper") .create(client, metaStore.getConf()); client.dropDatabase(database.getName(), true, true, true); Assert.assertFalse("The directory should be removed", metaStore.isPathExists(new Path(database.getLocationUri()))); }
@Test public void testDropDatabaseDeleteData() throws Exception { Database database = testDatabases[0]; Path dataFile = new Path(database.getLocationUri() + "/dataFile"); metaStore.createFile(dataFile, "100"); // Do not delete the data client.dropDatabase(database.getName(), false, false); // Check that the data still exist Assert.assertTrue("The data file should still exist", metaStore.isPathExists(dataFile)); // Recreate the database client.createDatabase(database); Assert.assertTrue("The data file should still exist", metaStore.isPathExists(dataFile)); // Delete the data client.dropDatabase(database.getName(), true, false); // Check that the data is removed Assert.assertFalse("The data file should not exist", metaStore.isPathExists(dataFile)); }
@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); }
@Test public void testCreateDatabaseDefaultValues() throws Exception { Database database = new DatabaseBuilder() .setName("dummy") .create(client, metaStore.getConf()); Database createdDatabase = client.getDatabase(database.getName()); Assert.assertNull("Comparing description", createdDatabase.getDescription()); Assert.assertEquals("Comparing location", metaStore.getWarehouseRoot() + "/" + createdDatabase.getName() + ".db", createdDatabase.getLocationUri()); Assert.assertEquals("Comparing parameters", new HashMap<String, String>(), createdDatabase.getParameters()); Assert.assertNull("Comparing privileges", createdDatabase.getPrivileges()); Assert.assertEquals("Comparing owner name", SecurityUtils.getUser(), createdDatabase.getOwnerName()); Assert.assertEquals("Comparing owner type", PrincipalType.USER, createdDatabase.getOwnerType()); }