使用 KeyConditionExpression 的 boto3 查询
boto3 query using KeyConditionExpression
我无法理解为什么下面对 DynamoDB table 的查询不起作用:
dict_table.query(KeyConditionExpression='norm = :cihan', ExpressionAttributeValues={':cihan': {'S': 'cihan'}})
并抛出此错误:
ClientError: An error occurred (ValidationException) when calling the Query operation: One or more parameter values were invalid: Condition parameter type does not match schema type
而以下工作:
dict_table.query(KeyConditionExpression=Key('norm').eq('cihan'))
norm
是字符串类型的字段。我正在使用 boto3 v 1.4.0 和 following the docs:
In [43]: boto3.__version__
Out[43]: '1.4.0'
谁能看出第一个查询中的错误是什么?
奖金问题:所有令牌有什么用,需要一直更换它们吗?为什么我不能直接说 dict_table.query(KeyConditionExpression='norm = cihan')
请按如下所述更改 ExpressionAttributeValues。
ExpressionAttributeValues={':cihan': 'cihan'}
在当前版本的 boto3 (1.9.128) 中,问题中提到的查询工作正常,除此之外现在没有其他工作,下面提到的查询对我有用:-
dynamo_client.query(
KeyConditionExpression='campaign_id = :201906',
ExpressionAttributeValues={':201906': {'S': '201906'}}
)
加分题...
为了回答您的奖励问题,所有令牌都是抽象发电机层,用于确保与之通信时发出的 http 请求之间的类型一致性。使用标准 http/json 在“2”和 2 等情况下存在歧义。因此,dynamo 强制客户端将值包装在专门定义其类型的对象中。
回到原来的问题...
话虽如此...有点荒谬,boto3 API 并没有为您抽象掉它。虽然我可以理解它有点超出了 boto3 工具的范围。 在名为 dynamof 的图书馆中有希望。使用它,您的查询将如下所示:
from boto3 import client
from dynamof import db as make_db
from dynamof import query
client = client('dynamodb', endpoint_url='http://localstack:4569')
db = make_db(client)
results = db(query(
table_name='dict_table',
conditions=attr('norm').equals('cihan')
))
在其他常用操作(例如创建 table、添加项目、更新、删除和扫描)中发出 dynamo 请求同样简单 API。更多操作和功能正在开发中。
免责声明:我写了 dynamof
我无法理解为什么下面对 DynamoDB table 的查询不起作用:
dict_table.query(KeyConditionExpression='norm = :cihan', ExpressionAttributeValues={':cihan': {'S': 'cihan'}})
并抛出此错误:
ClientError: An error occurred (ValidationException) when calling the Query operation: One or more parameter values were invalid: Condition parameter type does not match schema type
而以下工作:
dict_table.query(KeyConditionExpression=Key('norm').eq('cihan'))
norm
是字符串类型的字段。我正在使用 boto3 v 1.4.0 和 following the docs:
In [43]: boto3.__version__
Out[43]: '1.4.0'
谁能看出第一个查询中的错误是什么?
奖金问题:所有令牌有什么用,需要一直更换它们吗?为什么我不能直接说 dict_table.query(KeyConditionExpression='norm = cihan')
请按如下所述更改 ExpressionAttributeValues。
ExpressionAttributeValues={':cihan': 'cihan'}
在当前版本的 boto3 (1.9.128) 中,问题中提到的查询工作正常,除此之外现在没有其他工作,下面提到的查询对我有用:-
dynamo_client.query(
KeyConditionExpression='campaign_id = :201906',
ExpressionAttributeValues={':201906': {'S': '201906'}}
)
加分题...
为了回答您的奖励问题,所有令牌都是抽象发电机层,用于确保与之通信时发出的 http 请求之间的类型一致性。使用标准 http/json 在“2”和 2 等情况下存在歧义。因此,dynamo 强制客户端将值包装在专门定义其类型的对象中。
回到原来的问题...
话虽如此...有点荒谬,boto3 API 并没有为您抽象掉它。虽然我可以理解它有点超出了 boto3 工具的范围。 在名为 dynamof 的图书馆中有希望。使用它,您的查询将如下所示:
from boto3 import client
from dynamof import db as make_db
from dynamof import query
client = client('dynamodb', endpoint_url='http://localstack:4569')
db = make_db(client)
results = db(query(
table_name='dict_table',
conditions=attr('norm').equals('cihan')
))
在其他常用操作(例如创建 table、添加项目、更新、删除和扫描)中发出 dynamo 请求同样简单 API。更多操作和功能正在开发中。
免责声明:我写了 dynamof