1
votes

I'm coming from a strong relational database background and am having trouble understanding hash and range and secondary indexing in DynamoDB.

I'm looking to store notifications for users.

In SQL, the columns would be :

  • NotificationID (PK)
  • UserID (FK)
  • Message
  • DateCreated
  • DateViewed

I need help setting up the proper hash and range keys so this works properly in the following tasks

  • querying or scanning to find the latest notifications for a LIST of userIDs. This list will be dictated by which users are actually logged in at the moment. Equivalent to "WHERE userID IN (1,2,3)" in SQL
  • get count of notifications where dateViewed = '1/1/1900'
  • update specific notification
  • get all notifications for a particular user

Any help on structuring this table correctly is appreciated

1

1 Answers

0
votes

There are many ways to model this - here is one option you might want to consider:

  1. Create a table (called notifications) with hash primary key (notificationId - uuid) with other fields you described in the item.

Now, you can update item the base table using UpdateItem API. See: Java SDK layer: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaSDKHighLevel.html

2.Create a Global secondary indexes (GSI) on the notifications table with the following schema: - hash (userId) + range (notificationId) - this allows you to issue a Query API on secondary index "Get me all the notifications for this user" and you can filter by timestamp as well. See examples here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html . - For find latest notification across list of userIds: you can issue multiple Query API calls in parallel (in different threads) as DynamoDB.

There are several variations on data model (such as creating another GSI with hash (userId) and range (timestamp) - to efficiently sort notifications based on timestamp for a given user).

Hope this helps.