- Newest
- Most votes
- Most comments
Please refer to the document .Example: CustomSessionCredentialsProvider If you are still facing issue with above example, please create support ticket with us and we can work in screen share to discuss your issue in your local environment.
Also, please note above AthenaJDBC.java
is working fine in local IntelliJ.
download the relevant athena jdbc driver and add jar to the classpath
If you're using an IDE like IntelliJ or Eclipse, you can add the JAR file as a library or dependency.
If you're running your application from the command line, include the JAR file in the classpath while running your Java application:
java -cp /path/to/your/athena-jdbc-driver.jar:/path/to/your/application.jar your.main.ClassName
Hello,
Thank you for reporting this issue.
Checking the shared error message, it seems that while running the script from corretto-11 environment, it failed with "Unable to find suitable JDBC driver".
Since I don't have visibility how you are running corretto-11 environment, I used docker environment to reproduce this error.
While reproducing this issue, I was able to run the Athena query from my end. So, I suggested you to try the following options in your end.
Steps used for Athena JDBC Driver in corretto-11 using Docker environment:
Step 1: Download jar file"AthenaJDBC42-2.0.35.1000.jar" from the document[1]
Step 2: Create file with content of Dockerfile[2] and AthenaJDBC.java[3] from this re:post
Please note: The file name should be same i.e Dockerfile
for docker content and AthenaJDBC.java
for sample script
Step 3: Replace Java variables: ATHENA_SAMPLE_QUERY
, REGION
, USERNAME
, PASSWORD
and S3LOCATION
with your actual setup.
Step 4: Run following docker CLI command to build image and run docker container
docker build -t athena-jdbc .
docker run athena-jdbc
Please note: Above docker command is required to run where you have Dockerfile, downloaded jar from step 1 and AthenaJDBC.java file placed.
Now you should be able to see the Table column values in the docker log.
References:
[1] https://docs.aws.amazon.com/athena/latest/ug/connect-with-jdbc.html
[2] Dockerfile:
FROM amazoncorretto:11
COPY ./AthenaJDBC* ./
RUN echo $'Main-Class: AthenaJDBC' > Manifest.txt && javac AthenaJDBC.java
RUN jar -cfvm AthenaJDBC.jar Manifest.txt AthenaJDBC.class
CMD ["java","-cp","AthenaJDBC42-2.0.35.1000.jar:AthenaJDBC.jar", "AthenaJDBC"]
[3] AthenaJDBC.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AthenaJDBC {
// Variables
public static final String ATHENA_SAMPLE_QUERY = "SELECT * FROM default.table_name LIMIT 10;";
public static final String REGION = "Region name where you want to run Athena Query";
public static final String USERNAME = "AWS Access key";
public static final String PASSWORD = "AWS Secret key";
public static final String S3LOCATION = "S3 bucket location where you want to store Athena query output";
// Main Function
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection = null;
Class.forName("com.simba.athena.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:awsathena://AwsRegion=" + REGION +";User=" + USERNAME
+ ";Password=" + PASSWORD + ";S3OutputLocation=" + S3LOCATION + ";");
Statement statement = connection.createStatement();
ResultSet queryResults = statement.executeQuery(ATHENA_SAMPLE_QUERY);
while(queryResults.next()){
System.out.println(queryResults.getString(2));
}
}
}
If the environment is different than the tested above, I would like to suggest you to create support case with complete error messages.
Thank you for using Amazon Athena.
Hi
I am using
My use case is using CusomtSessionCredentialProvider.I already use the JDBC driver that u mentioned.I run it from local IntelliJ
Properties properties=new Properties();
properties.put("AwsCredentialsProviderClass","org.example.CustomSessionCredentialProvider");
String accessKey=System.getenv("AWS_ACCESS_KEY_ID");
String secretKey=System.getenv("AWS_SECRET_ACCESS_KEY");
String token=System.getenv("AWS_SESSION_TOKEN");
StringBuilder providerArgs=new StringBuilder(accessKey).append(",").append(secretKey).append(",").append(token);
properties.put("AwsCredentialsProviderArguments", providerArgs);
properties.put("LogLevel","6");
// System.setProperty("jdbc.drivers","com.simba.athena.jdbc42.Driver");
properties.setProperty("jdbc", "com.simba.athena.jdbc.Driver");
properties.put("S3OutputLocation", "s3://bucket/");
Connection connection = DriverManager.getConnection
("jdbc:awsathena://AwsRegion=us-east-1;", properties);
Statement statement= connection.createStatement();
ResultSet resultSet= statement.executeQuery("select * from table where dept = 'D11'");
while (resultSet.next()){
System.out.println(resultSet.getString("name")+","+resultSet.getString("id"));
}
public class CustomSessionCredentialProvider implements AWSSessionCredentialsProvider {
private BasicSessionCredentials m_credentials;
public CustomSessionCredentialProvider(
String awsAccessKey,
String awsSecretKey,
String sessionToken)
{
m_credentials =new BasicSessionCredentials(
awsAccessKey,
awsSecretKey,
sessionToken);
}
@Override
public AWSSessionCredentials getCredentials() {
return m_credentials;
}
@Override
public void refresh() {
}
}
Relevant content
- asked 4 months ago
- asked 5 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 23 days ago
- AWS OFFICIALUpdated 18 days ago
Athena .JDBC is working for me as well ..