DynamoDB是一种无模式数据库,这意味着您在创建时不需要定义所有属性,但您需要非常明确地说明主要属性和索引。每个声明的属性都需要一个显式的索引,因此会出现错误“必须对所有属性进行索引”。
例如,下面的代码将要求任何新条目中都必须包含“电子邮件”和“时间戳”:
provider "aws" {
region = var.region
}
module "dynamodb_table_1" {
source = "../../"
name = "Subscribed_Users"
hash_key = "Email"
range_key = "Timestamp"
enable_autoscaler = false
dynamodb_attributes = [
{
name = "Email"
type = "S"
}
]
local_secondary_index_map = [
{
name = "EmailAndTimestampSortIndex"
hash_key = "Email"
range_key = "Timestamp"
projection_type = "INCLUDE"
non_key_attributes = ["Email", "Timestamp"]
},
]
context = module.this.context
}
以下是两个有效的插入(因为它们都包含电子邮件和时间戳):
第一条记录
{
"Email": {
"S": "foo@foo.com"
},
"Timestamp": {
"S": "Wed, 18 Jan 2023 01:45:40 GMT"
},
"Address": {
"M": {
"Country": {
"S": "Australia"
}
}
},
"Name": {
"S": "Melchor"
}
}
第二项
{
"Email": {
"S": "foo2@foo.com"
},
"Timestamp": {
"S": "Wed, 18 Jan 2023 01:45:40 GMT"
},
"Name": {
"S": "Bob"
}
}
插入的值:
![输入图像描述](https://istack.dev59.com/Mt6vf.webp)
关于哈希和范围键的详细解释,请参见:https://dev59.com/gl4c5IYBdhLWcg3w-OQ4#27348364