Amazon Simpledb Java
From Foochal
This is a step by step guide to writing your first java program for Amazon Simpledb.
Sign up for Amazon Simpledb
You can sign up for the simpledb service here: http://aws.amazon.com/simpledb/
Once you are signed up, you will get an access key and a secret key. Note it down as you will need it to access Simpledb from your java application.
Download a java client
Amazon simpledb is a generic web service and you will need a java client to access it from your java program. I recommend simplejpa
Downloading the dependencies
I like maven to build my project. Here is a list of maven repositories and jars that I used build my project.
<repositories>
<repository>
<name>jets3t</name>
<id>jets3t</id>
<url>http://jets3t.s3.amazonaws.com/maven2
</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.java.dev.jets3t</groupId>
<artifactId>jets3t</artifactId>
<version>0.6.1</version>
</dependency>
<dependency>
<groupId>net.sf.jsr107cache</groupId>
<artifactId>jsr107cache</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>local</groupId>
<artifactId>typica</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>net.sf.scannotation</groupId>
<artifactId>scannotation</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager
</artifactId>
<version>3.2.1.ga</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>geronimo-spec</groupId>
<artifactId>geronimo-spec-jta</artifactId>
<version>1.0.1B-rc4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>local</groupId>
<artifactId>simplejpa</artifactId>
<version>0.7</version>
</dependency>
</dependencies>
The local dependencies above need to be downloaded from their source sites directly. For example, download the typica.jar version 1.4.1 and then use the following command to install it locally.
mvn install:install-file -DgroupId=local -DartifactId=typica -Dversion=1.4.1 -Dpackaging=jar -Dfile=typica.jar
Write a simple entity (Listing.java) that you want to persist
If you are familiar with JPA, then there is nothing new here
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Listing {
private String id;
private String name;
@Id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Create a dao class (ListingDao.java) which will do the lookup
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.spaceprogram.simplejpa.EntityManagerFactoryImpl;
import com.spaceprogram.simplejpa.SimpleEntityManager;
public class ListingDao {
private static final Log log = LogFactory.getLog(ListingDao.class);
private static EntityManagerFactoryImpl entityManagerFactory = new EntityManagerFactoryImpl(
"junit", null);
public static void persist(Listing listing) {
Stopwatch w = new Stopwatch();
try {
SimpleEntityManager em = (SimpleEntityManager) entityManagerFactory
.createEntityManager();
em.persistAsync(listing);
em.close();
} finally {
log.info("persist: " + w.getDisplay());
}
}
public static Listing findById(String id) {
Stopwatch w = new Stopwatch();
try {
SimpleEntityManager em = (SimpleEntityManager) entityManagerFactory.createEntityManager();
Listing listing = em.find(Listing.class, id);
em.close();
return listing;
} finally {
log.info("find: " + w.getDisplay());
}
}
public static Listing findAsyncById(String id) {
Stopwatch w = new Stopwatch();
try {
SimpleEntityManager em = (SimpleEntityManager) entityManagerFactory.createEntityManager();
Listing listing = em.find(Listing.class, id);
em.close();
return listing;
} finally {
log.info("find: " + w.getDisplay());
}
}
public static void shutdown() {
entityManagerFactory.close();
}
}
Finally, write a unit test (ListingDaoTest.java) which tests the dao
package local.simplejpademo;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import junit.framework.TestCase;
public class ListingTest extends TestCase {
public void testFindMany() throws InterruptedException {
List<Listing> allListings = new ArrayList<Listing>();
for (int i = 0; i < 10; i++) {
Listing listing = new Listing();
String id = UUID.randomUUID().toString();
listing.setId(id);
listing.setName("2006 Toyota Camry");
allListings.add(listing);
ListingDao.persist(listing);
}
Thread.sleep(5000);
for (Listing listing : allListings) {
Listing listingFound = ListingDao.findById(listing.getId());
assertNotNull(listingFound);
assertEquals(listing.getName(), listingFound.getName());
}
}
public void testFindSingle() throws InterruptedException {
Listing listing = new Listing();
String id = UUID.randomUUID().toString();
listing.setId(id);
listing.setName("2006 Toyota Camry");
ListingDao.persist(listing);
Listing listingFound = ListingDao.findById(id);
assertNotNull(listingFound);
assertEquals(listing.getName(), listingFound.getName());
Thread.sleep(5000);
}
@Override
public void tearDown() {
ListingDao.shutdown();
}
}
Specify your amazon accessKey and secretKey
Create a file called simplejpa.properties with your amazon accessKey and secretKey
accessKey = AAAAAAAAAAAAAAAAAAAAAAA secretKey = SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
Browsing your amazon simple db data
I like visually confirming that my data is indeed stored. You can use http://code.google.com/p/t-437 to browse your Amazon Simpledb "tables" and "rows".

