return item with maximum sort-key in dynamodb

拜拜、爱过 提交于 2020-01-22 23:06:11

问题


I'm using a python script to access a dynamodb database in AWS.

I have a table with a hash key and sort key.

For a given hash key, I want to find the item with the largest sort key that is less than a certain value. How can I do that?

Alternatively, is there a way to find the previous item from a given key?

I am not trying to find the item with the largest attribute value (an expensive task in dynamodb), I want the largest key value.


回答1:


I found the answer,

import boto3
import botocore
from boto3.dynamodb.conditions import Key, Attr


dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(table_name)

response = table.query(
              Limit = 1,
              ScanIndexForward = False,
              KeyConditionExpression=Key('device').eq(device) & Key('epoch').lte(threshold)
           )

Where:

  • 'device' is my hash key
  • 'epoch' is my sort key
  • threshold is the value I want to search below


来源:https://stackoverflow.com/questions/42520701/return-item-with-maximum-sort-key-in-dynamodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!