gpt4 book ai didi

python - 如何使用 Python 扫描 dynamoDB 表中的特定键?

转载 作者:行者123 更新时间:2023-11-30 22:19:16 24 4
gpt4 key购买 nike

我正在尝试扫描列名为 S3KeyID 的值,并且正在查找值“newkey”。
当我在测试中运行此命令时,它会扫描 dynamoDB 表中的所有 3 个项目,并找到 3 个结果,但没有匹配项。

import boto3
import json
import decimal
import calendar
import datetime
from boto3.dynamodb.conditions import Key, Attr

def lambda_handler(event, context):

StartDateTime = datetime.datetime.now() - datetime.timedelta(minutes=10000)
EndDateTime = datetime.datetime.now()


# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return str(o)
return super(DecimalEncoder, self).default(o)

dynamodb = boto3.resource('dynamodb')

table = dynamodb.Table('Overwatch')

print("Overwatch old messages")

response = table.scan(
#ExpressionAttributeNames = {'ST' : 'S3KeyID'},

FilterExpression = "S3KeyID = :key",

ExpressionAttributeValues =
{ #":dateStart": {"S": StartDateTime},
#":dateEnd": {"S": EndDateTime},
":key" : {"S" : "newkey"}
# ":S3KeyID : 1222433"
}

#ProjectionExpression="S3KeyID"
)

for i in response[u'Items']:
print(json.dumps(i, cls=DecimalEncoder))
return response

查看结果:

Items": [],
"Count": 0,
"ScannedCount": 3,

最佳答案

我想这样效果更好:

response = table.scan(
FilterExpression = Attr('S3KeyID').eq('newkey')
)

Read the docs了解更多示例。这是我的灵感:

Similarly you can scan the table based on attributes of the items. For example, this scans for all the users whose age is less than 27:

response = table.scan(
FilterExpression=Attr('age').lt(27)
)
items = response['Items']
print(items)

关于python - 如何使用 Python 扫描 dynamoDB 表中的特定键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49179036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com