gpt4 book ai didi

python - 如何使用 Boto3 在 Dynamodb 上将项目添加到 string_set

转载 作者:搜寻专家 更新时间:2023-10-30 19:42:43 25 4
gpt4 key购买 nike

我已经阅读了官方 AWS 文档和几个论坛,但在使用 Python/Boto3 和 Dynamodb 将项目添加到 string_set 时,我仍然找不到我做错了什么。这是我的代码:

table.update_item(
Key={
ATT_USER_USERID: event[ATT_USER_USERID]
},
UpdateExpression="add " + key + " :val0" ,
ExpressionAttributeValues = {":val0" : set(["example_item"]) },
)

我得到的错误是:

An error occurred (ValidationException) when calling the UpdateItem operation: An operand in the update expression has an incorrect data type\"

最佳答案

看起来你自己想出了一个方法,但对于其他来这里寻找答案的人来说:

  1. 您的“Key”语法需要一种数据类型(如“S”或“N”)
  2. 您需要在 ExpressionAttributeValues 中使用“SS”作为数据类型,并且
  3. 您不需要在 ExpressionAttributeValues 中进行“设置”。

这是我刚刚运行的一个例子(我有一个现有的集合,test_set,有 4 个现有值,我添加了第 5 个,字符串“五”):

import boto3
db = boto3.client("dynamodb")
db.update_item(TableName=TABLE,
Key={'id':{'S':'test_id'}},
UpdateExpression="ADD test_set :element",
ExpressionAttributeValues={":element":{"SS":['five']}})

所以之前,字符串集看起来像 ['one','two','three','four'],之后,它看起来像 ['one', '二','三','四','五']

关于python - 如何使用 Boto3 在 Dynamodb 上将项目添加到 string_set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44377713/

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