Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Create a Data Access Object for Amazon S3 Data using JDBI
A brief overview of creating a SQL Object API for Amazon S3 data in JDBI.
JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for Amazon S3 integrates connectivity to live Amazon S3 data in Java applications. By pairing these technologies, you gain simple, programmatic access to Amazon S3 data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read Amazon S3 data.
Create a DAO for the Amazon S3 ObjectsACL Entity
The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.
public interface MyObjectsACLDAO {
//request specific data from Amazon S3 (String type is used for simplicity)
@SqlQuery("SELECT OwnerId FROM ObjectsACL WHERE Name = :name")
String findOwnerIdByName(@Bind("name") String name);
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to Amazon S3
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Amazon S3.
To authorize Amazon S3 requests, provide the credentials for an administrator account or for an IAM user with custom permissions. Set AccessKey to the access key Id. Set SecretKey to the secret access key.
Note: You can connect as the AWS account administrator, but it is recommended to use IAM user credentials to access AWS services.
For information on obtaining the credentials and other authentication methods, refer to the Getting Started section of the Help documentation.
Built-in Connection String Designer
For assistance in constructing the JDBC URL, use the connection string designer built into the Amazon S3 JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.amazons3.jar
Fill in the connection properties and copy the connection string to the clipboard.
A connection string for Amazon S3 will typically look like the following:
jdbc:amazons3:AccessKey=a123;SecretKey=s123;
Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.
DBI dbi = new DBI("jdbc:amazons3:AccessKey=a123;SecretKey=s123;");
MyObjectsACLDAO dao = dbi.open(MyObjectsACLDAO.class);
//do stuff with the DAO
dao.close();
Read Amazon S3 Data
With the connection open to Amazon S3, simply call the previously defined method to retrieve data from the ObjectsACL entity in Amazon S3.
//disply the result of our 'find' method
String ownerId = dao.findOwnerIdByName("TestBucket");
System.out.println(ownerId);
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Amazon S3 by integrating with the CData JDBC Driver for Amazon S3. Download a free trial and work with live Amazon S3 data in custom Java applications today.