登录
推荐 文章 Go 技术 课程 下载 专题 AI
首页 >  Golang >  Go问答

DynamoDb 键架构过大,必须包含的最大数量

来源:stackoverflow

时间:2024-03-18 20:30:32 387浏览 收藏

使用 DynamoDB 时,键架构的大小有限制。主键和辅助键(外键)的总数不能超过 2 个。如果试图创建包含更多键的表,则会收到“密钥架构太大”错误。这与 SQL 数据库不同,在 SQL 数据库中,可以创建包含多个外键的表。在 NoSQL 数据建模中,需要采用不同的方法,以避免使用标准化数据模型。了解 NoSQL 数据建模的更多信息,可以帮助您避免此错误并有效使用 DynamoDB。

问题内容

我正在使用 golang 实现 dynamodb,但在创建时遇到错误,下表是我在应用程序中使用的代码,并且还发布了我面临的错误

input = &dynamodb.CreateTableInput{
            AttributeDefinitions: []*dynamodb.AttributeDefinition{
                {
                    AttributeName: aws.String("uuid"),
                    AttributeType: aws.String("S"),
                },
                {
                    AttributeName: aws.String("user_id"),
                    AttributeType: aws.String("N"),
                },
                {
                    AttributeName: aws.String("card_id"),
                    AttributeType: aws.String("S"),
                },
                {
                    AttributeName: aws.String("bid_amount"),
                    AttributeType: aws.String("N"),
                },
                {
                    AttributeName: aws.String("bid_flag"),
                    AttributeType: aws.String("N"),
                },
                {
                    AttributeName: aws.String("added_time"),
                    AttributeType: aws.String("N"),
                },
                {
                    AttributeName: aws.String("updated_time"),
                    AttributeType: aws.String("N"),
                },
            },
            KeySchema: []*dynamodb.KeySchemaElement{
                {
                    AttributeName: aws.String("uuid"),
                    KeyType:       aws.String("HASH"),
                },
                {
                    AttributeName: aws.String("user_id"),
                    KeyType:       aws.String("RANGE"),
                },
                {
                    AttributeName: aws.String("card_id"),
                    KeyType:       aws.String("RANGE"),
                },
                {
                    AttributeName: aws.String("bid_amount"),
                    KeyType:       aws.String("STRING"), // this is wrong but I'm just testing it
                },
                {
                    AttributeName: aws.String("bid_flag"),
                    KeyType:       aws.String("STRING"),// this is wrong but I'm just testing it
                },
                {
                    AttributeName: aws.String("added_time"),
                    KeyType:       aws.String("STRING"),// this is wrong but I'm just testing it
                },
                {
                    AttributeName: aws.String("updated_time"),
                    KeyType:       aws.String("STRING"), // this is wrong but I'm just testing it
                },
            },
            ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
                ReadCapacityUnits:  aws.Int64(5),
                WriteCapacityUnits: aws.Int64(5),
            },
            TableName: aws.String(tableName),
        }
    }
    result, err := dbSvc.CreateTable(input)

validationexception:密钥架构太大。 key schema 最多只能由表的 hash 和 range key 组成 状态代码:400,请求 id:b4f9453f-3032-458d-ab75-a08ee317c96b

从错误中我了解到,在 keyschema 中只有一个主键和一个辅助键(forigen key)。但在我的数据中有三个 forigen 键和一个主键,那么我该如何实现它。如果我做的或说的有什么问题,请抱歉,我是 dynamodb 的新手。请帮助我摆脱这个困境。


正确答案


这个问题表明您不清楚NoSQL和SQL数据库之间的区别。您似乎正在尝试在 DynamoDB 中使用标准化数据模型(就像使用 SQL 数据库一样)。这不是使用 DynamoDB 的方式。

有大量在线资源可用于教授使用 DynamoDB 进行 NoSQL 数据建模。我建议您在继续之前花些时间了解有关 NoSQL 数据建模的更多信息。您将为自己节省大量时间并减少挫败感!

从 this talk by Alex DeBrie from the 2019 AWS Re:Invent conference 开始。这是一个很棒的入门资源。

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>