DynamoDB - GSI更新项目(PHP)

时间:2016-01-21 15:27:54

标签: amazon-dynamodb

我正在尝试使用全球二级索引更新项目。我的表定义如下所示。我是DynamoDb的新手。

    $response = $this->client->createTable([
        'TableName' => 'rawproducts_products',
        'AttributeDefinitions' => [
            [
                'AttributeName' => 'product_code',
                'AttributeType' => 'N'
            ],
            [
                'AttributeName' => 'token',
                'AttributeType' => 'S'
            ],
            [
                'AttributeName' => 'processed_at',
                'AttributeType' => 'N'
            ],
            [
                'AttributeName' => 'created_at',
                'AttributeType' => 'N'
            ]                    
        ],
        'KeySchema' => [
            [
                'AttributeName' => 'product_code',
                'KeyType' => 'HASH' 
            ],
            [
                'AttributeName' => 'token',
                'KeyType' => 'RANGE' 
            ]
        ],
        'LocalSecondaryIndexes' => [
            [
                'IndexName' => 'ProductCodeProcessedIndex',
                'KeySchema' => [
                    ['AttributeName' => 'product_code', 'KeyType' => 'HASH'],
                    ['AttributeName' => 'processed_at', 'KeyType' => 'RANGE']
                ],
                'Projection' => [
                    'ProjectionType' => 'KEYS_ONLY',
                ],
            ],
            [
                'IndexName' => 'ProductCodeCreatedIndex',
                'KeySchema' => [
                    ['AttributeName' => 'product_code', 'KeyType' => 'HASH'],
                    ['AttributeName' => 'created_at', 'KeyType' => 'RANGE']
                ],
                'Projection' => [
                    'ProjectionType' => 'KEYS_ONLY',
                ],
            ]                
        ],
        'GlobalSecondaryIndexes' => [
            [
                'IndexName' => 'TokenIndex',
                'KeySchema' => [
                    [ 'AttributeName' => 'token', 'KeyType' => 'HASH' ]
                ],
                'Projection' => [
                    'ProjectionType' => 'ALL'
                ],
                'ProvisionedThroughput' => [
                    'ReadCapacityUnits' => 1, 'WriteCapacityUnits' => 1 
                ] 
            ] 
        ],            
        'ProvisionedThroughput' => [
            'ReadCapacityUnits'    => 5,
            'WriteCapacityUnits' => 6
        ]
    ]);

当我尝试使用以下查询更新属性“complete”时:

    $this->dynamoDb->updateItem([
        'TableName' => $this->table,
        'TableIndex' => 'TokenIndex',
        'Key'   =>  [
            'token'    =>  ['S' => (string)$this->token['S']]
        ],
        'UpdateExpression'  =>  'set complete = :complete',
        'ExpressionAttributeValues' =>  [
            ':complete' =>  ['N'   => (string)1]
        ]
    ]);

但我一直收到以下错误:

    "The provided key element does not match the schema"

任何人都可以告诉新手。非常感谢。

1 个答案:

答案 0 :(得分:4)

它显示了这一点,因为你没有正确传递密钥

You need to pass product_code and token both to update the value

其次

You cannot update value directly from GSI it's just a projection of the Table and not actual table

如果要更新必须在不在索引

中的表中更新的值

refer this link.