使用 Hive 的 JDBC 元数据 API 得到不明确的结果

Getting ambiguous result using JDBC Metadata API for Hive

我正在尝试以类似于 RDBMS 的方式使用 DatabaseMetaData 获取配置单元的 Table 名称。

示例代码:

try (Connection con = getJdbcConnection(connectionUri, driverName, username, password);) {
        DatabaseMetaData metadata = con.getMetaData();
        ResultSet rs = metadata.getTables(null, null, tableName, null);
        while (rs.next()) {
            System.out.println(rs.getString(3));
       }

  } catch (SQLException e) {
  }

private static void registerDriver(String driverName) {
        try {
            Class.forName(driverName);
         } catch (ClassNotFoundException e) {
            LOG.error("No class found for " + driverName + ". Details: " + e);
        }
    }

private static Connection getJdbcConnection(String connectionUri, String driverName, String username,
        String password) throws SQLException{
    registerDriver(driverName);
    return DriverManager.getConnection(connectionUri, username,password);
}

特定数据库中没有 table。使用不同的 table 名称,我得到不同的输出。

例如:

我输入table名字emp,有3条名字为emp

的记录

我输入table名字employee,有5条名字为employee

的记录

我输入 table 名称 emp12,它没有返回任何记录 (这是预期的)


我需要在 getTables 方法中传递架构名称

签名:

ResultSet getTables(String catalog,
                  String schemaPattern,
                  String tableNamePattern,
                  String[] types)
                    throws SQLException

我通过了以下参数:

  • 目录 = 空;
  • schemaPattern = Hive 架构名称
  • tableNamePattern = Hive Table 名称
  • 类型 = new String[] { "TABLE" }

示例代码:

    try (Connection con = getJdbcConnection(connectionUri, driverName, username, password);) {
        DatabaseMetaData metadata = con.getMetaData();
        ResultSet rs = metadata.getTables(null, schemaName, tableName, new String[] { "TABLE" });

        while (rs.next()) {
            String tName = rs.getString("TABLE_NAME");
            if (tName != null && tName.equals(tableName)) {
                LOG.info("Table [" + tableName + "] is present in the Database.");
                return true;
            }
        }
        rs.close();

        LOG.info("Table [" + tableName + "] is not present in the Database.");
        return false;

    } catch (SQLException e) {
        LOG.error("Not able to get Table Metadata . Caused By: " + e);
    }