gpt4 book ai didi

javascript - 字符串数组架构的 DynamoDB 数组

转载 作者:行者123 更新时间:2023-11-30 14:00:44 24 4
gpt4 key购买 nike

我正在寻求帮助,了解如何将下面的架构实现到我的 dynamodb 数据库中。

我的 JSON 示例

var users = [{
userId: 123,
userName: "John Smith",
additionalInfomation: [
["favoriteColor", "blue"],
["hobbies", "ping-pong"]
]
}]

到目前为止,这是我实现 userIduserName 架构的方法。我在设置 additionalInfomation 部分时遇到问题。

  const params = {
AttributeDefinitions: [
{
AttributeName: 'userId',
AttributeType: 'N'
},
{
AttributeName: 'userName',
AttributeType: 'S'
},
{
AttributeName: 'additionalInformation',
AttributeType: <NEED HELP HERE> // <-------------------- ?????
}
],
KeySchema: [
{
AttributeName: 'userId',
KeyType: 'HASH'
},
{
AttributeName: 'userName',
KeyType: 'RANGE'
},
{
AttributeName: 'additionalInformation',
KeyType: <NEED HELP HERE> // <-------------------- ?????
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1
},
TableName: 'USERS',
StreamSpecification: {
StreamEnabled: false
}
};
// Call DynamoDB to create the table
ddb.createTable(params, function(err, data) {
if (err) {
console.log('Error', err);
} else {
console.log('Table Created', data);
}
});

需要帮助设置 additionalInformation 架构。如果这不是正确的方法,请原谅我的无知。仍在学习 dynamoDB 和 aws 文档对初学者不是很有帮助。

最佳答案

对于这个用例,我建议您选择 userIduserName 作为表的 HASH 键(又名分区键) ,假设这两个属性中的任何一个都将唯一标识用户。 RANGE 键(又名排序键)在您有多个项目与一个分区键相关联时很有用,c.f. Primary Key 中的艺术家和歌曲名称DynamoDB 用户指南的段落。因此,这意味着您只需在 AttributeDefinitionsKeySchema 中指定一个属性。

此外,我建议您从您的架构中省略 additionalInformation,因为您假设您既不会将该信息用作分区键也不会用作排序键。

相反,您可以在调用 putItem() 时将它们作为两个单独的属性添加到各个项目中或 updateItem (或相应的 put()update() 函数,如果您使用 DynamoDB DocumentClient )。

const params = {
Item: {
"userId": {
N: "123"
},
"userName": {
S: "dummy"
},
"favoriteColor": {
S: "blue"
},
"hobbies": {
SS: ["ping-pong", "another hobby"]
}
},
TableName: "USERS"
};
const putItemPromise = dynamodb.putItem(params).promise()

请注意,在上面的示例中,我将 favoriteColor 指定为 S,即单个字符串,但将 hobbies 指定为 SS 表示它是一个字符串数组。这可能是您想要的,也可能不是您想要的,但是由于属性名称是“hobbies”(而不是“hobby”),我认为允许多个是有意义的。

关于javascript - 字符串数组架构的 DynamoDB 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56346463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com