【问题标题】:How create table dynamodb with global secondary indexes如何使用全局二级索引创建表 dynamodb
【发布时间】:2016-10-28 07:55:27
【问题描述】:

我想通过 dynamoDB 创建表 User,其中包含一些由 swagger 设计的属性:

User {
   id (string, optional): UUID of User ,
   name (string, optional),
   lastLoginedAt (string, optional),
   avatar (Avatar, optional),
}

Avatar {
   avatarId (string, optional):,
   iconUri (string, optional),
   message (string, optional),
}

并且希望User 会在 putItem 之后以 json 响应,如下所示:

{
"id": "string",
"name": "string",
"lastLoginedAt": "2016-06-24 15:28:26",
"avatar": {
  "avatarId": "string",
  "iconUri": "string",
  "message": "string"
},
}

我是初学者 Dynamodb,我仍然坚持使用创建表,这里是我的代码:

$dynamodb->createTable([
'TableName' => 'User',
'AttributeDefinitions' => [
    [ 'AttributeName' => 'id', 'AttributeType' => 'S' ],
    [ 'AttributeName' => 'name', 'AttributeType' => 'S' ],
    [ 'AttributeName' => 'avatar', 'AttributeType' => 'S' ]
],
'KeySchema' => [
    [ 'AttributeName' => 'id', 'KeyType' => 'HASH' ],  
    [ 'AttributeName' => 'name', 'KeyType' => 'RANGE' ]
],
'GlobalSecondaryIndexes' => [
    [
        'IndexName' => 'avatarIndex',
        'KeySchema' => [
            [ 'AttributeName' => 'avatarId', 'KeyType' => 'HASH' ],  
            [ 'AttributeName' => 'id', 'KeyType' => 'RANGE' ] 
        ],
        'Projection' => [ 
            'ProjectionType' => 'INCLUDE',
            'NonKeyAttributes' => [ 'iconUri', 'message' ]
        ],
        'ProvisionedThroughput' => [
            'ReadCapacityUnits' => 5,
            'WriteCapacityUnits' => 5
        ]
    ]
],
'ProvisionedThroughput' => [
    'ReadCapacityUnits' => 5,
    'WriteCapacityUnits' => 5
]]);

这是错误:

local.ERROR: Error executing "CreateTable" on "http://172.18.0.5:8000"; AWS HTTP error: Client error: `POST http://172.18.0.5:8000` resulted in a `400 Bad Request` response:{"__type":"com.amazon.coral.validate#ValidationException","message":"Global Secondary Index hash key not specified in At (truncated...)
ValidationException (client): Global Secondary Index hash key not   specified in Attribute Definitons.Type unknown. - {"__type":"com.amazon.coral.validate#ValidationException","message":"Global Secondary Index hash key not specified in Attribute Definitons.Type unknown."}

提前感谢!

【问题讨论】:

  • @HarshalBulsara 我已经添加了它

标签: php amazon-dynamodb swagger create-table


【解决方案1】:

基于错误,您似乎忘记在主表中添加属性,以下代码应该可以工作

$dynamodb->createTable([
'TableName' => 'User',
'AttributeDefinitions' => [
    [ 'AttributeName' => 'id', 'AttributeType' => 'S' ], 
    [ 'AttributeName' => 'name', 'AttributeType' => 'S' ],
    [ 'AttributeName' => 'avatar', 'AttributeType' => 'S' ],
    [ 'AttributeName' => 'avatarId', 'AttributeType' => 'S' ], // this attribute was missing  

],
'KeySchema' => [
    [ 'AttributeName' => 'id', 'KeyType' => 'HASH' ],  
    [ 'AttributeName' => 'name', 'KeyType' => 'RANGE' ]
],
'GlobalSecondaryIndexes' => [
    [
        'IndexName' => 'avatarIndex',
        'KeySchema' => [
            [ 'AttributeName' => 'avatarId', 'KeyType' => 'HASH' ],  
            [ 'AttributeName' => 'id', 'KeyType' => 'RANGE' ] 
        ],
        'Projection' => [ 
            'ProjectionType' => 'INCLUDE',
            'NonKeyAttributes' => [ 'iconUri', 'message' ]
        ],
        'ProvisionedThroughput' => [
            'ReadCapacityUnits' => 5,
            'WriteCapacityUnits' => 5
        ]
    ]
],
'ProvisionedThroughput' => [
    'ReadCapacityUnits' => 5,
    'WriteCapacityUnits' => 5
]]);

您需要在主表属性定义中包含 GSI 的 hash 和 Range 属性。除了 Lokesh 提到的之外,您还可以为您的 Avatar 对象使用 StringSet 数据类型。

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    在 Dynamodb 中,您无法保存复杂的对象。在您的情况下,您不能将其保存为复杂对象。

    但是您可以将头像对象 JSON 保存为字符串,而头像将是一个仅具有字符串类型的列。

    一旦将任何 JSON 保存为字符串,就无法在 JSON 中创建属性索引。

    在您的情况下,您不应该以 json 格式保存头像。您可以创建 avatarId、avatarIconUri 和 avatarMessage 等列。

    {
    "id": "string",
    "name": "string",
    "lastLoginedAt": "2016-06-24 15:28:26",
    "avatarId": "string",
    "avatarIconUri": "string",
    "avatarMessage": "string"
    }
    
    
    
    GlobalSecondaryIndexes' => [
        [
            'IndexName' => 'avatarIndex',
            'KeySchema' => [
                [ 'AttributeName' => 'avatarId', 'KeyType' => 'HASH' ],  
                [ 'AttributeName' => 'id', 'KeyType' => 'RANGE' ] 
            ],
            'Projection' => [ 
                'ProjectionType' => 'INCLUDE',
                'NonKeyAttributes' => [ 'avatarIconUri', 'avatarMessage' ]
            ],
            'ProvisionedThroughput' => [
                'ReadCapacityUnits' => 5,
                'WriteCapacityUnits' => 5
            ]
        ]
    ],
    

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 2020-11-12
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多