简体   繁体   English

AWS DynamoDB 使用 Python/Boto3/Lamba 更新整数计数器

[英]AWS DynamoDB update integer counter with Python/Boto3/Lamba

I have a DynamoDB table with a integer value, which needs to be incremented using Python/Boto3/AWS-Lambda environment.我有一个带有整数值的 DynamoDB 表,需要使用 Python/Boto3/AWS-Lambda 环境对其进行递增。

userMetrics
{
  "userid": 1234567890,
  "likemetrics": 0,
  "lasttimestamp": 1604553995
}

I am trying to increment the likemetrics integer counter using atomic increment method dynamodb: how to increment a value in map , but fails during testing.我正在尝试使用原子增量方法dynamodb: how to increment a value in map来增加 likemetrics 整数计数器,但在测试过程中失败。

   responseupd = dydb_usertable.update_item(
        Key = {
            'userid': 1234567890
        },
        ExpressionAttributeValues: { ":inc": {N: "1"} },
        UpdateExpression: "ADD likemetrics :inc"
        })

The error thrown by console is控制台抛出的错误是

{
  "errorMessage": "Syntax error in module 'lambda_function': unindent does not match any outer indentation level (lambda_function.py, line 34)",
  "errorType": "Runtime.UserCodeSyntaxError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\" Line 34\n           responseupd = dydb_usertable.update_item(\n"
  ]
}

Please carefuly look at the documentation here :请仔细查看此处的文档:

You are using mixed, Javascript and Python syntax.您正在使用混合的 Javascript 和 Python 语法。 It should be corrected as below:应按如下方式更正:

dydb_usertable.update_item(
    Key = {
        'userid': {
            'N': 1234567890
        }
    },
    ExpressionAttributeValues = { ":inc": {"N": "1"} },
    UpdateExpression = "ADD likemetrics :inc"
})

See updated syntax:查看更新的语法:

dydb_usertable.update_item(
        Key={
            'userid': 1234567890
        },
        UpdateExpression="ADD likemetrics :inc",
        ExpressionAttributeValues={
            ':inc': 1
        },
        ReturnValues="UPDATED_NEW"
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM