2015-11-03 58 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)