AWS Athena JDBC PreparedStatement

时间:2018-05-28 10:25:06

标签: amazon-web-services jdbc amazon-athena

我无法使AWS Athena JDBC驱动程序与PreparedStatement和绑定变量一起使用。如果我将所需的列值直接放在SQL字符串中,它就可以工作。但是,如果我使用占位符'?'我用PreparedStatement的setter绑定变量,它不起作用。当然,我们知道我们必须使用第二种方式(用于缓存,避免SQL注入等)。

我使用JDBC Driver AthenaJDBC42_2.0.2.jar。尝试使用占位符时出现以下错误?'在SQL字符串中。从JDBC连接获取PreparedStatement时抛出错误。它抱怨没有找到参数。但是我在代码中设置了它们。如何在获得PreparedStatement之前设置参数:-)?

java.sql.SQLException: [Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client. SYNTAX_ERROR: line 1:1: Incorrect number of parameters: expected 1 but found 0

at com.simba.athena.athena.api.AJClient.executeQuery(Unknown Source)
at com.simba.athena.athena.dataengine.AJQueryExecutor.<init>(Unknown Source)
at com.simba.athena.athena.dataengine.AJDataEngine.prepare(Unknown Source)
at com.simba.athena.jdbc.common.SPreparedStatement.<init>(Unknown Source)
at com.simba.athena.jdbc.jdbc41.S41PreparedStatement.<init>(Unknown Source)
at com.simba.athena.jdbc.jdbc42.S42PreparedStatement.<init>(Unknown Source)
at com.simba.athena.jdbc.jdbc42.JDBC42ObjectFactory.createPreparedStatement(Unknown Source)
at com.simba.athena.athena.jdbc42.AJJDBC42ObjectFactory.createPreparedStatement(Unknown Source)
at com.simba.athena.jdbc.common.SConnection.prepareStatement(Unknown Source)
at com.simba.athena.jdbc.common.SConnection.prepareStatement(Unknown Source)
at ****************************************************
Caused by: com.simba.athena.support.exceptions.GeneralException: [Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client. SYNTAX_ERROR: line 1:1: Incorrect number of parameters: expected 1 but found 0
... 37 more

我做错了吗?这是代码

    @Test
public void testWhichFails() throws SQLException {
    try (Connection connection = athenaConnexion()) {
        String sql = "select * from my_table where col = ? limit 10";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setInt(1, 30);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    System.out.println("rs.getString(1) = " + rs.getString(1));
                }
            }
        }
    }
}

@Test
public void testWhichWorks() throws SQLException {
    try (Connection connection = athenaConnexion()) {
        String sql = "select * from my_table where col = 30 limit 10";
        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            //ps.setInt(1, 30);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    System.out.println("rs.getString(1) = " + rs.getString(1));
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

Athena仅支持此处Athena SQL functions列出的SQL函数,这些函数依次基于Functions and Operators Presto version 0.172和以下Athena's SQL related limitations列表。可以在新版本的Presto Presto Documentation中使用预准备的语句。但是,Athena尚不支持此新版本。您可以随时写信给Athena支持团队,要求添加PREPARE功能。

答案 1 :(得分:1)

目前,我认为Athena JDBC jar不支持带位置变量的预处理语句。使用myBatis时,预处理语句变量#{variable}不起作用,而字符串替换${variable}则不起作用。

  • select * from my_table where col = #{col} limit 10无效
  • select * from my_table where col = ${col} limit 10确实有效

我认为错误的发生是因为Athena SConnection对象不支持位置变量,但由于我没有源代码,我无法验证。