使用 Boto 将值插入具有整数属性的 DynamoDB table
Insert a value into a DynamoDB table with integer attributes using Boto
我正在使用名为 DMS 的 DynamoDB table,我会在其中定期更新一些值。因此,我首先将一个元素的所有值保存在一个名为 "response" 的变量中。在我的示例中,该元素的主键为 220。在此之后,我将属性的值(在我的例子中是标题为 522 的元素)保存在变量 CounterOnePlus 中并添加 1。在此之后,我尝试更新列 522 中元素的给定属性。这是我的代码:
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('DMS')
AverageValue=220
DeltaValue=522
response = table.get_item(
Key={
'Average': '%d' % AverageValue,
}
)
item = response['Item']
CounterOnePlus=int(item['%d' % DeltaValue])+1
print(CounterOnePlus)
table.update_item(
Key={
'Average': '%d' % AverageValue,
},
UpdateExpression='SET 522 = :val1',
ExpressionAttributeValues={
':val1': CounterOnePlus
}
)
我面临的问题是,我无法保存这些值。原因是标题的名称,即 522。每当我将值插入到带有字符串 Header(如 [=32=)的列时,该值就会更新。使用 str(522) 将值 522 更改为字符串不会更改任何内容。我的错误信息如下:
2
Traceback (most recent call last):
File "TestBoto3.py", line 20, in <module>
':val1': CounterOnePlus
File "/home/ubuntu/.local/lib/python3.6/site-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "/home/ubuntu/.local/lib/python3.6/site-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/botocore/client.py", line 661, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: "522", near: "SET 522 ="
对我的问题有什么想法吗?谢谢
使用 ExpressionAttributeNames (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html):
table.update_item(
Key={
'Average': '%d' % AverageValue,
},
UpdateExpression='SET #DeltaValue = :val1',
ExpressionAttributeValues={
':val1': CounterOnePlus
},
ExpressionAttributeNames= {
"#DeltaValue": str(DeltaValue) # 522
}
)
我正在使用名为 DMS 的 DynamoDB table,我会在其中定期更新一些值。因此,我首先将一个元素的所有值保存在一个名为 "response" 的变量中。在我的示例中,该元素的主键为 220。在此之后,我将属性的值(在我的例子中是标题为 522 的元素)保存在变量 CounterOnePlus 中并添加 1。在此之后,我尝试更新列 522 中元素的给定属性。这是我的代码:
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('DMS')
AverageValue=220
DeltaValue=522
response = table.get_item(
Key={
'Average': '%d' % AverageValue,
}
)
item = response['Item']
CounterOnePlus=int(item['%d' % DeltaValue])+1
print(CounterOnePlus)
table.update_item(
Key={
'Average': '%d' % AverageValue,
},
UpdateExpression='SET 522 = :val1',
ExpressionAttributeValues={
':val1': CounterOnePlus
}
)
我面临的问题是,我无法保存这些值。原因是标题的名称,即 522。每当我将值插入到带有字符串 Header(如 [=32=)的列时,该值就会更新。使用 str(522) 将值 522 更改为字符串不会更改任何内容。我的错误信息如下:
2
Traceback (most recent call last):
File "TestBoto3.py", line 20, in <module>
':val1': CounterOnePlus
File "/home/ubuntu/.local/lib/python3.6/site-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "/home/ubuntu/.local/lib/python3.6/site-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/botocore/client.py", line 661, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: "522", near: "SET 522 ="
对我的问题有什么想法吗?谢谢
使用 ExpressionAttributeNames (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html):
table.update_item(
Key={
'Average': '%d' % AverageValue,
},
UpdateExpression='SET #DeltaValue = :val1',
ExpressionAttributeValues={
':val1': CounterOnePlus
},
ExpressionAttributeNames= {
"#DeltaValue": str(DeltaValue) # 522
}
)