【问题标题】:AWS DynamoDB Transaction issues (Python)AWS DynamoDB 事务问题 (Python)
【发布时间】:2020-12-04 07:01:11
【问题描述】:

我正在尝试以事务方式更新 2 个表。第一个表称为 CUSTOMER_TABLE,第二个表称为 CUSTOMER_JOB_TABLE。

对于第一个表,如果它不存在,我会创建一个新行。如果它确实存在,我将这个特定进程的值添加到 currentProcessedCount 列。对于第二个表,我总是创建一个新行。 2 个更新需要是事务性的。我收到以下错误,我无法弄清楚原因是什么。有人可以帮忙吗?

Response:
{
  "errorMessage": "An error occurred (TransactionCanceledException) when calling the TransactWriteItems operation: Transaction cancelled, please refer cancellation reasons for specific reasons [ValidationError, None]",
  "errorType": "TransactionCanceledException",
  "stackTrace": [
    "  File \\"/var/task/app.py\\", line 149, in lambda_handler\\n    c_table_response = update_customer_table(customer_id, customer_monthly_limit, number_of_rows,\\n",
    "  File \\"/var/task/app.py\\", line 226, in update_customer_table\\n    response = dynamodb_client.transact_write_items(\\n",
    "  File \\"/opt/python/botocore/client.py\\", line 316, in _api_call\\n    return self._make_api_call(operation_name, kwargs)\\n",
    "  File \\"/opt/python/botocore/client.py\\", line 635, in _make_api_call\\n    raise error_class(parsed_response, operation_name)\\n"
  ]
}

下面是我的调用方法

import boto3
dynamodb_client = boto3.client('dynamodb')
# grab static env variable
CUSTOMER_ID = os.environ['CUSTOMER_ID']
BUCKET_NAME = os.environ['BUCKET_NAME']
CUSTOMER_TABLE_NAME = os.environ['CUSTOMER_TABLE_NAME']
CUSTOMER_JOB_TABLE_NAME = os.environ['CUSTOMER_JOB_TABLE_NAME']

def update_customer_table(customer_id, customer_monthly_limit, number_of_rows, year_month, uuid, date_time, batch_no):
    response = dynamodb_client.transact_write_items(
        TransactItems=[
            {
                'Update': {
                    'TableName': CUSTOMER_TABLE_NAME,
                    'Key': {
                        'PK': {'S': customer_id},
                        'SK': {'N': str(year_month)},
                    },
                    'ExpressionAttributeNames': {
                        '#ml': "MonthlyLimit",
                        '#cpc': "currentProcessedCount"
                    },
                    'ExpressionAttributeValues': {
                        ':ml': {'N': str(customer_monthly_limit)},
                        ':cpc': {'N': str(number_of_rows)}
                    },
                    'UpdateExpression': "SET #ml = :ml ADD #cpc :cpc"
                }
            },
            {
                'Put': {
                    'TableName': CUSTOMER_JOB_TABLE_NAME,
                    'Item': {
                        'PK': {'S': f'{customer_id}_{uuid}'},
                        'SK': {'N': str(year_month)},
                        'CustomerId': {'S': customer_id},
                        'UUID': {'S': uuid},
                        'StartDateTime': {'N': date_time.strftime('%Y%m%d%H%M')},
                        'NumberOfSplitFiles': {'N': str(batch_no - 1)},
                        'TotalRowCount': {'N': str(number_of_rows)}

                    }
                }
            }
        ]
    )

    return response

【问题讨论】:

    标签: python-3.x aws-lambda transactions amazon-dynamodb boto3


    【解决方案1】:

    这是个问题,不知道是不是只有这个问题。

                   'ExpressionAttributeValues': {
                        ':ml': {'N': str(customer_monthly_limit)},
                        ':cpc': {'N': str(number_of_rows)}
                    },
                    'UpdateExpression': "SET #ml = :mlv ADD #cpc :cpc"
    

    :mlv 不匹配 :ml

    【讨论】:

    • 发现了,修复后还是一样的问题
    猜你喜欢
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 2019-07-20
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多