5

I have a table with a primary key and range key set:

user_id
timestamp_joined

Using the boto api layer, is there a way I can retrieve the item just by specifying the user_id (without the range key)? Every time I perform a getItem() query without specifying the range key it fails. I can only get it to work by specifying the range key (timestamp_joined).

The error message when fetching just by user_id is below:

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'} 
4

3 回答 3

3

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

于 2012-09-03T01:05:25.597 回答
0
result = table.scan(Attr('user_id').eq(/number here/),
                    Limit=1,
                    ConsistentRead=True)

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

于 2015-10-14T18:02:11.257 回答
0

这在日期范围内对我有用

我的桌子上有日期: 在此处输入图像描述

我做了这样的查询:

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 个结果。

于 2021-06-29T19:18:18.007 回答