【问题标题】:Create table in dynamoDB with LSI using boto使用 boto 使用 LSI 在 dynamoDB 中创建表
【发布时间】:2016-01-02 03:40:28
【问题描述】:

我知道如何使用 boto 创建表,但我找不到任何有关创建还包含 LSI 的表的在线帮助。我搜索了 boto 文档和 AWS 文档。如果你有一个如何创建这样一个表的简单示例,我可以从那里获取。

谢谢

【问题讨论】:

    标签: python amazon-dynamodb boto


    【解决方案1】:

    在我的研究中遇到了你的问题,发现了同样的事情。不确定您是否已经弄清楚了,但我想我会将我的发现发布给其他可能需要知道这一点的人。

    在我的情况下,我需要能够根据两个不同的排序键“日期”和“post_id”来查询并返回结果,我需要创建一个名为“帖子”的表和一个本地二级索引 (LSI ) 我想要创建的,称为“PostsByDate”。

    下面是我的解决方案。它是用 Python 编写的,但我相信你可以用你自己选择的语言弄清楚如何处理它。

    from __future__ import print_function # Python 2/3 compatibility
    import boto3
    
    dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000", region_name="us-west-2")
    
    
    table = dynamodb.create_table(
        TableName='Posts',
        KeySchema=[
            {
                'AttributeName': 'user_id',
                'KeyType': 'HASH'  #Partition key
            },
            {
                'AttributeName': 'post_id',
                'KeyType': 'RANGE'  #Sort key
            },
        ],
        AttributeDefinitions=[
            {
                'AttributeName': 'user_id',
                'AttributeType': 'N'
            },
            {
                'AttributeName': 'post_id',
                'AttributeType': 'N'
            },
            {
                'AttributeName': 'date',
                'AttributeType': 'N'
            },
    
        ],
        LocalSecondaryIndexes=[
            {
                'IndexName': 'PostsByDate',
                'KeySchema': [
                    {
                        'AttributeName': 'user_id',
                        'KeyType': 'HASH'  #Partition key
                    },
                    {
                        'AttributeName': 'date',
                        'KeyType': 'RANGE'  #Sort key
                    },
                ],
                'Projection': {
                    'ProjectionType': 'ALL'
                }
            }
        ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 10,
            'WriteCapacityUnits': 10,
            }
    )
    
    print("Table status:", table.table_status)
    

    【讨论】:

    • 在这种情况下,对于 PostsByDate 过滤,必须同时提供 user_id 和 date 来过滤数据,还是只使用 date 就足够了?
    • 将通过同时提供user_iddate 来过滤数据
    猜你喜欢
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2020-03-28
    相关资源
    最近更新 更多