使用boto3(“botocore.exceptions.ClientError”)创建dynamodb表时出错

2024-05-26 09:18:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用boto3创建dynamodb表。但我得到了以下错误:

“botocore.exceptions.ClientError:调用CreateTable操作时出错(ValidationException):KeySchema无效:第一个KeySchemaElement不是哈希键类型”

更多信息: 我的帐户中没有任何全局表

我试过的代码:

import boto3

client = boto3.client('dynamodb')

response = client.create_table(
    AttributeDefinitions=[
        {
            'AttributeName': 'student_id',
            'AttributeType': 'N'
        },
        {
            'AttributeName': 'student_name',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'course_id',
            'AttributeType': 'S'
        }
    ],
    TableName='students',
    KeySchema=[
        {
            'AttributeName': 'student_name',
            'KeyType': 'HASH'
        },
{
            'AttributeName': 'student_id',
            'KeyType': 'RANGE'
        },
    ],
    LocalSecondaryIndexes=[
        {
            'IndexName': 'course_id',
            'KeySchema': [
                {
                    'AttributeName': 'course_id',
                    'KeyType': 'RANGE'
                }
            ],
            'Projection': {
                'ProjectionType': 'ALL'
            }
        },
    ],
    BillingMode='PAY_PER_REQUEST',

)

Tags: nameclientid错误rangeboto3dynamodbstudent
1条回答
网友
1楼 · 发布于 2024-05-26 09:18:29

还应明确指定LSI的哈希键

import boto3

client = boto3.client('dynamodb')

response = client.create_table(
    AttributeDefinitions=[
        {
            'AttributeName': 'student_id',
            'AttributeType': 'N'
        },
        {
            'AttributeName': 'student_name',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'course_id',
            'AttributeType': 'S'
        }
    ],
    TableName='students',
    KeySchema=[
        {
            'AttributeName': 'student_name',
            'KeyType': 'HASH'
        },
        {
            'AttributeName': 'student_id',
            'KeyType': 'RANGE'
        },
    ],
    LocalSecondaryIndexes=[
        {
            'IndexName': 'course_id',
            'KeySchema': [
                {
                    'AttributeName': 'student_name',
                    'KeyType': 'HASH'
                },
                {
                    'AttributeName': 'course_id',
                    'KeyType': 'RANGE'
                }
            ],
            'Projection': {
                'ProjectionType': 'ALL'
            }
        },
    ],
    BillingMode='PAY_PER_REQUEST',

)

相关问题 更多 >