Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB + Store Values as Items or JSON

New to DynamoDB.

I'm creating a table with Primary Key 'UserID', Composite Key 'DateTime' and then I have the following as a value (note: I don't need to query any specifics in the below data - just write and read it):

UserID1
UserID2
Message
DateTime

Questions:

  1. is there any advantage in storing these 4 values as separate items or as one JSON string?
  2. UserID1 and Datetime in the stored value also make up the Primary/Composite Key - am I right assuming there is no point in storing these in the data/value as I can access this from the returned Keys when quering?
like image 417
Adam Avatar asked Dec 30 '12 00:12

Adam


People also ask

Does DynamoDB store data in JSON?

You can store a JSON document as an attribute in a DynamoDB table. To do this, use the withJSON method of Item . This method parses the JSON document and maps each element to a native DynamoDB data type.

How does DynamoDB store value?

You can store them as an object in Amazon S3 and then store the object identifier in your DynamoDB item. You can also use the object metadata support in Amazon S3 to provide a link back to the parent item in DynamoDB. Store the primary key value of the item as Amazon S3 metadata of the object in Amazon S3.

What format does DynamoDB store data?

DynamoDB uses JSON only as a transport protocol, not as a storage format. The AWS SDKs use JSON to send data to DynamoDB, and DynamoDB responds with JSON. DynamoDB does not store data persistently in JSON format. For more information about JSON, see Introducing JSON on the JSON.org website.


3 Answers

So your options are:

Hash Key | Range Key  | Attributes
----------------------------------
user id  | utc time   | json data
----------------------------------
user123  | 1357306017 | {UserID1:0, UserID2:0, Message:"", DateTime:0}

or

Hash Key | Range Key  | Attributes
--------------------------------------------------------------
user id  | utc time   | UserID1 | UserID2 | Message | DateTime
--------------------------------------------------------------
user123  | 1357306017 | 0       | 0       | ""      | 0

Both are viable options, and the choice comes down to how you want to read the data, if you have an attribute for each item, then you can request those attributes individually.

We tend to use a hybrid approach based upon our usage patterns. Elements we need to access individually are given their own attributes. Elements that we only ever want to access along with a collection of other elements all get assigned a single attribute and are then stored as a single blob of JSON string or a base64 encoded data.

For part two, indeed, you are right, you don't need to store user id and date time again as part of the attributes because they are the hash and range keys, which are returned when you make a request.

like image 76
sungiant Avatar answered Sep 16 '22 21:09

sungiant


With DynamoMapper you could do this in Java:

@DynamoDBTable(tableName = "myClass")
public class MyClass {

    @DynamoDBHashKey(attributeName = "id")
    private String id;

    @DynamoDBRangeKey(attributeName = "rangeKey")
    private long rangeKey;

    @DynamoDBTypeConvertedJson
    private Content content;

}

And the content class could be:

public class Content {

    @JsonProperty
    private List<Integer> integers = new ArrayList();

    @JsonProperty
    private List<String> strings = new ArrayList();

}
like image 34
Sebastian D'Agostino Avatar answered Sep 17 '22 21:09

Sebastian D'Agostino


DynamoDB now supports json object direct storing. read: http://aws.amazon.com/blogs/aws/dynamodb-update-json-and-more/

like image 30
WaylandZ Avatar answered Sep 18 '22 21:09

WaylandZ