Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Athena JDBC PreparedStatement

I don't manage to make AWS Athena JDBC driver working with PreparedStatement and binded variables. If I put the desired value of a column directly in the SQL string, it works. But if I use placeholders '?' and I bind variables with setters of PreparedStatement, it does not work. Of course, we know we have to use the second way of doing (for caching, avoid SQL injection and so on).

I use JDBC Driver AthenaJDBC42_2.0.2.jar. I get the following error when trying to use placeholders '?' in the SQL String. The error is thrown when I get the PreparedStatement from the JDBC Connection. It complains about parameters not being found. But I set them after in the code. How can I set the parameters before getting the 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

Am I doing something wrong ? Here is the code

    @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));
                }
            }
        }
    }
}
like image 728
Comencau Avatar asked May 28 '18 10:05

Comencau


People also ask

Does the Athena JDBC driver support AWS credentials?

However, the Athena JDBC driver supports reading credentials only from the AWS CLI credentials file. Because of this limitation, the profiles in the preceding examples must be placed in the same AWS CLI credentials file (~/.aws/credentials) and shouldn't be prefixed with profile.

How do I connect to Amazon S3 using Athena JDBC?

To do this, use the Amazon S3 links on this page to download, install, and configure the Athena JDBC driver. For permissions information, see Access through JDBC and ODBC connections. Starting with version 2.0.24, two versions of the driver are available: one that includes the AWS SDK, and one that does not.

How do I switch roles before connecting to the Athena JDBC driver?

To switch roles before connecting to the Athena JDBC driver, use the source_profile option in the named profile: 1. On the machine where the Athena JDBC driver is installed, add a named profile to the AWS CLI credentials file ( ~/.aws/credentials ).

Why should I unblock the JDBC port in Athena?

Therefore, unblock this port when you use a JDBC/ODBC driver to connect to Athena. If this port is blocked, your business intelligence tool might time out or fail to show query results when you run a query.


2 Answers

Athena supports only SQL functions listed here Athena SQL functions which in turn are based on Functions and Operators Presto version 0.172 with the following list of Athena's SQL related limitations. Prepared statements can be used in the new version of Presto Presto Documentation. However, Athena does not support this new version yet. You can always write to the Athena support team to ask for the PREPARE function to be added.

like image 52
Fabsklo Avatar answered Sep 22 '22 07:09

Fabsklo


Currently, I don't think the Athena JDBC jar supports prepared statements with position variables. While using myBatis, the prepared statement variable #{variable} didn't work, while the string replacement ${variable} did.

  • select * from my_table where col = #{col} limit 10 didn't work
  • select * from my_table where col = ${col} limit 10 did work

I think the error occurs because the Athena SConnection object doesn't support position variables, but since I don't have the source, I can't verify.

like image 22
jhorner Avatar answered Sep 19 '22 07:09

jhorner