简体   繁体   中英

Python AWS dynamoDB: how can I query elements with a filter on not primary column?

I use a dynamoDB to stock data for an app.

ACTUALLY: I am loading all the data from my dynamodb (with a .scan() ) then I filter the data in local.

PROBLEM: Loading values takes too much time.

SOLUTION: Load only filtered data. I wasn't able to use query like this one:

# Use the Table resource to query for all songs by artist Arturus Ardvarkian
response = table.query(
  KeyConditionExpression=Key('artist').eq('Arturus Ardvarkian')
)
print(response['Items'])

They always say botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: mail

You can create Local or Global secondary indices in your DynamoDB table. From your point of view it would be like there's different table definition (ie table key), but attached to the same data.

This way you will be able to use queries instead of filtering scan results.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html

Exact choice (LSI vs GSI) depends on your use case, and you can find comparison between the two here:

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryIndexes.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