使用boto3将字符串附加到AWS Dynamo DB表中的现有字符串值

发布于 2025-02-10 04:05:45 字数 1622 浏览 1 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2025-02-17 04:05:45

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”列表,您可能会更好地为您服务

 {"L": [ { "S": "abctest1.sh" },{ "S": "abctest2.sh" }]}}

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

 {"L": [ { "S": "abctest1.sh" },{ "S": "abctest2.sh" }]}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文