Using a ProjectionExpression with reserved words with Boto3 in DynamoDB(在 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']
这篇关于在 DynamoDB 中使用带有保留字的 ProjectionExpression 和 Boto3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 DynamoDB 中使用带有保留字的 ProjectionExpression 和 Boto3
基础教程推荐
- 在 Python 中将货币解析为数字 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
