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 是一个简单的 Hive 表,其中包含字符串类型列spd_field_label_id和SUBJECT_USER_ID。
简单准备语句使用 Hive2 在下面抛出错误堆栈跟踪。关于可能出现什么问题的任何指示?当使用语句而不是 PreparedStatement 并且不使用 ?用于参数绑定。
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)
stmt.executeQuery(query);
您使用了错误的方法。您已经准备好了声明。它已准备好执行。它应该是:
stmt.execute();