简体   繁体   中英

One-to-many in DynamoDB with Java Object Persistence Model

Is possible to create a relationship between two tables using DynamoDB Java Persistence Model?

I have the follow relationship of One Post to Many Comments

@DynamoDBTable(tableName="Post_MyApp")
public class Post {

private String id;
private String title;
private Set <Comment> comments;
//... Getters and setters and dynamo annotations
}

I have a separated table of comments, that Comment is another dynamo table/entity. My idea is create a table of post_comments like in SQL with all the comments of a post. This is the right way to do this with Dynamo or there is another way to do it better?

One post to many comments. You just need the following tables: 1. Post table: post-id (Hash Key), 2. Comment table: post-id (Hash Key), comment-id (Range Key)

To get all the comment for a post, you query the comment table by just giving the HashKey (post-id).

On Java side, you will have 2 classes (Post, Comments) mapped with the Dynamo DB Mapper.

DynamoDBMapper (AWS SDK for Java - 1.7.1) : http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodb/datamodeling/DynamoDBMapper.html

I don't know Java SDK at all but, from a design point of view, may I suggest you a "DynamoDBier" approach ?

Table Post_MyApp

  • article_id as hash_key
  • title
  • ...

Table Comments_MyApp

  • article_id as hash_key
  • datetime as range_key
  • text
  • ...

This allows you to get all comments for an article in a single Query and even to paginate using limit and ExclusiveStartKey

By the way, I found excellent examples In DynamoDb documentation's appendix: http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/SampleTablesAndData.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM