Putting/Updating DynamoDB 中的项目因 UpdateExpression 语法而失败
Putting/Updating item in DynamoDB fails for the UpdateExpression syntax
我创建了一个 DynamoDB table,在我的 Python 代码中,我将资源初始化如下:
self.dynamodb = self.session.resource('dynamodb').Table('aws-ci')
table 只有一个 index/key,名称为 environment
。我正在尝试将PUT
放入其中,一个对象如下:
{
"environment": "beta",
"X": {
"current_sha": "sha1",
"deployed": true,
"prev_sha": "sha2",
"status": "inactive"
},
"Y-Z": {
"current_sha": "sha1",
"deployed": false,
"prev_sha": "sha2",
"status": "active"
}
}
其中,X
和Y-Z
是微服务的名称。我的插入代码如下:
def put_service_data(self, environment, service_name, service_data, status = None):
get_previous = self.dynamodb.get_item(
Key = {
'environment': environment
}
).get(service_name)
service_data['prev'] = get_previous and get_previous.get('current_sha') or 'NULL'
if status == 'rollback' and get_previous:
service_data['current'] = get_previous.get('current_sha')
service_data['prev'] = get_previous.get('prev_sha')
set_query = "SET {0}.current_sha = :current, {0}.prev_sha = :prev, {0}.deployed = :is_deployed, {0}.current_status = :status".format(service_name)
updated = self.dynamodb.update_item(
Key = {
'environment': environment
},
UpdateExpression = set_query,
ExpressionAttributeValues = {
':current': service_data.get('current'),
':prev': service_data.get('prev'),
':status': service_data.get('status'),
':is_deployed': service_data.get('deployed')
},
ReturnValues = "ALL_NEW"
)
return updated
之前,我使用的不是 {0}.current_status
,而是 {0}.status
,但这引发了以下错误:
An error occurred (ValidationException
) when calling the
UpdateItem
operation: Invalid UpdateExpression
: Attribute name is
a reserved keyword; reserved keyword: status
无论如何,我将该属性名称更改为 current_status
并再次尝试插入,只是这次我收到:
An error occurred (ValidationException
) when calling the
UpdateItem
operation: The document path provided in the update
expression is invalid for update
当尝试为服务 X 设置属性时,以及当尝试为 Y-Z 设置属性时:
An error occurred (ValidationException
) when calling the
UpdateItem
operation: Invalid UpdateExpression
: Syntax error;
token: "-", near: "Y-Z"
我目前无法理解 update_item
调用应该如何工作。
set_query = "SET #service_name.current_sha = :current, #service_name.prev_sha = :prev, #service_name.deployed = :is_deployed, #service_name.current_status = :current_status"
updated = self.dynamodb.update_item(
Key = {
'environment': environment
},
UpdateExpression = set_query,
ExpressionAttributeValues = {
':current': service_data.get('current'),
':prev': service_data.get('prev'),
':current_status': service_data.get('status'),
':is_deployed': service_data.get('deployed')
},
ExpressionAttributeNames = {
'#service_name': service_name,
},
ReturnValues = "ALL_NEW"
)
我创建了一个 DynamoDB table,在我的 Python 代码中,我将资源初始化如下:
self.dynamodb = self.session.resource('dynamodb').Table('aws-ci')
table 只有一个 index/key,名称为 environment
。我正在尝试将PUT
放入其中,一个对象如下:
{
"environment": "beta",
"X": {
"current_sha": "sha1",
"deployed": true,
"prev_sha": "sha2",
"status": "inactive"
},
"Y-Z": {
"current_sha": "sha1",
"deployed": false,
"prev_sha": "sha2",
"status": "active"
}
}
其中,X
和Y-Z
是微服务的名称。我的插入代码如下:
def put_service_data(self, environment, service_name, service_data, status = None):
get_previous = self.dynamodb.get_item(
Key = {
'environment': environment
}
).get(service_name)
service_data['prev'] = get_previous and get_previous.get('current_sha') or 'NULL'
if status == 'rollback' and get_previous:
service_data['current'] = get_previous.get('current_sha')
service_data['prev'] = get_previous.get('prev_sha')
set_query = "SET {0}.current_sha = :current, {0}.prev_sha = :prev, {0}.deployed = :is_deployed, {0}.current_status = :status".format(service_name)
updated = self.dynamodb.update_item(
Key = {
'environment': environment
},
UpdateExpression = set_query,
ExpressionAttributeValues = {
':current': service_data.get('current'),
':prev': service_data.get('prev'),
':status': service_data.get('status'),
':is_deployed': service_data.get('deployed')
},
ReturnValues = "ALL_NEW"
)
return updated
之前,我使用的不是 {0}.current_status
,而是 {0}.status
,但这引发了以下错误:
An error occurred (
ValidationException
) when calling theUpdateItem
operation: InvalidUpdateExpression
: Attribute name is a reserved keyword; reserved keyword:status
无论如何,我将该属性名称更改为 current_status
并再次尝试插入,只是这次我收到:
An error occurred (
ValidationException
) when calling theUpdateItem
operation: The document path provided in the update expression is invalid for update
当尝试为服务 X 设置属性时,以及当尝试为 Y-Z 设置属性时:
An error occurred (
ValidationException
) when calling theUpdateItem
operation: InvalidUpdateExpression
: Syntax error; token: "-", near: "Y-Z"
我目前无法理解 update_item
调用应该如何工作。
set_query = "SET #service_name.current_sha = :current, #service_name.prev_sha = :prev, #service_name.deployed = :is_deployed, #service_name.current_status = :current_status"
updated = self.dynamodb.update_item(
Key = {
'environment': environment
},
UpdateExpression = set_query,
ExpressionAttributeValues = {
':current': service_data.get('current'),
':prev': service_data.get('prev'),
':current_status': service_data.get('status'),
':is_deployed': service_data.get('deployed')
},
ExpressionAttributeNames = {
'#service_name': service_name,
},
ReturnValues = "ALL_NEW"
)