使用 boto 使用 LSI 在 dynamoDB 中创建 table
Create table in dynamoDB with LSI using boto
我知道如何使用 boto 创建 table,但我找不到任何关于创建还包括 LSI 的 table 的在线帮助。我搜索了 boto 文档和 AWS 文档。如果您有一个如何创建这样的简单示例 table,我可以从那里获取。
谢谢
在我的研究中遇到了你的问题,发现了同样的事情。不确定您是否已经弄清楚了这一点,但我想我会 post 将我的发现提供给可能需要了解这一点的任何其他人。
在我的情况下,我需要能够查询并根据两个不同的排序键返回结果,'date' 和 'post_id' 对于我需要创建的 Table '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)
我知道如何使用 boto 创建 table,但我找不到任何关于创建还包括 LSI 的 table 的在线帮助。我搜索了 boto 文档和 AWS 文档。如果您有一个如何创建这样的简单示例 table,我可以从那里获取。
谢谢
在我的研究中遇到了你的问题,发现了同样的事情。不确定您是否已经弄清楚了这一点,但我想我会 post 将我的发现提供给可能需要了解这一点的任何其他人。
在我的情况下,我需要能够查询并根据两个不同的排序键返回结果,'date' 和 'post_id' 对于我需要创建的 Table '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)