/** * Query metadata to see if a database with the given name already exists. * * @param dbName * @return true if a database with the given name already exists, false if * does not exist. * @throws HiveException */ public boolean databaseExists(String dbName) throws HiveException { return getDatabase(dbName) != null; }
/** * Query metadata to see if a database with the given name already exists. * * @param dbName * @return true if a database with the given name already exists, false if * does not exist. * @throws HiveException */ public boolean databaseExists(String dbName) throws HiveException { return getDatabase(dbName) != null; }
public Tuple<Database> database() throws HiveException { return new Tuple<>(functionForSpec, () -> db.getDatabase(dbName)); }
public static boolean isBootstrapDumpInProgress(Hive hiveDb, String dbName) throws HiveException { Database database = hiveDb.getDatabase(dbName); if (database == null) { return false; } Map<String, String> params = database.getParameters(); if (params == null) { return false; } for (String key : params.keySet()) { if (key.startsWith(BOOTSTRAP_DUMP_STATE_KEY_PREFIX) && params.get(key).equals(ReplDumpState.ACTIVE.name())) { return true; } } return false; }
/** * Get the Database object for current database * @return a Database object if this database exists, null otherwise. * @throws HiveException */ public Database getDatabaseCurrent() throws HiveException { String currentDb = SessionState.get().getCurrentDatabase(); return getDatabase(currentDb); }
public static void resetDbBootstrapDumpState(Hive hiveDb, String dbName, String uniqueKey) throws HiveException { Database database = hiveDb.getDatabase(dbName); if (database != null) { Map<String, String> params = database.getParameters(); if ((params != null) && params.containsKey(uniqueKey)) { params.remove(uniqueKey); database.setParameters(params); hiveDb.alterDatabase(dbName, database); LOG.info("REPL DUMP:: Reset property for Database: {}, Property: {}", dbName, uniqueKey); } } }
/** * Get the Database object for current database * @return a Database object if this database exists, null otherwise. * @throws HiveException */ public Database getDatabaseCurrent() throws HiveException { String currentDb = SessionState.get().getCurrentDatabase(); return getDatabase(currentDb); }
protected Database getDatabase(String dbName, boolean throwException) throws SemanticException { Database database; try { database = db.getDatabase(dbName); } catch (Exception e) { throw new SemanticException(e.getMessage(), e); } if (database == null && throwException) { throw new SemanticException(ErrorMsg.DATABASE_NOT_EXISTS.getMsg(dbName)); } return database; }
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; }
protected Database getDatabase(String dbName, boolean throwException) throws SemanticException { Database database; try { database = db.getDatabase(dbName); } catch (Exception e) { throw new SemanticException(e.getMessage(), e); } if (database == null && throwException) { throw new SemanticException(ErrorMsg.DATABASE_NOT_EXISTS.getMsg(dbName)); } return database; }
/** * Returns the database path. */ public static Path getDbPath(Hive hive, Warehouse wh, String dbName) throws MetaException, HiveException { return wh.getDatabasePath(hive.getDatabase(dbName)); }
public static String setDbBootstrapDumpState(Hive hiveDb, String dbName) throws HiveException { Database database = hiveDb.getDatabase(dbName); if (database == null) { return null; } Map<String, String> newParams = new HashMap<>(); String uniqueKey = BOOTSTRAP_DUMP_STATE_KEY_PREFIX + UUID.randomUUID().toString(); newParams.put(uniqueKey, ReplDumpState.ACTIVE.name()); Map<String, String> params = database.getParameters(); // if both old params are not null, merge them if (params != null) { params.putAll(newParams); database.setParameters(params); } else { // if one of them is null, replace the old params with the new one database.setParameters(newParams); } hiveDb.alterDatabase(dbName, database); LOG.info("REPL DUMP:: Set property for Database: {}, Property: {}, Value: {}", dbName, uniqueKey, Utils.ReplDumpState.ACTIVE.name()); return uniqueKey; }
private TaskTracker forNewTable() throws Exception { Database parentDb = context.hiveDb.getDatabase(tableDesc.getDatabaseName()); // If table doesn't exist, allow creating a new one only if the database state is older than the update. // This in-turn applicable for partitions creation as well. if ((parentDb != null) && (!event.replicationSpec().allowReplacementInto(parentDb.getParameters()))) { return tracker; } Iterator<AddPartitionDesc> iterator = event.partitionDescriptions(tableDesc).iterator(); while (iterator.hasNext() && tracker.canAddMoreTasks()) { AddPartitionDesc currentPartitionDesc = iterator.next(); /* the currentPartitionDesc cannot be inlined as we need the hasNext() to be evaluated post the current retrieved lastReplicatedPartition */ addPartition(iterator.hasNext(), currentPartitionDesc, null); } return tracker; }
public Database getDatabase(String dbName) throws HiveException { if (!isRunFromMetaStore()) { return Hive.getWithFastCheck(conf).getDatabase(dbName); } else { try { return handler.get_database_core(dbName); } catch (NoSuchObjectException e) { throw new HiveException(e); } catch (MetaException e) { throw new HiveException(e); } } }
@Override public int unlockDatabase(Hive hiveDB, UnlockDatabaseDesc unlockDb) throws HiveException { HiveLockManager lockMgr = getAndCheckLockManager(); String dbName = unlockDb.getDatabaseName(); Database dbObj = hiveDB.getDatabase(dbName); if (dbObj == null) { throw new HiveException("Database " + dbName + " does not exist "); } HiveLockObject obj = new HiveLockObject(dbObj.getName(), null); List<HiveLock> locks = lockMgr.getLocks(obj, false, false); if ((locks == null) || (locks.isEmpty())) { throw new HiveException("Database " + dbName + " is not locked "); } for (HiveLock lock: locks) { lockMgr.unlock(lock); } return 0; }
private ReplLoadOpType getLoadDbType(String dbName) throws InvalidOperationException, HiveException { Database db = context.hiveDb.getDatabase(dbName); if (db == null) { return ReplLoadOpType.LOAD_NEW; } if (isDbAlreadyBootstrapped(db)) { throw new InvalidOperationException("Bootstrap REPL LOAD is not allowed on Database: " + dbName + " as it was already done."); } if (ReplUtils.replCkptStatus(dbName, db.getParameters(), context.dumpDirectory)) { return ReplLoadOpType.LOAD_SKIP; } if (isDbEmpty(dbName)) { return ReplLoadOpType.LOAD_REPLACE; } throw new InvalidOperationException("Bootstrap REPL LOAD is not allowed on Database: " + dbName + " as it is not empty. One or more tables/functions exist."); }
@Override public int lockDatabase(Hive hiveDB, LockDatabaseDesc lockDb) throws HiveException { HiveLockManager lockMgr = getAndCheckLockManager(); HiveLockMode mode = HiveLockMode.valueOf(lockDb.getMode()); String dbName = lockDb.getDatabaseName(); Database dbObj = hiveDB.getDatabase(dbName); if (dbObj == null) { throw new HiveException("Database " + dbName + " does not exist "); } HiveLockObjectData lockData = new HiveLockObjectData(lockDb.getQueryId(), String.valueOf(System.currentTimeMillis()), "EXPLICIT", lockDb.getQueryStr(), conf); HiveLock lck = lockMgr.lock(new HiveLockObject(dbObj.getName(), lockData), mode, true); if (lck == null) { return 1; } return 0; }
/** * Get the database object * @param catName catalog name. If null, the default will be pulled from the conf. This * means the caller does not have to check isCatNameSet() * @param dbName database name. * @return * @throws HiveException */ public Database getDatabase(String catName, String dbName) throws HiveException { catName = catName == null ? MetaStoreUtils.getDefaultCatalog(conf) : catName; if (!isRunFromMetaStore()) { return Hive.getWithFastCheck(conf).getDatabase(catName, dbName); } else { try { return handler.get_database_core(catName, dbName); } catch (NoSuchObjectException e) { throw new HiveException(e); } catch (MetaException e) { throw new HiveException(e); } } }
@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; }
private void handleLineage(LoadTableDesc ltd, Operator output) throws SemanticException { if (ltd != null) { queryState.getLineageState() .mapDirToOp(ltd.getSourcePath(), output); } else if ( queryState.getCommandType().equals(HiveOperation.CREATETABLE_AS_SELECT.getOperationName())) { Path tlocation = null; String tName = Utilities.getDbTableName(tableDesc.getTableName())[1]; try { Warehouse wh = new Warehouse(conf); tlocation = wh.getDefaultTablePath(db.getDatabase(tableDesc.getDatabaseName()), tName, tableDesc.isExternal()); } catch (MetaException|HiveException e) { throw new SemanticException(e); } queryState.getLineageState() .mapDirToOp(tlocation, output); } }