AWS DynamoDB事务问题(Python)



我正在尝试以事务方式更新两个表。第一个表被称为CUSTOMER_table,第二个表被称作CUSTOMER_JOB_table。

对于第一个表,如果不存在,我会创建一个新行。如果它确实存在,我会将这个特定进程的值添加到currentProcessedCount列中。对于第二个表,我总是创建一个新行。这两个更新需要是事务性的。我犯了以下错误,我不知道是什么原因。有人能帮我吗?

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

这是一个问题,不确定它是否是唯一的问题。

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

:mlv:ml不匹配

最新更新