简体   繁体   中英

Python - Retrieving max primary key value from DynamoDB using Boto3

I'm simply trying to retrieve the max value of my primary keys with a query/scan using Boto3. I'm trying to accomplish this so my program can simply increment a variable I'll set equal to the max "ID" value by 1 for the next table entry.

Screenshot of DynamoDB Table

response = table.scan(
    FilterExpression=Attr('ID'). #Here is where I'd assume a condition
)                                #is placed to find the highest value but
                                 #the Boto3 doc doesn't seem to have a 
                                 #method like 'max()' or something...

Searching around I found how to do this using the original 'boto' so I'm sure there is a way using its newer version 'boto3' but I haven't found anything yet.

Any help/guidance would be greatly appreciated! Thanks!

Boto3 Doc: http://boto3.readthedocs.io/en/latest/guide/dynamodb.html#querying-and-scanning

I am suffering from the same issue and can't find an elegant solution. However if your primary keys are just integers you can do something like this to find the max key:

import boto3

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM