Examples: Using the Connector in a Java Application

The following code examples demonstrate how to use the Simba Amazon Athena JDBC Connector in a Java application:

Example: Creating a Connector

This example demonstrates how to create an instance of the Simba Amazon Athena JDBC Connector in a Java application:

Properties info = new Properties();

info.put("User", "AWSAccessKey");

info.put("Password", "AWSSecretAccessKey");

info.put("S3OutputLocation", "s3://my-athena-result-bucket/test/");

 

Class.forName("com.simba.athena.jdbc.Driver");

 

Connection connection = DriverManager.getConnection("jdbc:awsathena://AwsRegion=us-east-1;", info);

Examples: Using a Credentials Provider

The following examples demonstrate different ways of using a credentials provider that implements the AWSCredentialsProvider interface with the JDBC connector:

For more information about configuring the connector to authenticate your connection using a credentials provider, see Using the AWSCredentialsProvider Interface.

Example: DefaultAWSCredentialsProviderChain

This example demonstrates how to use the DefaultAWSCredentialsProviderChain. You do not need to supply any credential provider arguments because they are taken from one of the locations in the default credentials provider chain. For detailed information about configuring default credentials, see "Using the Default Credential Provider Chain" in the AWS SDK for Java Developer Guide: http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html#credentials-default.

Properties info = new Properties();

info.put("AwsCredentialsProviderClass", "com.simba.athena.amazonaws.auth.DefaultAWSCredentialsProviderChain");

Example: PropertiesFileCredentialsProvider

This example demonstrates how to use the PropertiesFileCredentialsProvider, which uses only one argument and obtains the required credentials from a file:

Properties info = new Properties();

info.put("AwsCredentialsProviderClass", "com.simba.athena.amazonaws.auth.PropertiesFileCredentialsProvider");

info.put("AwsCredentialsProviderArguments", "/Users/myUser/.athenaCredentials");

With the implementation shown above, the credentials provider obtains the required credentials from a file named /Users/myUser/.athenaCredentials, which should contain the following text:

accessKey=[YourAccessKey]

secretKey=[YourSecretKey]

The variables are defined as follows:

  • [YourAccessKey] is the access key provided by your AWS account.
  • [YourSecretKey] is the secret key provided by your AWS account.

Example: InstanceProfileCredentialsProvider

This example demonstrates how to use the InstanceProfileCredentialsProvider. You do not need to supply any credential provider arguments because they are provided using the EC2 instance profile for the instance on which you are running your application. However, you still need to set the AwsCredentialsProviderClass property to this class name.

Properties info = new Properties();

info.put("AwsCredentialsProviderClass", "com.simba.athena.amazonaws.auth.InstanceProfileCredentialsProvider");

Example: CustomSessionCredentialsProvider

CustomSessionsCredentialsProvider is not included with the connector, so you must create it before you can use it.

The credentials object can use either BasicSessionCredentials or AWSCredentials.

This example demonstrates how to create a custom credentials provider named CustomSessionCredentialsProvider that uses an access key, secret key, and session token:

package com.example;

 

import com.simba.athena.amazonaws.auth.AWSCredentials;

com.simba.athena.amazonaws.auth.AWSCredentialsProvider;

import com.simba.athena.amazonaws.auth.BasicSessionCredentials;

 

public class CustomSessionCredentialsProvider implements AWSCredentialsProvider

{

    private BasicSessionCredentials m_credentials;

// AWSCredentials can also be used instead of BasicSessionCredentials

//Set aws_credentials_provider_class = "com.simba.athena.amazonaws.custom.athena.jdbc.CustomIAMRoleAssumptionSAMLCredentialsProvider"

// set AwsCredentialsProviderArguments = "<accessID>,<secretKey>,<sessionToken>"

 

    public CustomSessionCredentialsProvider(

        String awsAccessKey,

        String awsSecretKey,

        String sessionToken)

    {

        m_credentials =

            new BasicSessionCredentials(

                awsAccessKey,

                awsSecretKey,

                sessionToken);

    }

 

    @Override

    public AWSCredentials getCredentials()

    {

        return m_credentials;

    }

 

    @Override

    public void refresh(){

//Use this method if refresh token

}

}

The following example demonstrates how to use CustomSessionCredentialsProvider after it has been created:

Properties info = new Properties();

info.put("AwsCredentialsProviderClass", "com.example.CustomSessionCredentialsProvider");

String providerArgs = "My_Access_Key," + "My_Secret_Key," + "My_Token";

info.put("AwsCredentialsProviderArguments", providerArgs);

Example: Executing a SELECT Query

This example demonstrates how to execute a SELECT query:

Statement statement = connection.createStatement();

ResultSet queryResults = statement.executeQuery("SELECT * FROM integer_table");

Example: Running a CREATE Statement

This example demonstrates how to run a CREATE statement:

Statement statement = connection.createStatement();

ResultSet queryResults = statement.executeQuery("CREATE EXTERNAL TABLE IF NOT EXISTS tableName (Col1 String) LOCATION 's3://bucket/tableLocation'");

Example: Listing Tables

This example demonstrates how to list the tables from the result set of a query:

import java.sql.*;

import java.util.Properties;

 

public class AthenaJDBCDemo {

static final String athenaUrl = "jdbc:awsathena://AwsRegion=us-east-1;";

 

public static void main(String[] args) {

Connection conn = null;

Statement statement = null;

 

try {

Class.forName("com.simba.athena.jdbc.Driver");

Properties info = new Properties();

info.put("S3OutputLocation", "s3://my-athena-result-bucket/test/");

info.put("LogPath", "/Users/myUser/athenaLog");

info.put("LogLevel","6");

info.put("AwsCredentialsProviderClass","com.simba.athena.amazonaws.auth.PropertiesFileCredentialsProvider");

info.put("AwsCredentialsProviderArguments","/Users/myUser/.athenaCredentials");

String databaseName = "default";

 

System.out.println("Connecting to Athena...");

conn = DriverManager.getConnection(athenaUrl, info);

 

System.out.println("Listing tables...");

String sql = "show tables in "+ databaseName;

statement = conn.createStatement();

ResultSet rs = statement.executeQuery(sql);

 

while (rs.next()) {

//Retrieve table column.

String name = rs.getString("tab_name");

 

//Display values.

System.out.println("Name: " + name);

}

rs.close();

conn.close();

} catch (Exception ex) {

ex.printStackTrace();

} finally {

try {

if (statement != null)

statement.close();

} catch (Exception ex) {

 

}

try {

if (conn != null)

conn.close();

} catch (Exception ex) {

 

ex.printStackTrace();

}

}

System.out.println("Finished connectivity test.");

}

}