有一个带有分区键和排序键的AWS DynamoDB表。 同一张表有全局索引,由分区键和排序键组成。无论如何要结合使用 GSI 和主分区键吗?
我尝试使用Python使用
KeyConditionExpression
进行组合。但它确实抛出错误,指出无效列
这个问题在这里得到了解答。请检查此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}')