SCAN on key attribute in DynamoDB - amazon-web-services

I need to execute an in query on the key attribute. Since, query doesn't provide in condition, I am planning to use scan. Will scan on key attribute scan the entire table?

Will SCAN on key attribute scan the entire table?
Yes, see Query and Scan in Amazon DynamoDB:
Scan
A scan operation scans the entire table. You can specify filters to
apply to the results to refine the values returned to you, after the
complete scan. Amazon DynamoDB puts a 1MB limit on the scan (the limit
applies before the results are filtered). A scan can result in no
table data meeting the filter criteria.
Specifically, there is no difference between key and non key attributes as far as the Scan API is concerned, i.e. you simply provide the desired attributes by name, regardless of them being used as an attribute constituting the Primary Key as well or not:
AttributesToGet
Array of Attribute names. If attribute names are not specified then
all attributes will be returned. If some attributes are not found,
they will not appear in the result.

wouldn't batchGetItem work for you?

Related

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.

DynamoDBMapper queryPage working with non sort key as filter parameter

From the dynamoDBMapper docs
Querypage:
Queries a table or secondary index and returns a single page of
matching results. As with the query method, you must specify a
partition key value and a query filter that is applied on the sort key
attribute. However, queryPage will only return the first "page" of
data - that is, the amount of data that will fit within 1 MB.
I've applied query filter on a attribute which is not a sort key. But everything works. How is this possible?
I think that particular statement is misleading in the documentation. You can apply query-filter/filter-expression on any non-key attributes, but you cannot apply filter expression on key attributes :
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html#FilteringResults
You can send your feedback ("Feedback" button at the bottom of the doc page) to Amazon. I think they're pretty responsive for the feedbacks they receive.

Query hash/range key and local secondary index

Is it possible to Query a DynamoDB table using both the hash & range key AND a local secondary index?
I have three attributes I want to compare against in my query. Two are the main hash and range keys and the third is the range key of the local secondary index.
No, but that shouldn't be necessary based on your description of what you are trying to accomplish.
If you are trying to access an object based on the hash and range key (of the main table) as well as an additional attribute, selecting on only the hash and range of the main table (which is required to return a single record by definition) will return that record.
If your concern is that the third attribute may be a value that you want to ignore the entire record you can use a query filter to have that item filtered out by DynamoDB or you can use logic in your application to ignore that object.

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

Dynamo DB batch operations on single table

I've been going through AWS DynamoDB docs and cannot figure out what's the difference between batchGetItem() and Query().
My use case: I have a table which has Id as primary hash key, and attribute values are Name and Marks.
I would like to perform batch query which returns list of names and marks by providing list of Id's which are primary keys.
Should I use batchGetItem() or Query()?
BatchGetItem: Allows to you parallelize "GetItem" requests for languages that don't support parallelism (i.e. javascript). This includes retrieving items from different tables (doesn't support indexes though).
Query: Allows you to page through tables with a Hash-Range schema (where you'll have multiple results associated with a Hash key) and allows you to retrieve items from the indexes on your table. Note you can also add an additional condition on range key in your KeyConditions and add conditions on any non primary key attribute in your QueryFilter.
It seems like that your use case calls for a BatchGetItem request, as you are trying to retrieve items from your base table by way of a Hash key.
Hope that helps!