@Override public void dropDatabase(String dbName, boolean ignoreIfNotExists) throws DatabaseNotExistException { if (databases.containsKey(dbName)) { databases.remove(dbName); } else if (!ignoreIfNotExists) { throw new DatabaseNotExistException(catalogName, dbName); } }
@Override public void alterDatabase(String dbName, CatalogDatabase newDatabase, boolean ignoreIfNotExists) throws DatabaseNotExistException { if (dbExists(dbName)) { databases.put(dbName, newDatabase); } else if (!ignoreIfNotExists) { throw new DatabaseNotExistException(catalogName, dbName); } }
@Override public void renameDatabase(String dbName, String newDbName, boolean ignoreIfNotExists) throws DatabaseNotExistException { if (dbExists(dbName)) { databases.put(newDbName, databases.remove(dbName)); } else if (!ignoreIfNotExists) { throw new DatabaseNotExistException(catalogName, dbName); } }
@Override public CatalogDatabase getDatabase(String dbName) throws DatabaseNotExistException { if (!dbExists(dbName)) { throw new DatabaseNotExistException(catalogName, dbName); } else { return databases.get(dbName); } }
@Override public void dropDatabase(String dbName, boolean ignoreIfNotExists) throws DatabaseNotExistException { try { client.dropDatabase(dbName, true, ignoreIfNotExists); } catch (NoSuchObjectException e) { if (!ignoreIfNotExists) { throw new DatabaseNotExistException(catalogName, dbName); } } catch (TException e) { throw new FlinkHiveException(String.format("Failed to drop database %s", dbName), e); } }
@Override public List<ObjectPath> listTables(String dbName) throws DatabaseNotExistException { try { return client.getAllTables(dbName).stream() .map(t -> new ObjectPath(dbName, t)) .collect(Collectors.toList()); } catch (UnknownDBException e) { throw new DatabaseNotExistException(catalogName, dbName); } catch (TException e) { throw new FlinkHiveException( String.format("Failed to list tables in database %s", dbName), e); } }
@Override public List<ObjectPath> listTables(String dbName) throws DatabaseNotExistException { checkArgument(!StringUtils.isNullOrWhitespaceOnly(dbName), "dbName cannot be null or empty"); if (!dbExists(dbName)) { throw new DatabaseNotExistException(catalogName, dbName); } return tables.keySet().stream() .filter(k -> k.getDbName().equals(dbName)) .collect(Collectors.toList()); }
@Override public CatalogDatabase getDatabase(String dbName) throws DatabaseNotExistException { Database hiveDb; try { hiveDb = client.getDatabase(dbName); } catch (NoSuchObjectException e) { throw new DatabaseNotExistException(catalogName, dbName); } catch (TException e) { throw new FlinkHiveException( String.format("Failed to get database %s from HiveCatalog %s", dbName, catalogName), e); } return HiveMetadataUtil.createCatalogDatabase(hiveDb); }
@Override public void alterDatabase(String dbName, CatalogDatabase newDatabase, boolean ignoreIfNotExists) throws DatabaseNotExistException { try { if (dbExists(dbName)) { client.alterDatabase(dbName, HiveMetadataUtil.createHiveDatabase(dbName, newDatabase)); } else if (!ignoreIfNotExists) { throw new DatabaseNotExistException(catalogName, dbName); } } catch (TException e) { throw new FlinkHiveException(String.format("Failed to alter database %s", dbName), e); } }
@Override public void createTable(ObjectPath tableName, CatalogTable table, boolean ignoreIfExists) throws TableAlreadyExistException, DatabaseNotExistException { if (!dbExists(tableName.getDbName())) { throw new DatabaseNotExistException(catalogName, tableName.getDbName()); } if (tableExists(tableName)) { if (!ignoreIfExists) { throw new TableAlreadyExistException(catalogName, tableName.getFullName()); } } else { tables.put(tableName, table); if (table.isPartitioned()) { partitions.put(tableName, new LinkedHashMap<>()); } } }
@Override public void createTable(ObjectPath path, CatalogTable table, boolean ignoreIfExists) throws TableAlreadyExistException, DatabaseNotExistException { try { if (tableExists(path)) { if (!ignoreIfExists) { throw new TableAlreadyExistException(catalogName, path.getFullName()); } } else { // Testing shows that createTable() API in Hive 2.3.4 doesn't throw UnknownDBException as it claims // Thus we have to manually check if the db exists or not if (!dbExists(path.getDbName())) { throw new DatabaseNotExistException(catalogName, path.getDbName()); } client.createTable(HiveMetadataUtil.createHiveTable(path, table)); } } catch (TException e) { throw new FlinkHiveException( String.format("Failed to create table %s", path.getFullName()), e); } }