【问题标题】:DynamoDB: scan() with FilterExpression that combines multiple attributesDynamoDB:scan() 和 FilterExpression 组合了多个属性
【发布时间】:2017-12-21 20:01:52
【问题描述】:

我的 DynamoDB 表中的项目具有以下格式:

{
  'id': 1,
  'last_check': 1234556,
  'check_interval': 100,
  ....
}

现在我想扫描表格并查找所有last_check + check_interval 小于某个给定值last_check_time 的项目。我没有找到创建组合两个属性的 FilterExpression 的方法,所以我目前正在按照以下方式进行操作:

last_check_time = time.time()
response = status_table.scan(
    ProjectionExpression='id, last_check, check_interval',
    FilterExpression = Attr('last_check').lt(last_check_time)
)
# manually filter the items and only preserve those with last_check + check_interval < last_check_time:
for item in response['Items']:
    if item['last_check'] + item['check_interval'] < last_check_time:
        # This is one of the items I'm interested in!
        ....
    else:
        # Not interested in this item. And I'd prefer to let DynamoDB filter this out.
        continue

有没有办法让 DynamoDB 进行过滤,从而使上面示例中的 for 循环过时?

【问题讨论】:

    标签: python-3.x amazon-web-services amazon-dynamodb boto3


    【解决方案1】:

    很遗憾,目前无法请求 DynamoDB 为您执行过滤计算,但您可以创建另一个属性,该属性是两个属性的总和,并且您有几种方法可以实现;

    1. 将项目写入 DynamoDB 时,可能会在代码中计算附加属性 (last_check + check_interval)。

    2. 使用 DynamoDB 触发器创建附加属性(last_check + check_interval)

    您可以使用任一选项在要过滤的项目上创建新属性。

    【讨论】:

    • 谢谢!我想我会选择方法 1。
    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多