字符串数组架构的 DynamoDB 数组

DynamoDB Array of Array of strings schema

我正在寻求帮助,了解如何将下面的架构实现到我的 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 作为 table 的 HASH 键(又名分区键),假设这些属性中的任何一个都将唯一标识用户。当您有多个项目与一个分区键 c.f 相关联时,RANGE 键(又名排序键)很有用。 DynamoDB 用户指南的 Primary Key 段中的艺术家和歌曲标题。因此,这意味着您只需在 AttributeDefinitionsKeySchema.

中指定一个属性

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

相反,您可以在调用 putItem() or updateItem (or the corresponding put() and update() functions if you use the 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"),我认为允许多个是有意义的。