Hive 2 JDBC PreparedStatement throws an error cann

2019-09-22 02:49发布

try {
        Class.forName("org.apache.hive.jdbc.HiveDriver");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    }
    String query = "SELECT spd_field_label_id FROM RAL WHERE SUBJECT_USER_ID = ?";

    PreparedStatement stmt = null;
    Connection con = null;
    boolean testCasePassed = false;

    try {

        con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
        stmt = con.prepareStatement(query);

        stmt.setString(1, "USR-44");
        ResultSet resultSet = stmt.executeQuery(query);

        Assert.assertNotNull(resultSet);

        while (resultSet.next()) {
            testCasePassed = true;
            System.out.println("=======Test =========" + resultSet.getString("spd_field_label_id"));
        }
    } finally {

        if (stmt != null) {
            stmt.close();
        }

        if (con != null) {
            con.close();
        }

    }

    return testCasePassed;

RAL is a simple Hive table with String type columns spd_field_label_id and SUBJECT_USER_ID.

Simple PreparedStatement using Hive2 throwing an Error stacktrace below. Any pointers on what could be wrong? Same query works fine when using Statement instead of PreparedStatement and without using ? for parameter binding.

org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: ParseException line 1:62 cannot recognize input near '?' '<EOF>' '<EOF>' in expression specification
at org.apache.hive.jdbc.Utils.verifySuccess(Utils.java:264)
at org.apache.hive.jdbc.Utils.verifySuccessWithInfo(Utils.java:250)
at org.apache.hive.jdbc.HiveStatement.runAsyncOnServer(HiveStatement.java:309)
at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:250)
at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:434)

1条回答
混吃等死
2楼-- · 2019-09-22 03:30
stmt.executeQuery(query);

You're using the wrong method. You've already prepared the statement. It is ready to execute. It should be:

stmt.execute();
查看更多
登录 后发表回答