【问题标题】:Python boto3 DynamoDB query for getting data with timerange用于获取具有时间范围的数据的 Python boto3 DynamoDB 查询
【发布时间】:2021-09-12 21:14:58
【问题描述】:

我正在尝试在 Python boto3 中为 dynamoDB 构建一个查询,以获取时间范围之间的数据。我的表排序键是时间戳。

response = table.query(
    KeyConditionExpression='deviceid = :deviceid',
    FilterExpression=Key('timestamp').between(':ts1',':ts2'),
    # ExpressionAttributeNames = {"#t": "timestamp"},
    ExpressionAttributeValues={
        ':deviceid': {'S': 'BSM_G101'},
        ':ts1': "2021-09-12 10:56:26",
        ':ts2': "2021-09-12 10:57:00"
    }
)

查询导致错误:

"botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Value provided in ExpressionAttributeValues unused in expressions: keys: {:ts1, :ts2}"

有人能告诉我当前查询有什么问题并帮我纠正吗?

【问题讨论】:

    标签: python boto3 dynamodb-queries


    【解决方案1】:

    我们可以通过 KeyConditionExpression 使用分区和排序键进行查询

    response = table.query(
        KeyConditionExpression=Key('deviceid').eq('BSM_G101') & Key('timestamp').between("2021-09-12 10:56:26","2021-09-12 10:57:00"),
    )
    print(response['Items'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-21
      • 2016-01-21
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多