简体   繁体   English

如何使用 boto3 dynamo DB 查询 GSI 和分区键?

[英]How to query on GSI and partition Key with boto3 dynamo DB?

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...我必须使用 GSI 和 PK 查询带有 boto3 的 dynamoDB 表,我的 GSI 是一个日期,我需要获取最新日期,但我不知道如何在 GSI 和 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 {}

To Query a GSI it looks something like this:要查询 GSI,它看起来像这样:

    response = client.query(
        TableName='table-name',
        IndexName='index-name',
        ExpressionAttributeNames={
            '#pk': 'gsi_pk',
        },
        ExpressionAttributeValues={
            ':pk': pk_id,
        },
        KeyConditionExpression='#pk = :pk'
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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