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

标签 php amazon-dynamodb swagger create-table

我想创建表 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将在 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."}

先谢过!

最佳答案

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

$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 提到的之外,您还可以为 Avatar 对象使用 StringSet 数据类型。

希望有帮助。

关于php - 如何使用全局二级索引创建表 dynamodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38034690/

相关文章:

swagger - OpenApi 中的摘要和描述有什么区别?

java - Swagger 不扫描位于不同 jar 文件中的实体类中的 ApiModel 和 ApiModelProperty 注释

php - 如何使用 PHP 在单个 foreach 循环中将三个值添加到数据库中?

php - 表单生成器中的过滤器集合 [symfony2]

java - DynamoDB 中分区键内多列组合的唯一约束

node.js - DynamoDB 扫描 FilterExpression 返回空结果

python - 如何从现有的 falcon api 生成 Open API 规范?

php - 如何让 PHPUnit 在测试路径中排除 .svn 目录?

php - 如何使用 mysqli 在 php 变量中存储 mysql select 语句

java - 动态数据库 API : How can I build an "add JSON attribute if not present" update request?