Can you query by a range in a GSI in DynamoDB - amazon-web-services

Suppose I had a users and images tables in DynamoDB.
users table
userId (hash) name email
images table
imagesId (hash) userId filename
How should I set up the range or GSI if I wanted to get all images for a single userId? Should images table be a composite key with imageId (hash) and userId (range) and search by range (if possible)? or should userId (be a GSI) and query just be userId?

It looks like you could add a GSI on your Images table with the reverse key schema of your table:
hash key - userId
range key - imagesId
filename - additional property
You can then query this GSI with a userId to get all of its associated imagesId.

You cannot query using just a range key in DynamoDB.
Unless you want your query results from the images table to be sorted by userId, there's no point to using it as a range key. If you want to retrieve all of the images with a certain userId from the images table using a query, you need to use a GSI on userId.

Related

Get data based on sort index AWS dynamoDB

I have a DynamoDB table like this:
I want to list all posts irrespective of users, i-e by getting all data whose sort key is "post". How can I achieve this?
I have heard about Global Secondary Index, but couldn't figure out how to use them.
You create a global secondary index with a Key Schema like this:
Partition Key: SK attribute of the base table
Sort Key: PK attribute of the base table
It's called an inverted index. Then you can Query the Global Secondary Index by specifying the IndexName in the Query and search for all items that have "post" as the value for SK.

User ID vs ID for primary key dynamoDB

I have a table in which has a "userId" column (set as a partition key) and a "createdAt" column (set as the sort key) so they form up a composite primary key.
I also need to find the exact row in case I don't have the User ID available, so I made another column "id" and made it as a global secondary index.
In my case, should I make the "id" column the primary key and remove the "userId" as the partition key or will this remove the feature of what "Partitioning" actually does by the DynamoDB?
Similarly, If I need to delete a row from the table, should I send "createdAt" field from the front end to be able to find out the exact row? Does this make sense? Sending the "id" of the row seems more good to me to be able to delete the row.
You probably don't want to put a timestamp in your user primary keys. Why? You'd need to know the exact time the user was created to fetch a user, which is probably not what you want.
Consider using a partition key of USER#<user_id> and a sort key of something predictable, like A or METADATA or USER#<user_id>. This allows you to fetch/delete a user by their ID.
If you have access patterns around fetching users in order of account creation, you can create a GSI with the sort key set to the createdAt attribute.

DynamoDB : How to query multiple partition key ordered by sort key on GSI

I'm currently developing an Instagram clone using dynamoDB and appSync as back-end.
Im currently working on the user's feed page. I need to query all posts by the user and the users he follows ordered by timestamp.
I created a GSI with userId as partition key and timestamp as sort key.
I tried:
"Query" on GSI, but it allow only one partition key
"Scan" on the GSI then filter on userId, but it doesn't order posts by timestamp
"BatchGet", but it doesn't order posts by timestamp, and the result is not flatten
Post Attributes:
{
type:"POST",
id:"post1",
timeStamp:"213213123",
img_url: "https://img.com/img.jpg",
userComment:"nice pic",
userId:"user1",
userName:"kevin",
numberOfComment:"0",
numberOfLike:"0
}
User attribute:
{
type:"USER",
id:"user1",
userName:"Kevin",
FollowerCount:0,
FollowCount:0,
PostCount:1,
bio:"bio"
}
Is there another solution?
edit: my dynamodbTable

DynamoDB: Retrieving the n most recent items for each user

I have a DynamoDB table that store information about images. The hash key is a unique string that identifies each image. There are also two global secondary indicies: username and creation date. Username belongs to the user who created the image.
For each user, I want to be able to show them their 10 most recent images. How can I retrieve items from the table by first identifying images associated with a particular username, then choosing 10 of them by sorting through the creation dates?
In order to do this query, you need a GSI with a hash key of userId and a sort key of creationDate.
You can then do a query for a specific userId, set ScanIndexForward to false, and set Limit to n.

How to structure table in DynamoDB?

I have four fields: payload, receivedOn, topic, uuid.
I have taken date[milliseconds] as primary key [partition key].
I want to write a query that gives me result based on receivedOn field.
I have tried to scan the database but it does not give result in ascending and descending format.
When I use query I have to use date[partition key] and receivedOn both.
As you can see I have to assign value in Partition key, but all my partition key is different. So how should I structure my database so I can query on receivedOn field and can get data in descending order.
Please help.
Thank you in advance.
Answer:
In dynamoDB partition key is combination of two keys : primary key and sortKey. You can sort your data using sortKey. Partition key can be same but sortKey cannot. So I give receivedOn value to sortKey and add the millisecond to it so it will always be unique.