How AWS DynamoDB query result is a list of items while partition key is a unique value - amazon-web-services

I'm new to AWS DynamoDB and wanted to clarify something.
As I learned when we use query we should use the partition key which is unique among the list of items, then how the result from a query is a list!!! it should be one row!! and why do we even need adding more condition?
I think I am missing something here, can someone please help me to understand?
I need this because I want to query on list of applications with specific status value or for specific range of time but if I am supposed to provide the appId what is the point of the condition?
Thank you in advance.

Often your table will have sort key, which together with your partition key will create composite primary key. In this scenario a query can return multiple items. To return only one value, not a list, you can use get_item instead if you know unique value of the composite primary key.

Related

Is it possible to run a greater than query in AWS DynamoDB?

Let's say I have my DynamoDB table like this, with Order ID as the primary key. :
The Order ID increments by one, everytime I add/put a new item.
Now, I have one number, let's say 1000, and my user wants to get all the items which have Order ID > 1000.
So the items returned would be 1001, 1002, 1003, and so on till the last one.
My requirement is as simple as it seems - but is this thing possible to do with Query method of AWS DynamoDB?
Any help is appreciated :)
Thanks!
There's currently no way to filter on partition key, but I can suggest a way that you can achieve what you want.
You're heading in the right direction with Query which has a "greater than" operator. However, it only operates on the sort key attribute.
With Query, you essentially choose a single partition key, and provide a filter expression that is applied to the sort key of items within that partition.
Since your partition key is currently "Order ID?", you'll need to add a Global Secondary Index to query the way you want.
Without knowing more about your access patterns, I'd suggest you add a Global Secondary Index using "From" as the partition key, which I assume is the user ID. You can then use "Order ID" as the sort key.
my user wants to get all the items which have Order ID > 1000.
With the GSI in place, you can achieve this by doing a query for items where "User ID" is userId and "Order ID" > orderId.
You can find more on query here, details on adding a GSI here, and more info on choosing a partition key here.
No, because Query expects an exact key, and does not allow an expression for the partition key (it does however for the sort key).
What you could use however is a Scan with a FilterExpressions (see Filter Expressions for Scan
and Condition Expressions for the syntax). This reads all records and filters afterwards, so it is not the most effective way.

How to run a 'greater than' query in Amazon DynamoDB?

I have a primary key in the table as 'OrderID', and it's numerical which increments for every new item. An example table would look like -
Let's assume that I want to get all orders above the OrderID '1002'. How would I do that?
Is there any possibility of doing this with DynamoDB Query?
Any help is appreciated :)
Thanks!
Unfortunately with this base table you cannot perform a query with a greater than for the partition key.
You have 3 choices:
Migrate to using scan, this will use up your read credits significantly.
Creating a secondary index, you'd want a global secondary index with the sort key becoming your order id. Take a look here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.OnlineOps.html#GSI.OnlineOps.Creating.
Loop over in the application performing a Query or GetItem request from intial value until there are no results left (very inefficient).
The best practice would be to use the GSI if you can as this will be the most performant.

AWS DynamoDB. Querying all hashes IN array

I know BatchGetItems allows for retrieval of multiple hash keys. To save on the read capacity, I like to know if Query provide same functionality via some "IN" keyword I can use? ie, all primary keys will be inserted into an array for Query to search "IN" in the array.
Query doesn't provide what you want. As per the documentation here:
KeyConditionExpression: The condition must perform an equality test on a single partition key value. The condition can also perform one of several comparison tests on a single sort key value. Query can use KeyConditionExpression to retrieve one item with a given partition key value and sort key value, or several items that have the same partition key value but different sort key values.
BatchGetItem is the only option that you have.

How to perform a range query over AWS dynamoDB

I have a AWS DynamoDB table storing books information, the hash key is book id. There is an attribute for book price.
Now I want to perform a query to return all the books whose price is lower than a certain value. How to do this efficiently, without scanning the whole table?
The query on secondary-index seems only could return a set of entries with the index being a certain value, so I am confused about how to perform a range query efficiently. Thank you very much!
There are two things that maybe you are confusing. The range key with a range on an attribute.
To clarify, in this case you would need a secondary index and when querying the index you would specify a key condition (assuming java and assuming secondary index on value - this in pretty much any sdk supported language)
see http://docs.amazonaws.cn/en_us/AWSJavaSDK/latest/javadoc/index.html?com/amazonaws/services/dynamodbv2/model/QueryRequest.html w/ a BETWEEN condition.
You can't do query of that kind. DynamoDB is sharded across many nodes by hash key, so doing a query without hash key (on all hash keys) is essentially a full scan.
A hack for your case would be to have a hash key with only one value for the whole table, but this is fundamentally wrong because you loose all the pros of using DynamoDB. See hot hash key issue for more info: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForTables.html

Get first N items sorted by a key in DynamoDB

I thought this would be easy but I can't figure it out.
I have a DynamoDB table where all the items have the same attributes. One of the attributes is a numeric one named ytd. I simply want the first 5 items sorted by ytd.
you cant do it in a simple way.
dynamo db return ordered results of the same hash key
so if your hash key here is X, and range key will be 'ytd', then in order to get 'ytd' ordered items then X should be the same.
i dont know your exactly flow, but if you are not query X (you just need to get ordered 'ytd' no matter for X), then you can add a global secondary partition with hash key=partition, range key=ytd as described here:
How to choose a partition key in DynamoDB for a chat app