代码之家  ›  专栏  ›  技术社区  ›  Roshi

将DynamoDB GSI与主分区密钥一起使用

  •  3
  • Roshi  · 技术社区  · 2 年前

    有一个AWS DynamoDB表,其中包含分区键和排序键。 同一个表具有全局索引,该索引由分区键和排序键组成。无论如何,要将GSI和主分区密钥组合在一起使用吗?

    我尝试了组合使用 KeyConditionExpression 使用python。但它确实会在说无效列时出错

    1 回复  |  直到 2 年前
        1
  •  3
  •   Jonny packer    2 年前

    这里回答了这个问题。请检查一下 https://stackoverflow.com/a/78399861/23887016

    只需要创建具有两个表达式的KeyConditionExpression。一个带有分区键,另一个带有排序键。

    此视频有助于逐步找到答案和所有与DynamoDB相关的查询(插入、删除、使用GSI查询、使用本地索引查询等) https://youtu.be/x8IxY4zoBGI

    python代码如下所示。customer_id是分区键,order_id是排序键。

    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}')
        
    
    推荐文章