Dynamodb:使用两个以上的属性进行查询

Dynamodb: query using more than two attributes

在 Dynamodb 中,您需要在索引中指定可用于进行查询的属性。

如何使用两个以上的属性进行查询?

使用 boto 的示例。

Table.create('users', 
        schema=[
            HashKey('id') # defaults to STRING data_type
        ], throughput={
            'read': 5,
            'write': 15,
        }, global_indexes=[
            GlobalAllIndex('FirstnameTimeIndex', parts=[
                HashKey('first_name'),
                RangeKey('creation_date', data_type=NUMBER),
            ],
            throughput={
                'read': 1,
                'write': 1,
            }),
            GlobalAllIndex('LastnameTimeIndex', parts=[
                HashKey('last_name'),
                RangeKey('creation_date', data_type=NUMBER),
            ],
            throughput={
                'read': 1,
                'write': 1,
            })
        ],
        connection=conn)

如何使用 boto 查找名字 'John'、姓氏 'Doe' 以及创建于“2015 年 3 月 21 日”的用户?

您的数据建模过程必须考虑到您的数据检索需求,在 DynamoDB 中您只能通过散列或散列 + 范围键进行查询。

如果通过主键查询不能满足您的要求,您当然可以通过创建二级索引(本地或全局)来使用备用键。

但是,在某些情况下,多个属性的串联可以用作您的主键,以避免维护二级索引的成本。

如果您需要通过名字、姓氏和创建日期获取用户,我建议您将这些属性包含在哈希和范围键中,这样就不需要创建额外的索引。

哈希键应该包含一个可以由您的应用程序计算的值,同时提供统一的数据访问。例如,假设您选择按如下方式定义密钥:

哈希键(名称):first_name#last_name

范围键(已创建):MM-DD-YYYY-HH-mm-SS-毫秒

如果提到的属性不足以使您的密钥在 table.

中唯一,您始终可以附加其他属性
users = Table.create('users', schema=[
        HashKey('name'),
        RangeKey('created'),
     ], throughput={
        'read': 5,
        'write': 15,
     })

正在将用户添加到 table:

with users.batch_write() as batch:
     batch.put_item(data={
         'name': 'John#Doe',
         'first_name': 'John',
         'last_name': 'Doe',
         'created': '03-21-2015-03-03-02-3243',
     })

用于查找创建于“03-21-2015”的用户 John Doe 的代码应该类似于:

name_john_doe = users.query_2(
   name__eq='John#Doe',
   created__beginswith='03-21-2015'
)

for user in name_john_doe:
     print user['first_name']

重要注意事项:

我。如果您的查询开始变得太复杂,并且由于连接字段太多而导致哈希或范围键太长,那么一定要使用二级索引。这是一个好兆头,表明仅主索引不足以满足您的要求。

二。我提到哈希键应该提供统一数据访问

"Dynamo uses consistent hashing to partition its key space across its replicas and to ensure uniform load distribution. A uniform key distribution can help us achieve uniform load distribution assuming the access distribution of keys is not highly skewed." [DYN]

Hash Key不仅可以唯一标识记录,而且是保证负载分配的机制。 Range Key(使用时)有助于指示大部分将一起检索的记录,因此,存储也可以针对此类需求进行优化。

下面的link对题目有完整的解释:

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForTables.html#GuidelinesForTables.UniformWorkload