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

Time:10-05

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

CodePudding user response:

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 (i.e. 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

  • Related