1

Running an ALTER TABLE ADD PARTITION using the Athena JDBC driver is throwing an Exception that is proving hard to understand, this is the code:

package none;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class test {

    public static void main(String[] args) throws Exception {
        Properties info = new Properties();

        info.put("User","myUser");
        info.put("Password","myPass");
        info.put("S3OutputLocation", "s3://my-bucket/output");

        Class.forName("com.simba.athena.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:awsathena://AwsRegion=us-east-1;",info);

        Statement statement = connection.createStatement();
        ResultSet queryResults = statement.executeQuery("ALTER TABLE test01 ADD PARTITION (col2 = 'b') LOCATION 's3://my-bucket/col2=b/'");             
    }

}

This is the error that the code above throws, note that the query runs OK in the Athena console if I copy paste it from the error message:

Exception in thread "main" java.sql.SQLDataException: [Simba][JDBC](11300) A ResultSet was expected but not generated from query "ALTER TABLE test01 ADD PARTITION (col2 = 'b') LOCATION 's3://my-bucket/col2=b/'". Query not executed. 
    at com.simba.athena.exceptions.ExceptionConverter.toSQLException(Unknown Source)
    at com.simba.athena.jdbc.common.SStatement.checkCondition(Unknown Source)
    at com.simba.athena.jdbc.common.SStatement.executeNoParams(Unknown Source)
    at com.simba.athena.jdbc.common.SStatement.executeNoParams(Unknown Source)
    at com.simba.athena.jdbc.common.SStatement.executeQuery(Unknown Source)
    at none.test.main(test.java:22)

However, if I replace the statement with a simple SELECT, it works fine:

//this works fine
ResultSet queryResults = statement.executeQuery("SELECT * FROM test01");
queryResults.next();
System.out.println(queryResults.getString("col1") + " -> " + queryResults.getString("col2"));

Here's the table definition:

CREATE EXTERNAL TABLE test01 (
  `col1` string
) PARTITIONED BY (
  col2 string 
)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
WITH SERDEPROPERTIES (
  'serialization.format' = ',',
  'field.delim' = ','
) LOCATION 's3://my-bucket/'
TBLPROPERTIES ('has_encrypted_data'='false');

The driver version used is the one provided on the Athena docs: https://s3.amazonaws.com/athena-downloads/drivers/JDBC/SimbaAthenaJDBC_2.0.9/AthenaJDBC42_2.0.9.jar

Any thoughts on how to address this? Thanks!

Ilya Kisil
  • 2,490
  • 2
  • 17
  • 31
jmng
  • 2,479
  • 1
  • 25
  • 38

1 Answers1

3

You appear to be using statement.executeQuery(...) to execute a SQL statement that isn't a query.

Try using statement.execute(...) instead:

    statement.execute("ALTER TABLE test01 ADD PARTITION (col2 = 'b') LOCATION 's3://my-bucket/col2=b/'");             
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • What a blunder! I was mislead by the examples using executeQuery for CREATE and no useful error messages being produced, but execute() is the way to go, thanks. – jmng Oct 03 '19 at 08:50