amazon-web-servicesamazon-dynamodbdynamodb-queries

Use DynamoDB GSI with Primary Partition Key


There is an AWS DynamoDB table with partition key and sort key. Same table has global index which consist with partition key and sort key. Anyway to use combination of GSI and primary partition key together?

I tried with combination using KeyConditionExpression using python. But it does thorw error saying invalid column


Solution

  • This was answered here. Please check this https://stackoverflow.com/a/78399861/23887016

    Just you need to create KeyConditionExpression with two expression. One with partition key and other with sort key.

    This video helps step by step to find the answers and all DynamoDB related queries ( insert, delete, query using GSI, query using Local index etc) https://youtu.be/x8IxY4zoBGI

    The python code is like below. The customer_id is the partition key and order_id is the sort key.

    def query_by_partition_key_and_sort_key(customer_value, order_value):
        response = {}
        filtering_exp = Key('customer_id').eq(customer_value)
        filtering_exp2 = Key('order_id').eq(order_value)
        response = demo_table.query(
            KeyConditionExpression=filtering_exp and filtering_exp2)
        
        for item in response["Items"]:
            print(f'Item: {item}')