Back Home

Amazon DynamoDB is a proprietary Nosql Database

Item's have key's for search DynamoDB supports two different kinds of primary keys:


Describe a table

aws dynamodb describe-table --table-name prod-catalog-v2 --region eu-west-1

and scan

aws dynamodb scan --table-name prod-catalog-v2 --region eu-west-1

Create a Table

Tables are created with a range of attributes:

S - the attribute is of type String
N - the attribute is of type Number
B - the attribute is of type Binary

In code we construct a range of attributes passed to create the table:

 // Key attribute definition
        ArrayList<AttributeDefinition> attrDefList= new ArrayList<AttributeDefinition>();
        attrDefList.add(new AttributeDefinition().withAttributeName("bookId").withAttributeType("N"));
        attrDefList.add(new AttributeDefinition().withAttributeName("yop").withAttributeType("S"));

these attributes can be used to be 'indexed' as a primary key

   ArrayList<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
        ks.add(new KeySchemaElement("bookId", KeyType.HASH));
        ks.add(new KeySchemaElement("yop",KeyType.RANGE));

And then create the table:

 CreateTableRequest request = new CreateTableRequest()

The bit I skipped is the “Provisioned Throughput”

  ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()


 private static void updateAddNewAttribute() {
        Table table = dynamoDB.getTable(tableName);
        try {
            UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("Id", 121)
                .withUpdateExpression("set #na = :val1").withNameMap(new NameMap().with("#na", "NewAttribute"))
                .withValueMap(new ValueMap().withString(":val1", "Some value")).withReturnValues(ReturnValue.ALL_NEW);
            UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
            // Check the response.
            System.out.println("Printing item after adding new attribute...");
        catch (Exception e) {
            System.err.println("Failed to add new attribute in " + tableName);


Table events can be streamed. Those events are bundled into 'shards'

  • ListStreams - returns a list of stream descriptors for the current account and endpoint. You can optionally request just the stream descriptors for a particular table name.
  • DescribeStream - returns detailed information about a given stream. The output includes a list of shards associated with the stream, including the shard IDs.
  • GetShardIterator - returns a shard iterator, which describes a location within a shard. You can request that the iterator provide access to the oldest point, the newest point, or a particular point in the stream.
  • GetRecords - returns the stream records from within a given shard. You must provide the shard iterator returned from a GetShardIterator request.

Note also the which says “Using the Kinesis Adapter is the recommended way to consume Streams from DynamoDB.”

An example of a kinesis stream listener

Running Local Version


java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar --port 8001

Running and Embedded Dynamodb

  FileUtils.copyFile(new File("libs/"), new File(System.getProperty("") + "/"));
        System.load(System.getProperty("") + "/");
        // now create client and table
        AmazonDynamoDBLocal amazonDynamoDBLocal = DynamoDBEmbedded.create();
        dbLocal = amazonDynamoDBLocal.amazonDynamoDB();
        db = new DynamoDB(dbLocal);
        CreateTableResult result = dbLocal.createTable(new CreateTableRequest().withTableName("catalog-v2")
                .withKeySchema(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition().withAttributeName("id").withAttributeType(ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L))
        Table table = db.getTable("catalog-v2");
        File test = new File("test");
                System.err.println("local path " + test.getAbsolutePath());
        Resource resouce = resourceLoader.getResource("classpath:testData/catalog-v2.json");
        String data = FileUtils.readFileToString( resouce.getFile());
        JSONObject jsonData = new JSONObject(data);
        // put json direct
        Item item = table.getItem(new PrimaryKey("id", "516037-289558-289559"));

Testing with Spring

@SpringBootTest(classes = Application.class)
@TestPropertySource(properties = { 
  "" })
public class ProductInfoRepositoryIntegrationTest {
    private DynamoDBMapper dynamoDBMapper;
    private AmazonDynamoDB amazonDynamoDB;
amazon_dynamodb.txt · Last modified: 2018/06/11 05:48 by root
RSS - 200 © CrosswireDigitialMedia Ltd