使用boto3将字符串附加到AWS Dynamo DB表中的现有字符串值
我正在使用boto3来更新AWS Dynamo DB表。
要求是将“ abctest1.sh”附加到键'init_script_name'的现有值。这意味着“ init_script_name”的更新值应为dynamo db表中所有项目的abctest.sh,abctest1.sh'。
"system_configuration": {
"L": [
{
"M": {
"name": {
"S": "Desktop"
},
"configuration": {
"L": [
{
"M": {
"key": {
"S": "DiskSize"
},
"value": {
"S": "RootVolume"
}
}
},
{
"M": {
"key": {
"S": "init_script_name"
},
"value": {
"S": "abctest.sh"
}
}
}
]
}
}
}
]
}
我尝试了以下代码块以更新密钥。但是它用abctest1.sh替换现有的“ abctest.sh”,而不是将现有值附加或串联以新值
updResponse=dyd.update_item(TableName='test1',
Key={
"name":{"S":"Linux"}
},
UpdateExpression="SET #sys_config[0].#config[1].#val= :updVal",
ExpressionAttributeNames={"#sys_config": "system_configuration",
"#config":"configuration",
"#val":"value" },
ExpressionAttributeValues={":updVal": {"S":"abctest1.sh"}}
)
I am using boto3 to update AWS dynamo db table.
Requirement is to append 'abctest1.sh' to existing value for key 'init_script_name'. That means the updated value for 'init_script_name' should be 'abctest.sh,abctest1.sh' for all items in the dynamo db table.
"system_configuration": {
"L": [
{
"M": {
"name": {
"S": "Desktop"
},
"configuration": {
"L": [
{
"M": {
"key": {
"S": "DiskSize"
},
"value": {
"S": "RootVolume"
}
}
},
{
"M": {
"key": {
"S": "init_script_name"
},
"value": {
"S": "abctest.sh"
}
}
}
]
}
}
}
]
}
I tried below code block to update the key. But it is replacing existing 'abctest.sh' with abctest1.sh, instead of appending or concatenating existing value with new value
updResponse=dyd.update_item(TableName='test1',
Key={
"name":{"S":"Linux"}
},
UpdateExpression="SET #sys_config[0].#config[1].#val= :updVal",
ExpressionAttributeNames={"#sys_config": "system_configuration",
"#config":"configuration",
"#val":"value" },
ExpressionAttributeValues={":updVal": {"S":"abctest1.sh"}}
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Dynamo没有类似的Concat字符串功能。您可以增加数字或将项目添加到列表或地图但不能将项目添加到字符串中。仅替换字符串,
请参见: https://docs.aws。 Amazon.com/amazondynamodb/latest/developerguide/expressions.updateexpressions.html
您将需要先获取项目的副本,然后更新该副本并推开更新的副本。即:获取物品,将其存储为一个。更新您需要使用新值 +旧值更新的密钥,然后使用此新的conteded键进行更新。
如果您必须做一些以上的事情,那么您肯定想研究批处理写作: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/guide/dynamodb.html#batch-writing
lot,通过将其从“ S”字符串更改为“ L”列表,您可能会更好地为您服务
Dynamo doesn't have a concat string functionality like this. You can increment numbers or add items to a list or map but not a string. Strings are replace only
see: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
You will need to get a copy of the item first then update that copy and push the updated copy. Ie: get the item, store as a dict. Update the key you need to update with the new value + old value, then UpdateItem with this new concatted key.
If you have to do more than a few you definitely want to look into the batch writing: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html#batch-writing
If this is a task you will be performing a lot, you might be better served by changing that from a "S" string to a "L" list