如何重命名 DynamoDB column/key
How to rename DynamoDB column/key
在我的一个 DynamoDb table 中,我有一个名为 "status" 的 column/key,它原来是一个保留关键字。不幸的是,不能选择删除整个 table 并重新启动它。如何重命名密钥?
这是导致异常的 Lambda 代码:
try :
response = table.query(
IndexName='myId-index',
KeyConditionExpression=Key('myId').eq(someId)
)
for item in response['Items']:
print('Updating Item: ' + item['id'])
table.update_item(
Key={
'id': item['id']
},
UpdateExpression='SET myFirstKey = :val1, mySecondKey = :val2, myThirdKey = :val3, myFourthKey = :val4, myFifthKey = :val5, status = :val6',
ExpressionAttributeValues={
':val1': someValue1,
':val2': someValue2,
':val3': someValue3,
':val4': someValue4,
':val5': someValue5,
':val6': someValue6
}
)
except Exception, e:
print ('ok error: %s' % e)
这里是例外:
2016-06-14 18:47:24 UTC+2
ok error: An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: status
没有真正简单的方法来重命名列。您必须为每个条目创建一个新属性,然后删除现有属性的所有值。
没有理由放弃您的 attribute/column,如果您在查询 table 时遇到问题,请使用 表达式属性名称 .
来自 Expression Attribute Names 文档:
On some occasions, you might need to write an expression containing an attribute name that conflicts with a DynamoDB reserved word... To work around this, you can define an expression attribute name. An expression attribute name is a placeholder that you use in the expression, as an alternative to the actual attribute name.
有一个简单的解决方案而不是重命名列:在查询中使用 projection-expression 和 expression-attribute-names。
我 运行 解决了同样的问题(我的 table 包含列 "name"。这是一个示例查询:
TableName: 'xxxxxxxxxx',
ExpressionAttributeNames: {
'#A': 'country',
'#B': 'postcode',
'#C': 'name'
},
ExpressionAttributeValues: {
':a': {S: 'DE'},
':c': {S: 'Peter Benz'}
},
FilterExpression: 'country = :a AND #C = :c',
ProjectionExpression: '#A, #B, #C'
使用 AWS 支持的 NoSQL Workbench 应用程序,我导出了 table 的副本,其中包含我想要更改的列名。我打开了 NoSQL Workbench 创建的 JSON 文件,然后为相关列的名称做了一个简单的 Find/Replace。
由于 .JSON 文件中的名称看起来正确,我使用 NoSQL 应用程序将 table 重新导入回 dynamodb。导入会覆盖现有数据,这将清除错误的列名。
如果你有一个巨大的数据集,下载一个副本到你的本地计算机可能不是一个好的解决方案,但对于我的小 table 它工作得很好。
在我的一个 DynamoDb table 中,我有一个名为 "status" 的 column/key,它原来是一个保留关键字。不幸的是,不能选择删除整个 table 并重新启动它。如何重命名密钥?
这是导致异常的 Lambda 代码:
try :
response = table.query(
IndexName='myId-index',
KeyConditionExpression=Key('myId').eq(someId)
)
for item in response['Items']:
print('Updating Item: ' + item['id'])
table.update_item(
Key={
'id': item['id']
},
UpdateExpression='SET myFirstKey = :val1, mySecondKey = :val2, myThirdKey = :val3, myFourthKey = :val4, myFifthKey = :val5, status = :val6',
ExpressionAttributeValues={
':val1': someValue1,
':val2': someValue2,
':val3': someValue3,
':val4': someValue4,
':val5': someValue5,
':val6': someValue6
}
)
except Exception, e:
print ('ok error: %s' % e)
这里是例外:
2016-06-14 18:47:24 UTC+2 ok error: An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: status
没有真正简单的方法来重命名列。您必须为每个条目创建一个新属性,然后删除现有属性的所有值。
没有理由放弃您的 attribute/column,如果您在查询 table 时遇到问题,请使用 表达式属性名称 .
来自 Expression Attribute Names 文档:
On some occasions, you might need to write an expression containing an attribute name that conflicts with a DynamoDB reserved word... To work around this, you can define an expression attribute name. An expression attribute name is a placeholder that you use in the expression, as an alternative to the actual attribute name.
有一个简单的解决方案而不是重命名列:在查询中使用 projection-expression 和 expression-attribute-names。
我 运行 解决了同样的问题(我的 table 包含列 "name"。这是一个示例查询:
TableName: 'xxxxxxxxxx',
ExpressionAttributeNames: {
'#A': 'country',
'#B': 'postcode',
'#C': 'name'
},
ExpressionAttributeValues: {
':a': {S: 'DE'},
':c': {S: 'Peter Benz'}
},
FilterExpression: 'country = :a AND #C = :c',
ProjectionExpression: '#A, #B, #C'
使用 AWS 支持的 NoSQL Workbench 应用程序,我导出了 table 的副本,其中包含我想要更改的列名。我打开了 NoSQL Workbench 创建的 JSON 文件,然后为相关列的名称做了一个简单的 Find/Replace。
由于 .JSON 文件中的名称看起来正确,我使用 NoSQL 应用程序将 table 重新导入回 dynamodb。导入会覆盖现有数据,这将清除错误的列名。
如果你有一个巨大的数据集,下载一个副本到你的本地计算机可能不是一个好的解决方案,但对于我的小 table 它工作得很好。