Php 如何使用全局二级索引创建表dynamodb

Php 如何使用全局二级索引创建表dynamodb,php,amazon-dynamodb,swagger,create-table,Php,Amazon Dynamodb,Swagger,Create Table,我想通过dynamoDB创建表User,其中包含一些属性,这些属性由swagger设计: User { id (string, optional): UUID of User , name (string, optional), lastLoginedAt (string, optional), avatar (Avatar, optional), } Avatar { avatarId (string, optional):, iconUri (strin

我想通过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),
}
而want
用户
将在putItem之后使用json进行响应,如下所示:

{
"id": "string",
"name": "string",
"lastLoginedAt": "2016-06-24 15:28:26",
"avatar": {
  "avatarId": "string",
  "iconUri": "string",
  "message": "string"
},
}
我是begginer 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."}

提前谢谢

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

但您可以将avatar对象JSON保存为字符串,avatar将成为一个只包含字符串类型的列

一旦将任何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
        ]
    ]
],

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

$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的哈希和范围属性。除此之外,正如Lokesh所提到的,您还可以为您的化身对象使用StringSet数据类型


希望能有所帮助。

@HarshalBulsara我已经添加了它