配置单元自定义项包含查询语句

ecbunoof  于 2021-06-29  发布在  Hive
关注(0)|答案(0)|浏览(241)

我在写一些自定义项时遇到了一个问题,我在网站上搜索了相关的帖子,但恐怕我还没有任何有用的想法。问题是:我将在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>

任何想法都将受到高度赞赏!提前谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题