Как создать таблицу DynamodB с глобальными вторичными индексами

я хочу создать стол User DynamoDB с некоторыми атрибутами, которые разработаны 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 будет ответ с 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

Решение

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

$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 и атрибут Range в определение атрибута Первичных таблиц. В дополнение к этому, как упомянул Lokesh, вы можете использовать тип данных StringSet для вашего объекта Avatar.

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

3

Другие решения

В Dynamodb вы не можете сохранить сложный объект. В вашем случае аватар, вы не можете сохранить его как сложный объект.

Но вы можете сохранить объект аватара JSON как строку, и аватар будет иметь столбец только с типом string.

После сохранения любого 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

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector