2017-10-19 123 views
0

我读官方的文档http://boto3.readthedocs.io/en/latest/_modules/boto3/dynamodb/conditions.htmlboto3 dynamodb IN条件

您可以创建一个条件是这样的:

Attr(key).eq(value) 

在那里你是否value等于使用class Attr()与均衡方法key的价值。

但是,您如何使用SQL IN运算符?

我想查询,如果值IN键的值

在文档只是出现一个在课堂上,我没有看到任何例子或者如何使用它

class In(ComparisonCondition): 
    expression_operator = 'IN' 
    has_grouped_values = True 

回答

1

操作IN无法使用在KeyConditionExpression即使用查询API获取多个哈希键值。 DynamoDB目前不支持此功能。

如果您需要检索多个哈希键值,请使用Batch Get Item api。

示例代码: -

email1 = "[email protected]" 
email2 = "[email protected]" 

try: 
    response = dynamodb.batch_get_item(
    RequestItems={ 
     'users': { 
      'Keys': [ 
       { 
        'email': email1 
       }, 
       { 
        'email': email2 
       }, 
      ],    
      'ConsistentRead': True    
     } 
    }, 
    ReturnConsumedCapacity='TOTAL' 
) 
except ClientError as e: 
    print(e.response['Error']['Message']) 
else: 
    item = response['Responses'] 
    print("BatchGetItem succeeded:") 
    print(json.dumps(item, indent=4, cls=DecimalEncoder))