使用Boto3从DynamoDB检索最大主键值

使用Boto3从DynamoDB检索最大主键值

本文介绍了Python-使用Boto3从DynamoDB检索最大主键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是尝试使用Boto3通过一次查询/扫描来检索主键的最大值。我正在尝试完成此操作,以便我的程序可以简单地将下一个表条目的变量设置为等于最大 ID的值。

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.

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...

我搜索了如何使用原始的 boto来执行此操作,因此我确定有一种使用其较新版本 boto3的方法,但我没有找到

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:

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

这篇关于Python-使用Boto3从DynamoDB检索最大主键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 14:55