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
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
