org.apache.hadoop.hive.ql.metadata.Hive.getDatabase()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(296)

本文整理了Java中org.apache.hadoop.hive.ql.metadata.Hive.getDatabase()方法的一些代码示例,展示了Hive.getDatabase()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Hive.getDatabase()方法的具体详情如下:
包路径:org.apache.hadoop.hive.ql.metadata.Hive
类名称:Hive
方法名:getDatabase

Hive.getDatabase介绍

[英]Get the database by name.
[中]按名称获取数据库。

代码示例

代码示例来源:origin: apache/hive

/**
 * 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;
}

代码示例来源:origin: apache/drill

/**
 * 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;
}

代码示例来源:origin: apache/hive

public Tuple<Database> database() throws HiveException {
 return new Tuple<>(functionForSpec, () -> db.getDatabase(dbName));
}

代码示例来源:origin: apache/hive

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;
}

代码示例来源:origin: apache/hive

/**
 * 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);
}

代码示例来源:origin: apache/hive

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);
  }
 }
}

代码示例来源:origin: apache/drill

/**
 * 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);
}

代码示例来源:origin: apache/hive

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;
}

代码示例来源:origin: apache/hive

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;
}

代码示例来源:origin: apache/drill

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;
}

代码示例来源:origin: apache/hive

/**
 * Returns the database path.
 */
public static Path getDbPath(Hive hive, Warehouse wh, String dbName) throws MetaException, HiveException {
 return wh.getDatabasePath(hive.getDatabase(dbName));
}

代码示例来源:origin: apache/hive

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;
}

代码示例来源:origin: apache/hive

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;
}

代码示例来源:origin: apache/drill

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);
  }
 }
}

代码示例来源:origin: apache/hive

@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;
}

代码示例来源:origin: apache/hive

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.");
}

代码示例来源:origin: apache/hive

@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;
}

代码示例来源:origin: apache/hive

/**
 * 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);
  }
 }
}

代码示例来源:origin: apache/hive

@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;
}

代码示例来源:origin: apache/hive

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);
 }
}

相关文章

Hive类方法