在DynamoDB中将带有保留字的ProjectionExpression与Boto3结合使用 [英] Using a ProjectionExpression with reserved words with Boto3 in DynamoDB

查看:171
本文介绍了在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.

我们应该对任何保留字使用别名,然后使用

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结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆