Using Apache Gora with AWS DynamoDB

DZone 's Guide to

Using Apache Gora with AWS DynamoDB

This article will provide basic instructions to setup AWS DynamoDB as a datastore for Apache Gora.

· Database Zone ·
Free Resource

Cloud architectures are becoming widely used because of various advantages. Everyone is moving their infrastructure to completely on cloud or hybrid. This article will provide basic instructions to setup AWS DynamoDB as a datastore for Apache Gora. 

Below steps will help you setup assuming the reader is well versed with Java and Maven frameworks. We will use gora-tutorial module that comes along with the Apache-Gora archive for this setup.

Step 1

Download Gora from http://gora.apache.org/downloads.html. At the time of writing, we have Gora version at 0.8. Unzip the downloaded file and build the framework by using the below command inside the unzipped folder (apache-gora-0.8).


Step 2

 gora-tutorial/conf/gora.properties  file provides the details on the datastore provider. Below is an example of the DynamoDB datastore configuration.





Step 3

A mapping file is used to map the fields as well as to generate Java stubs for the objects that we plan to store in DynamoDB. A sample file is given below:


<?xml version="1.0" encoding="UTF-8"?>
  <table name="TestTable" readcunit="1" writecunit="1" package="org.apache.gora.tutorial.log.generated"> <!-- optional descriptors for tables -->
    <attribute name="id" type="S" key="hash"/>
    <attribute name="value" type="S"/>

Step 4

Gora has to be provided with the AWS credentials for creating table (if autocreateschema=true in gora.properties) and to write and read rows.

Create a file with name AwsCredentials.properties with your accessKey and secretKey as credentials.



Step 5

Generate Java stub files DB mapping file using the Dynamo compiler:

 bin/gora dynamocompiler gora-tutorial/conf/gora-dynamodb-mapping.xml  gora-tutorial/src/main/java/

The above command will create stubs TestTable.java which has all the code that is required for persistence.

Step 6

Create the below Java class, which will be used to test our DynamoDB integration. 


package org.apache.gora.tutorial;

import java.io.IOException;

import org.apache.gora.store.DataStore;
import org.apache.gora.store.DataStoreFactory;
import org.apache.gora.tutorial.log.generated.TestTable;
import org.apache.hadoop.conf.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GoraDynamoTest {
 private static final Logger log = LoggerFactory.getLogger(GoraDynamoTest.class);

 private DataStore < Long, TestTable > dataStore;

 public GoraDynamoTest() {
  try {
  } catch (IOException ex) {
   throw new RuntimeException(ex);

 private void init() throws IOException {
  //Data store objects are created from a factory. It is necessary to 
  //provide the key and value class. The datastore class is optional, 
  //and if not specified it will be read from the properties file
  dataStore = DataStoreFactory.getDataStore(Long.class, TestTable.class,
   new Configuration());

 public static void main(String[] args) throws Exception {
  log.debug("In Main");
  GoraDynamoTest test1 = new GoraDynamoTest();
  // TODO Auto-generated method stub
  TestTable table = new TestTable();

  if (table != null) {
   //store the pageview
   test1.storePageview((long) 1, table);
 private void storePageview(long key, TestTable pageview) throws Exception {
  log.info("Storing Pageview in: " + dataStore.toString());
  dataStore.put(key, pageview);

Step 7

Run the above Java program.

mvn # to build the source

bin/gora gora-tutorial org.apache.gora.tutorial.GoraDynamoTest 

This will create a table called TestTable in your AWS region if it doesnt exist and creates an entry into that table.

Happy coding.

apache gora ,dynamodb ,database ,tutorial ,aws

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}