我正在尝试以事务方式更新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",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,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}'},'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