使用Impala的PreparedStatement:无法识别'?'附近的输入?

时间:2018-04-27 14:17:12

标签: jdbc hive prepared-statement impala

我在PreparedStatement和Impala方面遇到了一些麻烦。这是我的示例代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class TestPreparedStatement {

    public static void main(String[] args) {

        String connectionString = "jdbc:impala://[MY_IP]:[MY_PORT];UseNativeQuery=1;AuthMech=3";
        String username = "username";
        String password = "password";

        try {
            Class.forName("com.cloudera.impala.jdbc4.Driver");

            Connection _conn = DriverManager.getConnection(connectionString, username, password);

            String _sql = "select number_impala, string_impala from my_table where number_impala = ?";

            PreparedStatement pstm = _conn.prepareStatement(_sql);

            pstm.setObject(1, 1);

            pstm.executeQuery();

            pstm.close();

            _conn.close();

        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(TestPreparedStatement.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

出于某种原因,我得到了ParseException,可能是关于?要在PreparedStatement中使用的查询中的符号。 Stacktrace在这里:

log4j:WARN No appenders could be found for logger (org.apache.thrift.transport.TSaslTransport).
log4j:WARN Please initialize the log4j system properly.
Abr 27, 2018 2:44:24 PM TestPreparedStatement main
SEVERE: null
java.sql.SQLException: [Simba][ImpalaJDBCDriver](500051) ERROR processing query/statement. Error Code: 40000, SQL state: TStatus(statusCode:ERROR_STATUS, infoMessages:[*org.apache.hive.service.cli.HiveSQLException:Error while compiling statement: FAILED: ParseException line 1:72 cannot recognize input near '?' '<EOF>' '<EOF>' in expression specification:28:27, org.apache.hive.service.cli.operation.Operation:toSQLException:Operation.java:400, org.apache.hive.service.cli.operation.SQLOperation:prepare:SQLOperation.java:187, org.apache.hive.service.cli.operation.SQLOperation:runInternal:SQLOperation.java:271, org.apache.hive.service.cli.operation.Operation:run:Operation.java:337, org.apache.hive.service.cli.session.HiveSessionImpl:executeStatementInternal:HiveSessionImpl.java:439, org.apache.hive.service.cli.session.HiveSessionImpl:executeStatement:HiveSessionImpl.java:405, sun.reflect.GeneratedMethodAccessor51:invoke::-1, sun.reflect.DelegatingMethodAccessorImpl:invoke:DelegatingMethodAccessorImpl.java:43, java.lang.reflect.Method:invoke:Method.java:497, org.apache.hive.service.cli.session.HiveSessionProxy:invoke:HiveSessionProxy.java:78, org.apache.hive.service.cli.session.HiveSessionProxy:access$000:HiveSessionProxy.java:36, org.apache.hive.service.cli.session.HiveSessionProxy$1:run:HiveSessionProxy.java:63, java.security.AccessController:doPrivileged:AccessController.java:-2, javax.security.auth.Subject:doAs:Subject.java:422, org.apache.hadoop.security.UserGroupInformation:doAs:UserGroupInformation.java:1917, org.apache.hive.service.cli.session.HiveSessionProxy:invoke:HiveSessionProxy.java:59, com.sun.proxy.$Proxy22:executeStatement::-1, org.apache.hive.service.cli.CLIService:executeStatement:CLIService.java:257, org.apache.hive.service.cli.thrift.ThriftCLIService:ExecuteStatement:ThriftCLIService.java:501, org.apache.hive.service.cli.thrift.TCLIService$Processor$ExecuteStatement:getResult:TCLIService.java:1313, org.apache.hive.service.cli.thrift.TCLIService$Processor$ExecuteStatement:getResult:TCLIService.java:1298, org.apache.thrift.ProcessFunction:process:ProcessFunction.java:39, org.apache.thrift.TBaseProcessor:process:TBaseProcessor.java:39, org.apache.hive.service.auth.TSetIpAddressProcessor:process:TSetIpAddressProcessor.java:56, org.apache.thrift.server.TThreadPoolServer$WorkerProcess:run:TThreadPoolServer.java:286, java.util.concurrent.ThreadPoolExecutor:runWorker:ThreadPoolExecutor.java:1142, java.util.concurrent.ThreadPoolExecutor$Worker:run:ThreadPoolExecutor.java:617, java.lang.Thread:run:Thread.java:745, *org.apache.hadoop.hive.ql.parse.ParseException:line 1:72 cannot recognize input near '?' '<EOF>' '<EOF>' in expression specification:32:5, org.apache.hadoop.hive.ql.parse.ParseDriver:parse:ParseDriver.java:204, org.apache.hadoop.hive.ql.parse.ParseDriver:parse:ParseDriver.java:166, org.apache.hadoop.hive.ql.Driver:compile:Driver.java:522, org.apache.hadoop.hive.ql.Driver:compileInternal:Driver.java:1356, org.apache.hadoop.hive.ql.Driver:compileAndRespond:Driver.java:1343, org.apache.hive.service.cli.operation.SQLOperation:prepare:SQLOperation.java:185], sqlState:42000, errorCode:40000, errorMessage:Error while compiling statement: FAILED: ParseException line 1:72 cannot recognize input near '?' '<EOF>' '<EOF>' in expression specification), Query: select number_impala, string_impala from my_table where number_impala = ?.
    at com.cloudera.impala.hivecommon.api.HS2Client.executeStatementInternal(HS2Client.java:832)
    at com.cloudera.impala.hivecommon.api.HS2Client.executeStatement(HS2Client.java:287)
    at com.cloudera.impala.hivecommon.dataengine.HiveJDBCNativeQueryExecutor.executeQuery(HiveJDBCNativeQueryExecutor.java:504)
    at com.cloudera.impala.hivecommon.dataengine.HiveJDBCNativeQueryExecutor.<init>(HiveJDBCNativeQueryExecutor.java:244)
    at com.cloudera.impala.hivecommon.dataengine.HiveJDBCDataEngine.prepare(HiveJDBCDataEngine.java:234)
    at com.cloudera.impala.jdbc.common.SPreparedStatement.<init>(SPreparedStatement.java:285)
    at com.cloudera.impala.jdbc.jdbc4.S4PreparedStatement.<init>(S4PreparedStatement.java:106)
    at com.cloudera.impala.jdbc.jdbc4.JDBC4ObjectFactory.createPreparedStatement(JDBC4ObjectFactory.java:167)
    at com.cloudera.impala.hivecommon.core.jdbc4.HiveJDBC4ObjectFactory.createPreparedStatement(HiveJDBC4ObjectFactory.java:118)
    at com.cloudera.impala.jdbc.common.SConnection.prepareStatement(SConnection.java:1143)
Caused by: com.cloudera.impala.support.exceptions.GeneralException: [Simba][ImpalaJDBCDriver](500051) ERROR processing query/statement. Error Code: 40000, SQL state: TStatus(statusCode:ERROR_STATUS, infoMessages:[*org.apache.hive.service.cli.HiveSQLException:Error while compiling statement: FAILED: ParseException line 1:72 cannot recognize input near '?' '<EOF>' '<EOF>' in expression specification:28:27, org.apache.hive.service.cli.operation.Operation:toSQLException:Operation.java:400, org.apache.hive.service.cli.operation.SQLOperation:prepare:SQLOperation.java:187, org.apache.hive.service.cli.operation.SQLOperation:runInternal:SQLOperation.java:271, org.apache.hive.service.cli.operation.Operation:run:Operation.java:337, org.apache.hive.service.cli.session.HiveSessionImpl:executeStatementInternal:HiveSessionImpl.java:439, org.apache.hive.service.cli.session.HiveSessionImpl:executeStatement:HiveSessionImpl.java:405, sun.reflect.GeneratedMethodAccessor51:invoke::-1, sun.reflect.DelegatingMethodAccessorImpl:invoke:DelegatingMethodAccessorImpl.java:43, java.lang.reflect.Method:invoke:Method.java:497, org.apache.hive.service.cli.session.HiveSessionProxy:invoke:HiveSessionProxy.java:78, org.apache.hive.service.cli.session.HiveSessionProxy:access$000:HiveSessionProxy.java:36, org.apache.hive.service.cli.session.HiveSessionProxy$1:run:HiveSessionProxy.java:63, java.security.AccessController:doPrivileged:AccessController.java:-2, javax.security.auth.Subject:doAs:Subject.java:422, org.apache.hadoop.security.UserGroupInformation:doAs:UserGroupInformation.java:1917, org.apache.hive.service.cli.session.HiveSessionProxy:invoke:HiveSessionProxy.java:59, com.sun.proxy.$Proxy22:executeStatement::-1, org.apache.hive.service.cli.CLIService:executeStatement:CLIService.java:257, org.apache.hive.service.cli.thrift.ThriftCLIService:ExecuteStatement:ThriftCLIService.java:501, org.apache.hive.service.cli.thrift.TCLIService$Processor$ExecuteStatement:getResult:TCLIService.java:1313, org.apache.hive.service.cli.thrift.TCLIService$Processor$ExecuteStatement:getResult:TCLIService.java:1298, org.apache.thrift.ProcessFunction:process:ProcessFunction.java:39, org.apache.thrift.TBaseProcessor:process:TBaseProcessor.java:39, org.apache.hive.service.auth.TSetIpAddressProcessor:process:TSetIpAddressProcessor.java:56, org.apache.thrift.server.TThreadPoolServer$WorkerProcess:run:TThreadPoolServer.java:286, java.util.concurrent.ThreadPoolExecutor:runWorker:ThreadPoolExecutor.java:1142, java.util.concurrent.ThreadPoolExecutor$Worker:run:ThreadPoolExecutor.java:617, java.lang.Thread:run:Thread.java:745, *org.apache.hadoop.hive.ql.parse.ParseException:line 1:72 cannot recognize input near '?' '<EOF>' '<EOF>' in expression specification:32:5, org.apache.hadoop.hive.ql.parse.ParseDriver:parse:ParseDriver.java:204, org.apache.hadoop.hive.ql.parse.ParseDriver:parse:ParseDriver.java:166, org.apache.hadoop.hive.ql.Driver:compile:Driver.java:522, org.apache.hadoop.hive.ql.Driver:compileInternal:Driver.java:1356, org.apache.hadoop.hive.ql.Driver:compileAndRespond:Driver.java:1343, org.apache.hive.service.cli.operation.SQLOperation:prepare:SQLOperation.java:185], sqlState:42000, errorCode:40000, errorMessage:Error while compiling statement: FAILED: ParseException line 1:72 cannot recognize input near '?' '<EOF>' '<EOF>' in expression specification), Query: select number_impala, string_impala from my_table where number_impala = ?.
    ... 10 more

问题是PreparedStatement中用于接收绑定的字符。我还跟着this示例和?存在。那么我做错了什么?

1 个答案:

答案 0 :(得分:1)

这里的问题是:

UseNativeQuery=1

将此值更改为0可让我克服我的问题。