asp*_*asp 4 python sdk amazon-web-services amazon-dynamodb-streams
我正在尝试用 python 创建 dynamo db 表。下面是我的脚本。我正在尝试创建一个分区键和排序键和一堆列。
我试过的:
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.create_table(
TableName='g_view_data',
KeySchema=[
{
'AttributeName': 'customer_id',
'KeyType': 'HASH' #Partition key
},
{
'AttributeName': 'key_id',
'KeyType': 'RANGE' #Sort key
}
],
AttributeDefinitions=[
{
'AttributeName': 'cusotmer_id',
'AttributeType': 'N'
},
{
'AttributeName': 'key_id',
'AttributeType': 'N'
},
{
'AttributeName': 'dashboard_name',
'AttributeType': 'S'
},
{
'AttributeName': 'tsm',
'AttributeType': 'S'
},
{
'AttributeName': 'security_block',
'AttributeType': 'S'
},
{
'AttributeName': 'core_block',
'AttributeType': 'S'
},
{
'AttributeName': 'type',
'AttributeType': 'S'
},
{
'AttributeName': 'subscription',
'AttributeType': 'S'
},
{
'AttributeName': 'account_id',
'AttributeType': 'S'
},
{
'AttributeName': 'region',
'AttributeType': 'S'
},
{
'AttributeName': 'NAT',
'AttributeType': 'S'
},
{
'AttributeName': 'jb',
'AttributeType': 'S'
},
{
'AttributeName': 'dc',
'AttributeType': 'S'
},
{
'AttributeName': 'av',
'AttributeType': 'S'
},
{
'AttributeName': 'gl',
'AttributeType': 'S'
},
{
'AttributeName': 'backup',
'AttributeType': 'S'
},
{
'AttributeName': 'cpm',
'AttributeType': 'S'
},
{
'AttributeName': 'zb',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
print("Table status:", table.table_status)
Run Code Online (Sandbox Code Playgroud)
我得到的输出:
invalid One or more parameter values were invalid: Some index key attributes are not defined in AttributeDefinitions.
Run Code Online (Sandbox Code Playgroud)
有人可以建议我的代码有什么问题吗..只是想创建一个简单的表。为什么它抱怨缺少参数。不确定..有人可以建议吗
编辑 1:在属性中添加 customer_id,key_id 后,我收到不同的错误
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the CreateTable operation: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions
Run Code Online (Sandbox Code Playgroud)
小智 7
这已经太晚了,但是您的属性定义中的 customer_id 有一个拼写错误
KeySchema=[
{
'AttributeName': 'customer_id',
'KeyType': 'HASH' #Partition key
},
{
'AttributeName': 'key_id',
'KeyType': 'RANGE' #Sort key
}
],
AttributeDefinitions=[
{
'AttributeName': 'cusotmer_id',
'AttributeType': 'N'
},
Run Code Online (Sandbox Code Playgroud)
正如AWS 文档中所述,KeySchema 中的属性也必须在 AttributeDefinitions 中定义。请尝试将 customer_id 和 key_id 添加到您的 AttributeDefinitions 中。
至于 AttributeDefinitions,它们仅用于键(主键和索引)。所以这是一个对我有用的例子:
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.create_table(
TableName='g_view_data',
KeySchema=[
{
'AttributeName': 'customer_id',
'KeyType': 'HASH'
},
{
'AttributeName': 'key_id',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'customer_id',
'AttributeType': 'N'
},
{
'AttributeName': 'key_id',
'AttributeType': 'N'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
print("Table status:", table.table_status)
Run Code Online (Sandbox Code Playgroud)
然后在创建该表后,您可以添加具有您想要的任何属性的项目。例如,将此代码添加到您的脚本中:
table = dynamodb.Table('g_view_data')
item = table.put_item(
Item={
'customer_id': 234,
'key_id': 123,
'dashboard_name': 'test',
}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4250 次 |
| 最近记录: |