hakopako

Full-stack Engineer's blog

How to use Dynamodb local

日本語 | English

Overview

As a databases, DynamoDB of AWS could be your choice. But it's difficult to use it as a development environment because writing and reading are pricey.

So here is DynamoDBLocal for you. It's definitely good to use as development environment. And possibly make it work as product environment by running on EC2.

Official document

docs.aws.amazon.com

Things needed for here

  • JRE (using 7 here)
  • DynamoDB.local (Download from the official page)
    At this time, using dynamodb_local_2016-05-17.tar.gz
  • AWS SDK supported language * if needed

Passing JavaScript array with local console page will work enough, as long as you can access DynamoDBLocal with a browser.

Launch

Just launch the jar file like so.

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

$ java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -port 8001
Initializing DynamoDB Local with the following configuration:
Port:   8001
InMemory:       false
DbPath: null
SharedDb:       false
shouldDelayTransientStatuses:   false
CorsParams:     *

Let's try DynamoDBLocal !!!

once launched DynamoDBLocal, you can check the console here http://localhost:8001/shell/ . Write queries on left pane, execute it by clicking > button, then the response is on right pane.

f:id:hakopako03:20160711113004p:plain

First of all, set access key (identifying db file)

Set a string at configuration form from right top button. The access key is going to be an actual db file name.

f:id:hakopako03:20160711113023p:plain

Check basic queries and take your next step !!!!!

Create Table
var params = {
    TableName: "tbl_sample",
    KeySchema: [
        {
            AttributeName: "id",
            KeyType: "HASH"
        }
    ],
    AttributeDefinitions: [
        {
            AttributeName: "id",
            AttributeType: "N"
        }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 1, 
        WriteCapacityUnits: 1
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});
Put Item
var params = {
    TableName: "tbl_sample",
    Item: { 
        id: 1,
        name: "apple",
        price: 120,
    },
    ReturnValues: "NONE",
    ReturnConsumedCapacity: "NONE",
    ReturnItemCollectionMetrics: "NONE"
};
docClient.put(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});
Scan
var params = {
    TableName: 'tbl_sample',
    Limit: 10,
    ConsistentRead: false,
    ReturnConsumedCapacity: 'NONE', 
};
dynamodb.scan(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response
});

Conclusion

I'm sure you can try and understand DynamoDBLocal easily because queries sample is prepared in right top menu. But be careful, this isn't guaranteed as much performance as cloud DynamoDB. Thus I recommend you use it at development environment, or use it consistently at all environments regardless of development,staging and product.