0

我在写一些UDF时遇到了一个问题,我在网站上搜索了相关的帖子,但恐怕我还没有任何有用的想法。问题是:我要在UDF中执行一条SQL语句,然后打印查询结果。这是我的代码:

public final class AnalyzeConstraints extends UDF {

    private Connection connToHive = null;

    public Connection getHiveConn() throws SQLException {
        if (connToHive == null) {
            try {
                Class.forName("org.apache.hive.jdbc.HiveDriver");
            } catch (ClassNotFoundException err) {
                err.printStackTrace();
                System.exit(1);
            }
            // hive cluster IP address
            connToHive = DriverManager.getConnection(
                    "jdbc:hive://XXXXX:10004/default", "user", "passwd");
            System.out.println("loggin");
        }
        return connToHive;
    }

    public void closeHiveConn() throws SQLException {
        if (connToHive != null) {
            connToHive.close();
        }
    }

//# end region

//# region HiveUtility
    // query data
    public ResultSet queryData(String sql) throws SQLException {
        Connection conn = getHiveConn();
        Statement stmt = conn.createStatement();
        ResultSet res = stmt.executeQuery(sql);
        return res;
    }
//# end region

//# region UDF implement
    public String evaluate(String table) throws SQLException {
        StringBuffer result = new StringBuffer();

        String schema = null;
        String table_name = null;
        if (table.length() > 0 && table.indexOf(".") > 0 && table.split("\\.").length == 2) {
            schema = table.split("\\.")[0];
            table_name = table.split("\\.")[1];

            // For debug
            System.out.println(schema + ":" + table_name);
        }
        else
            result.append("ERROR: \'" + table + "\' is not a valid table in the current search_path\n");

        //# region analyze PK
            StringBuffer sqlPK = new StringBuffer();
            sqlPK.append(String.format("select constraint_name, column_name from catalog.constraint_columns where table_schema = \'%s\' and Upper(table_name) = \'%s\' and constraint_type = \'p\';\n", schema, table_name.toUpperCase()));

            // For debug
            System.out.println(sqlPK.toString());

            ResultSet resPK = queryData(sqlPK.toString());
            // Print resultset here
 } 

这是错误消息:

FAILED: SemanticException [Error 10014]: Line 1:8 Wrong arguments ''catalog.systables'': org.apache.hadoop.hive.ql.metadata.HiveException: Unable to execute method public java.lang.String AnalyzeConstraints.evaluate(java.lang.String) throws java.sql.SQLException  on object AnalyzeConstraints@4f86c135 of class AnalyzeConstraints with arguments {catalog.systables:java.lang.String} of size 1
hive>

任何想法将不胜感激!提前致谢!

4

0 回答 0