在 DynamoDB 中使用带有保留字的 ProjectionExpression 和 Boto3
问题描述
我正在使用 boto3 从我的 DynamoDB 数据库中选择数据
I'm selecting data from my DynamoDB database using boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(table_name)
response = table.scan(ProjectionExpression='Id,Name')['Items']
工作正常.现在我还想检索一个(不幸的是)用保留字命名的属性 - 比如说 CONNECTION
.
Works fine. Now I also want to retrieve an attribute that is (unfortunately) named with a reserved word - let's say CONNECTION
.
response = table.scan(ProjectionExpression='Id,Name,Connection')['Items']
我收到类似
调用 Scan 时发生错误 (ValidationException)操作:无效的 ProjectionExpression:属性名称是保留的关键词;保留关键字:连接
An error occurred (ValidationException) when calling the Scan operation: Invalid ProjectionExpression: Attribute name is a reserved keyword; reserved keyword: Connection
我知道在使用过滤器或查询时有一种别名技术,但是对于来自 boto3 的简单投影是否存在这种技术?
I know there's an aliasing technique if using filters or queries, but does this exist for simple projections from boto3?
解决方案
原来这和直接调用DynamoDB API一样很容易解决.
Turns out that this is easily solved the same as when calling the DynamoDB API directly.
我们应该为任何保留字使用别名,然后使用 ExpressionAttributeName 参数/属性.
We should use an alias for any reserved word, and then provide a mapping from the alias back to the 'true' name with the ExpressionAttributeName parameter/property.
response = table.scan(ProjectionExpression = 'Id, Name, #c',
ExpressionAttributeNames = {'#c': 'Connection'})['Items']
相关文章