Ready to get started?

Learn more about the CData JDBC Driver for Amazon DynamoDB or download a free trial:

Download Now

Create a Data Access Object for Amazon DynamoDB Data using JDBI

A brief overview of creating a SQL Object API for Amazon DynamoDB 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 DynamoDB integrates connectivity to live Amazon DynamoDB data in Java applications. By pairing these technologies, you gain simple, programmatic access to Amazon DynamoDB data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write Amazon DynamoDB data.

Create a DAO for the Amazon DynamoDB Lead 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 MyLeadDAO { //insert new data into Amazon DynamoDB @SqlUpdate("INSERT INTO Lead (FirstName, Revenue) values (:firstName, :revenue)") void insert(@Bind("firstName") String firstName, @Bind("revenue") String revenue); //request specific data from Amazon DynamoDB (String type is used for simplicity) @SqlQuery("SELECT Revenue FROM Lead WHERE FirstName = :firstName") String findRevenueByFirstName(@Bind("firstName") String firstName); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Amazon DynamoDB

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Amazon DynamoDB.

The connection to Amazon DynamoDB is made using your AccessKey, SecretKey, and optionally your Domain and Region. Your AccessKey and SecretKey can be obtained on the security credentials page for your Amazon Web Services account. Your Region will be displayed in the upper left-hand corner when you are logged into DynamoDB.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the Amazon DynamoDB JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.amazondynamodb.jar

Fill in the connection properties and copy the connection string to the clipboard.

A connection string for Amazon DynamoDB will typically look like the following:

jdbc:amazondynamodb:Access Key=xxx;Secret Key=xxx;Domain=amazonaws.com;Region=OREGON;

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:amazondynamodb:Access Key=xxx;Secret Key=xxx;Domain=amazonaws.com;Region=OREGON;"); MyLeadDAO dao = dbi.open(MyLeadDAO.class); //do stuff with the DAO dao.close();

Read Amazon DynamoDB Data

With the connection open to Amazon DynamoDB, simply call the previously defined method to retrieve data from the Lead entity in Amazon DynamoDB.

//disply the result of our 'find' method String revenue = dao.findRevenueByFirstName("Bob"); System.out.println(revenue);

Write Amazon DynamoDB Data

It is also simple to write data to Amazon DynamoDB, using the previously defined method.

//add a new entry to the Lead entity dao.insert(newFirstName, newRevenue);

Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Amazon DynamoDB by integrating with the CData JDBC Driver for Amazon DynamoDB. Download a free trial and work with live Amazon DynamoDB data in custom Java applications today.