仅通过指定索引键从DynamoDB表中检索具有范围键的项

时间:2012-09-02 23:47:37

标签: python boto amazon-dynamodb

我有一个主键和范围键设置的表:

user_id
timestamp_joined

使用boto api图层,有没有办法只通过指定user_id(没有范围键)来检索项目?每次执行getItem()查询而不指定范围键时,它都会失败。我只能通过指定范围键(timestamp_joined)来使其工作。

仅通过user_id提取时的错误消息如下:

boto.exception.DynamoDBResponseError: DynamoDBResponseError: 400 Bad Request 
{'message': 'One or more parameter values were invalid: The provided 
key size does ot match with that of the schema', '__type':
'com.amazon.coral.validate#ValidationException'} 

3 个答案:

答案 0 :(得分:3)

听起来你想要执行查询。您可以查询仅指定user_id(散列键)的表,并且查询将返回与该user_id匹配的所有项目,而不管范围值如何。

答案 1 :(得分:0)

result = table.scan(Attr('user_id').eq(/number here/),
                    Limit=1,
                    ConsistentRead=True)

如果每个user_id有超过1个项目,则可以将结果限制为每个扫描或查询最多1M个结果。

答案 2 :(得分:0)

这在日期范围内对我有用

我的桌子上有日期: enter image description here

我做了一个这样的查询:

import requests, json 
import boto3
from boto3.dynamodb.conditions import Key, Attr

def lambda_handler(event, context):
    dynamodb = boto3.resource('dynamodb', region_name='us-east-1', aws_access_key_id='your_access',aws_secret_access_key='your_secret')
    table = dynamodb.Table('Table_name')

    date_i = '2013-11-18'
    date_f = '2013-11-19'

    try:
        fe = Key('Date').between(date_i,date_f);
        response = table.scan(
            FilterExpression=fe
        )
        print(response['Items'])
    except Exception as e:
        print(e)
        raise e

我在该范围内有 2 个结果。

相关问题