diff --git ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java index cceea019c2..4ddf20265a 100644 --- ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java +++ ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java @@ -1402,12 +1402,56 @@ public Table getTable(final String dbName, final String tableName, /** * Get all tables for the specified database. * @param dbName - * @return List of table names + * @return List of all tables * @throws HiveException */ public List getAllTableObjects(String dbName) throws HiveException { + return getTableObjects(dbName, ".*", null); + } + + /** + * Get all virtual view names for the specified database. + * @param dbName + * @return List of virtual view table names + * @throws HiveException + */ + public List getAllVirtualViews(String dbName) throws HiveException { + return getTablesByType(dbName, ".*", TableType.VIRTUAL_VIEW); + } + + /** + * Get all virtual views for the specified database. + * @param dbName + * @return List of virtual view table objects + * @throws HiveException + */ + public List
getAllVirtualViewObjects(String dbName) throws HiveException { + return getTableObjects(dbName, ".*", TableType.VIRTUAL_VIEW); + } + + /** + * Get all materialized view names for the specified database. + * @param dbName + * @return List of materialized view table names + * @throws HiveException + */ + public List getAllMaterializedViews(String dbName) throws HiveException { + return getTablesByType(dbName, ".*", TableType.MATERIALIZED_VIEW); + } + + /** + * Get all materialized views for the specified database. + * @param dbName + * @return List of materialized view table objects + * @throws HiveException + */ + public List
getAllMaterializedViewObjects(String dbName) throws HiveException { + return getTableObjects(dbName, ".*", TableType.MATERIALIZED_VIEW); + } + + private List
getTableObjects(String dbName, String pattern, TableType tableType) throws HiveException { try { - return Lists.transform(getMSC().getTableObjectsByName(dbName, getMSC().getAllTables(dbName)), + return Lists.transform(getMSC().getTableObjectsByName(dbName, getTablesByType(dbName, pattern, tableType)), new com.google.common.base.Function() { @Override public Table apply(org.apache.hadoop.hive.metastore.api.Table table) { @@ -1510,7 +1554,7 @@ public Table apply(org.apache.hadoop.hive.metastore.api.Table table) { List result = new ArrayList<>(); for (String dbName : getMSC().getAllDatabases()) { // From metastore (for security) - List tables = getMSC().getAllTables(dbName); + List tables = getAllMaterializedViews(dbName); // Cached views (includes all) Collection cachedViews = HiveMaterializedViewsRegistry.get().getRewritingMaterializedViews(dbName); diff --git ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveMaterializedViewsRegistry.java ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveMaterializedViewsRegistry.java index ba46ff76e7..51b6ef58fc 100644 --- ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveMaterializedViewsRegistry.java +++ ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveMaterializedViewsRegistry.java @@ -121,32 +121,28 @@ public static HiveMaterializedViewsRegistry get() { * it does. */ public void init(final Hive db) { - try { - List
tables = new ArrayList
(); - for (String dbName : db.getAllDatabases()) { - // TODO: We should enhance metastore API such that it returns only - // materialized views instead of all tables - tables.addAll(db.getAllTableObjects(dbName)); - } - pool.submit(new Loader(tables)); - } catch (HiveException e) { - LOG.error("Problem connecting to the metastore when initializing the view registry"); - } + pool.submit(new Loader(db)); } private class Loader implements Runnable { - private final List
tables; + private final Hive db; - private Loader(List
tables) { - this.tables = tables; + private Loader(Hive db) { + this.db = db; } @Override public void run() { - for (Table table : tables) { - if (table.isMaterializedView()) { - addMaterializedView(table); + try { + List
materializedViews = new ArrayList
(); + for (String dbName : db.getAllDatabases()) { + materializedViews.addAll(db.getAllMaterializedViewObjects(dbName)); + } + for (Table mv : materializedViews) { + addMaterializedView(mv); } + } catch (HiveException e) { + LOG.error("Problem connecting to the metastore when initializing the view registry"); } } } diff --git standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java index d9155c4c35..db60ff29a8 100644 --- standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java +++ standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java @@ -383,6 +383,39 @@ public Database getDatabase(String dbName) throws MetaException{ } /** + * Get table names by using direct SQL queries. + * + * @param dbName Metastore database namme + * @param tableType Table type, or null if we want to get all tables + * @return list of table names + */ + public List getTables(String db_name, TableType tableType) throws MetaException { + List ret = new ArrayList(); + String queryText = "SELECT " + TBLS + ".\"TBL_NAME\"," + + " FROM " + TBLS + " " + + " INNER JOIN " + DBS + " ON " + TBLS + ".\"DB_ID\" = " + DBS + ".\"DB_ID\" " + + " WHERE " + DBS + ".\"NAME\" = ? " + + (tableType == null ? "" : "AND " + TBLS + ".\"TBL_TYPE\" = ? ") ; + + List pms = new ArrayList(); + pms.add(db_name); + if (tableType != null) { + pms.add(tableType.toString()); + } + + Query queryParams = pm.newQuery("javax.jdo.query.SQL", queryText); + List sqlResult = ensureList(executeWithArray( + queryParams, pms.toArray(), queryText)); + + if (!sqlResult.isEmpty()) { + for (Object[] line : sqlResult) { + ret.add(extractSqlString(line[0])); + } + } + return ret; + } + + /** * Gets partitions by using direct SQL queries. * Note that batching is not needed for this method - list of names implies the batch size; * @param dbName Metastore db name. @@ -2250,4 +2283,5 @@ public void closeAllQueries() { } return ret; } + } diff --git standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java index 575ba05a4d..de7dc2d9d8 100644 --- standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java +++ standalone-metastore/src/main/java/org/apache/hadoop/hive/metastore/ObjectStore.java @@ -188,7 +188,6 @@ import org.apache.hadoop.hive.metastore.utils.JavaUtils; import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils; import org.apache.hadoop.hive.metastore.utils.ObjectPair; -import org.apache.hadoop.util.StringUtils; import org.apache.thrift.TException; import org.datanucleus.AbstractNucleusContext; import org.datanucleus.ClassLoaderResolver; @@ -1296,6 +1295,34 @@ public Table getTable(String dbName, String tableName) throws MetaException { @Override public List getTables(String dbName, String pattern, TableType tableType) throws MetaException { + try { + // We only support pattern matching via jdo since pattern matching in Java + // might be different than the one used by the metastore backends + return getTablesInternal(dbName, pattern, tableType, (pattern == null || pattern.equals(".*")), true); + } catch (NoSuchObjectException e) { + throw new MetaException(ExceptionUtils.getStackTrace(e)); + } + } + + protected List getTablesInternal(String dbName, String pattern, TableType tableType, + boolean allowSql, boolean allowJdo) throws MetaException, NoSuchObjectException { + final String db_name = normalizeIdentifier(dbName); + return new GetListHelper(dbName, null, allowSql, allowJdo) { + @Override + protected List getSqlResult(GetHelper> ctx) + throws MetaException { + return directSql.getTables(db_name, tableType); + } + + @Override + protected List getJdoResult(GetHelper> ctx) + throws MetaException, NoSuchObjectException { + return getTablesInternalViaJdo(db_name, pattern, tableType); + } + }.run(false); + } + + private List getTablesInternalViaJdo(String dbName, String pattern, TableType tableType) throws MetaException { boolean commited = false; Query query = null; List tbls = null;