简体   繁体   中英

DynamoDB How to query a non-key attribute with strong consistency?

I have the following table in DynamoDB:

Table: Person

Attributes: person_id, firstName, lastName, age, birthday

Partition Key: person_id

My problem is: I need to query all the people that are between a certain age, for instance age > 18 AND age < 45.

However, I need strong consistency in my query, therefore, I cannot use a Global Secondary Index.

In this case what would be the best/possible solutions?

What you are asking for is contradictory. If you need to get “all people with ages between 18 and 45”, unless this table is very very small, the set of people could change while you are querying. This is inherently the type of operation that is tolerant of eventual consistency.

I also wonder what you would do with the results.

If the the table is really big and you would have a lot of results for your query, how would you present the data?

If the table is always small then you can simply scan it and apply filters.

And ask yourself, what does it mean for a query or scan to be consistent? The very fact that a query/scan can take multiple round-trips to the server to complete means it won't give you a point in time snapshot of the data. Unless the data is very small or changes infrequently.

I would suggest you to use scan operation since you want to achieve filtering but you are not aware of the partition Key . In java, you can follow the below guide for scanning the table and using filter expressions. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ScanJavaDocumentAPI.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM