我想在从 DynamoDB 获取项目列表时使用 IN 运算符

I want to use IN operator while getting the list of items from DynamoDB

我在变量 domains_hash 中有一组域,我想从 dynamo table 中获取所有项目,其中的域来自 domains_hash .

table 中的列 domain 是分区键。

到目前为止,我就是这样做的-

from botocore.exceptions import ClientError

domains_hash =('196af8b5fca2fa43e8f328d7bd996eafc2d799c1b90233a84d43a5c8cd52ac97',
 'f4af8a416f59f0829e39006e59a3698a3580d310ae2617dcc418b33a90cc9f01',
 '3bf08cfb1b1a363c04ae2ce1c1177641ffb9ca09bc835faff47c025d5379ae6e',
 '6dd22c3e88531abea2dec8403a2a1d9bc23376470c4bc2b2145fd03deef270bb',
 'a402e656cf14b4794fd3762074ad4689cd086efa7c377ae19d68c4d38cee41f3',
 '6d9d3804f2fa1e876533cb3131b1c49414dfbcd710b4710922426244971145ca',
 'f92c9f9a1799251fec6d397a22271183b01625c279b4af324cb49056a62633f4')


try:

    res = endpoint_users.query(
        ConditionExpression="domain IN :dh",
        ExpressionAttributeValues={':dh': domains_hash},
        ScanIndexForward=False,
    )
except ClientError as e:
    raise DopeException

但是我收到这个错误 -

ParamValidationError: Parameter validation failed:
Unknown parameter in input: "ConditionExpression", must be one of: TableName, IndexName, Select, AttributesToGet, Limit, ConsistentRead, KeyConditions, QueryFilter, ConditionalOperator, ScanIndexForward, ExclusiveStartKey, ReturnConsumedCapacity, ProjectionExpression, FilterExpression, KeyConditionExpression, ExpressionAttributeNames, ExpressionAttributeValues

首先您的查询需要 KeyConditionExpression 而不是 ConditionExpression。但是查询不支持 IN 操作。您的选择是:

  • 进行扫描
  • 运行 匹配单个域 ID 的多个查询
  • 在表达式中使用 OR 请求多个域