2015-11-03 52 views
0

我知道如何使用boto创建表,但我无法找到任何有关创建包含LSI的表的在线帮助。我搜索了boto文档和AWS文档。如果你有一个简单的例子来说明如何创建这样一个表格,那么我可以从这里取出它。使用boto在LSI的dynamoDB中创建表使用boto

谢谢

回答

3

在我的研究中发现你的问题找到相同的东西。不知道你是否想通了,但我想我会发布我的调查结果给任何其他人可能需要知道这一点。

在我的情况下,我需要能够查询并根据两个不同的排序键返回结果,'date'和'post_id'为我需要创建的表,称为'Posts'和本地二级索引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)