2016-06-26 3 views
0

Я хочу создать таблицу User по dynamoDB с некоторыми атрибутами, которые дизайн чванства:Как создать таблицу dynamodb с глобальными вторичными индексами

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 воли ответ с JSON после putItem, как показано ниже:

{ 
"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."} 

Спасибо заранее!

+0

Какая ошибка? –

+0

@HarshalBulsara Я добавил (а) –

ответ

0

В 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 
     ] 
    ] 
], 
0

Основываясь на ошибки, кажется, что вы забыли добавить атрибуты в основной таблице, следующий код должен работать

$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 для вашего объекта Avatar.

Надеюсь, что это поможет.