-1

I have to query a dynamoDB table with boto3 by using the GSI and the PK, my GSI is a date and I need to get the latest date, but i don't know how to query on GSI and on the PK...

My code

if pk_id:
    self.__logger.info(f"DynamoDB : get current object attached to pk {pk_id}")
    item_found = self.table_query(???)
    if item_found is not None and item_found.get('Items'):
        return item_found
    else:
        return {}
mooncrunch
  • 81
  • 1
  • 1
  • 8
  • Does this answer your question? [How do we query on a secondary index of dynamodb using boto3?](https://stackoverflow.com/questions/35758924/how-do-we-query-on-a-secondary-index-of-dynamodb-using-boto3) – luk2302 Dec 21 '22 at 10:22
  • It's a part from the answer, but how to query on the PK and the GSI with only one request ? – mooncrunch Dec 21 '22 at 10:24
  • You don't...... – luk2302 Dec 21 '22 at 10:25
  • We can't ? I don't understand ? – mooncrunch Dec 21 '22 at 10:28
  • I believe you need to understand the concepts in DynamoDB more. A GSI has a partition key, it will allow you to read data based on a different view from your main table. – Leeroy Hannigan Dec 21 '22 at 10:37
  • Please share the structure of your data and the query which you are trying to fulfill. – Leeroy Hannigan Dec 21 '22 at 10:37
  • Yes it's true, it's my first day on dynamo db it's pretty hard to get all the concept :( – mooncrunch Dec 21 '22 at 10:41
  • I suggest that you at least read the DynamoDB documentation before trying to use it. – Leeroy Hannigan Dec 21 '22 at 11:28
  • You should be posting question on SO on your first day with a new tech, you should not be posting question in the first week or even the first month. Any question you can have in that time is answerable by doing research on your own. – luk2302 Dec 21 '22 at 11:47

1 Answers1

0

To Query a GSI it looks something like this:

    response = client.query(
        TableName='table-name',
        IndexName='index-name',
        ExpressionAttributeNames={
            '#pk': 'gsi_pk',
        },
        ExpressionAttributeValues={
            ':pk': pk_id,
        },
        KeyConditionExpression='#pk = :pk'
    )
Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31