In my test project, I'm trying to simulate a small social network that allows the users to post a personal status message.
I would to use DynamoDB for status message and MySQL for the user friendships.
My doubt is: how can I design my dynamo table to store status message and fetch only the status of friends users?
For example, I need to fetch all the statuses of my friends ordered by creation date from Dynamo. I've thought of a table with this indexes:
- HashKey : user_id
- RangeKey: creation_date
- Attribute1: message
- Attribute2: ....
But, can I filter only the statuses of my friends on this table?
The question is, a user can have a list of friends. Given a user, we want to get all his/her friends (name + online/offline status). How do we do this in DynamoDB?
First for each user you need to assign a user_id. That will be the unique identifier for that user, in other words, a pointer.
You can use a Hash-Range table to store the friendship data. Both the hash key and range key of this table will be user_id. In DynamoDB each hash key can have multiple range keys. The hash key will be one user and we can store all her friends's user id in the range key.
assuming user_1 is friend with user_2 and user_3
"Friendship table in DynamoDB"
HashKey RangeKey
user_1 user_2
user_1 user_3
user_2 user_1
user_3 user_1
You can have another table that stores User information
"User table in DynamoDB"
HashKey name isLoggedin
user_1 J true
user_2 swaggyp false
user_3 furion false
Now you want to get user_1's friend. You can do a DynamoDB query with hash_key equals to user_1. In the above example, you will get 2 rows:
(user_1, user_2) (uiser_1, user_3).
Now you know that user_2 and user_3 are user_1's friends. You can use User the table to get user_2 and user_3's online status.
Lets say your MySql returns user's friend's Ids:
eg. 1,2,3
now you will call dynamoDB table and fetch all items with the help of Batchgetitem* by passing arrays of id's you get from SQl
DynamoDB Schema :
hash :user_id
range:Date and time
other attribute
By default DynamoDB will sort on range field (you can make it ascending/descending)so you will get all the data of the keys that you have asked.
*you can refer batch get items from http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html
Hope that helps.