![]() |
VOOZH | about |
Spring Boot is a framework that makes engineering Java web applications easier. It offers the ability to create standalone applications with minimal configuration. When paired with the CData JDBC driver for Amazon Athena, Spring Boot can work with live Amazon Athena data. This article shows how to configure data sources and retrieve data in your Java Spring Boot Application, using the CData JDBC Driver for Amazon Athena.
With built-in optimized data processing, the CData JDBC Driver offers unmatched performance for interacting with live Amazon Athena data. When you issue complex SQL queries to Amazon Athena, the driver pushes supported SQL operations, like filters and aggregations, directly to Amazon Athena and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations). Its built-in dynamic metadata querying allows you to work with and analyze Amazon Athena data using native data types.
CData provides the easiest way to access and integrate live data from Amazon Athena. Customers use CData connectivity to:
Users frequently integrate Athena with analytics tools like Tableau, Power BI, and Excel for in-depth analytics from their preferred tools.
To learn more about unique Amazon Athena use cases with CData, check out our blog post: https://www.cdata.com/blog/amazon-athena-use-cases.
In an IDE (in this tutorial, we use IntelliJ), choose a Maven project:
π Create a new Maven project
In the generated project, go to the pom.xml file, and add the required dependencies for Spring Boot:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.2</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-install-plugin</artifactId> <version>2.5.1</version> <executions> <execution> <id>id.install-file</id> <phase>clean</phase> <goals> <goal>install-file</goal> </goals> <configuration> <file>C:\Program Files\CData[product_name] ####\lib\cdata.jdbc.amazonathena.jar</file> <groupId>org.cdata.connectors</groupId> <artifactId>cdata-amazonathena-connector</artifactId> <version>23</version> <packaging>jar</packaging> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>org.cdata.connectors</groupId> <artifactId>cdata-amazonathena-connector</artifactId> <version>23</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <distributionManagement> <repository> <uniqueVersion>false</uniqueVersion> <id>test</id> <name>My Repository</name> <url>scp://repo/maven2</url> <layout>default</layout> </repository> </distributionManagement> </project>
Note: The year (####) and the version number (as seen in the provided XML script) should be adjusted according to the current version of the CData JDBC driver being utilized.
In the java directory, create a new package. Usually the name of the package is the name of the groupId
(com.example) followed by the artifactId (.MDS).
π Create a new Package in java folder
π Enter the name of the Package
Mark the "java" directory as the "Sources Root" (denoted by a blue color). To do this, right-click the java directory and choose Mark Directory as -> Sources Root (As shown below). Additionally, mark the "resources" directory as the "Resources Root."
π Mark Directory as Sources Root
Create an "application.properties" file to store the database connection properties. To do this, right-click on the "resources" folder, opt for New -> File, input the file name as "application.properties," and press Enter.
π Create a new (configuration) file
π Name the file as application.properties
In the application.properties file, we set the configuration properties for the Amazon Athena JDBC Driver, using the Class name and JDBC URL:
spring.datasource.driver=cdata.jdbc.amazonathena.AmazonAthenaDriver spring.datasource.url=jdbc:amazonathena:AWSAccessKey='a123';AWSSecretKey='s123';AWSRegion='IRELAND';Database='sampledb';S3StagingDirectory='s3://bucket/staging/';
For assistance in constructing the JDBC URL, use the connection string designer built into the Amazon Athena JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.amazonathena.jar
To authorize Amazon Athena requests, provide the credentials for an administrator account or for an IAM user with custom permissions: Set to the access key Id. Set to the secret access key.
Note: Though you can connect as the AWS account administrator, it is recommended to use IAM user credentials to access AWS services.
To obtain the credentials for an IAM user, follow the steps below:
To obtain the credentials for your AWS root account, follow the steps below:
If you are using the CData Data Provider for Amazon Athena 2018 from an EC2 Instance and have an IAM Role assigned to the instance, you can use the IAM Role to authenticate. To do so, set to true and leave and empty. The CData Data Provider for Amazon Athena 2018 will automatically obtain your IAM Role credentials and authenticate with them.
In many situations it may be preferable to use an IAM role for authentication instead of the direct security credentials of an AWS root user. An AWS role may be used instead by specifying the . This will cause the CData Data Provider for Amazon Athena 2018 to attempt to retrieve credentials for the specified role. If you are connecting to AWS (instead of already being connected such as on an EC2 instance), you must additionally specify the and of an IAM user to assume the role for. Roles may not be used when specifying the and of an AWS root user.
For users and roles that require Multi-factor Authentication, specify the and connection properties. This will cause the CData Data Provider for Amazon Athena 2018 to submit the MFA credentials in a request to retrieve temporary authentication credentials. Note that the duration of the temporary credentials may be controlled via the (default 3600 seconds).
In addition to the and properties, specify , and . Set to the region where your Amazon Athena data is hosted. Set to a folder in S3 where you would like to store the results of queries.
If is not set in the connection, the data provider connects to the default database set in Amazon Athena.
π Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)After setting the properties in the application.properties file, we now configure them.
First, we mark the Amazon Athena data source as our primary data source. Then, we create a Data Source Bean.
Create a DriverManagerDataSource.java file and create a Bean within it, as shown below. If @Bean gives an error, Spring Boot may not have loaded properly. To fix this, go to File -> Invalidate Caches and restart. Additionally, make sure that Maven has added the Spring Boot dependencies.
To create a data source bean, we use the DriverManagerDataSource Class. This class allows us to set
the properties of the data source. To create this Java class, right-click on "com.example.MDS" package, and choose New -> Java Class.
π Create a new Java class
The following code shows the bean definition of our data source. Each driver should have a bean.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import javax.sql.DataSource;
public class DriverManagerDataSource{
@Autowired
private static Environment env;
@Bean(name ="AmazonAthena")
@Primary
public static DataSource AmazonAthenaDataSource()
{
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("cdata.jdbc.amazonathena.AmazonAthenaDriver");
dataSourceBuilder.url("jdbc:amazonathena:AWSAccessKey='a123';AWSSecretKey='s123';AWSRegion='IRELAND';Database='sampledb';S3StagingDirectory='s3://bucket/staging/';");
return dataSourceBuilder.build();
}
//@Override
public void setEnvironment( final Environment environment) {
env=environment;
}
}
Next, move the Amazon Athena jar file to the Documents folder (see path in command below) - The idea is to have a path without any spaces for the jar file. Then, click the
Maven icon (top right corner of IntelliJ) and click "Execute Maven Goal." Now, run the following command:
π Execute Maven Goal
π Run Maven install command
mvn install:install-file "-Dfile=C:\Program Files\CData[product_name] ####\lib\cdata.jdbc.amazonathena.jar" -DgroupId=org.cdata.connectors -DartifactId=cdata-amazonathena-connector -Dversion=23 -Dpackaging=jar
Follow either of the given steps to run this command:
After pressing enter, we see the following output:
π Successful installation of the JDBC driver
The last step is testing the connection. Create a new Java class following the format
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import java.sql.Connection;
import java.sql.SQLException;
import static com.example.demo.DriverManagerDataSources.AmazonAthenaDataSource;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MDSApplication {
//remove the comment on the line below
public static void main (){
SpringApplication.run(DemoApplication.class, args);
Connection conn = AmazonAthenaDataSource().getConnection();
System.out.println("Catalog: "+ conn.getCatalog());
}
}
The output generated should look like this:
π Successful test connection
Download a free, 30-day trial of the CData JDBC Driver for Amazon Athena and start working with your live Amazon Athena in Spring Boot.
Download a free trial of the Amazon Athena Driver to get started:
Download NowLearn more:
π Amazon Athena IconRapidly create and deploy powerful Java applications that integrate with Amazon Athena.