php 使用全局辅助索引创建DynamoDB表

xhv8bpkk  于 2023-09-29  发布在  PHP
关注(0)|答案(2)|浏览(107)

我是AWS DynamoDB和nosql的新手,我在创建表时遇到了问题。
我尝试创建一个名为User的表,并具有以下属性:

  • UserId(HASH)
  • OSType(范围)
  • MSISDN
  • IMSI
  • DeviceID

我不仅需要通过UserId查询表,还需要通过以下字段查询:

  • MSISDN
  • IMSI
  • DeviceID

我的逻辑如下:
1.通过UserId字段查询表。
1.如果查询没有返回任何结果,则创建一个新的查询,但检查是否有其他用户具有相同的MSISDN字段相同的IMSI字段相同的DeviceID字段。
在阅读了manual about LSI/GSI之后,我很难理解如何创建表和定义这些索引。
这是我特灵使用PHP+AWS SDK来创建表的代码:

$client->createTable(array(
    'TableName' => 'User',
    'AttributeDefinitions' => array(
        array('AttributeName' => 'UserId',      'AttributeType' => 'S'),
        array('AttributeName' => 'OSType',      'AttributeType' => 'S'),
        array('AttributeName' => 'MSISDN',      'AttributeType' => 'S'),
        array('AttributeName' => 'IMSI',        'AttributeType' => 'S'),
        array('AttributeName' => 'DeviceID',    'AttributeType' => 'S'),
    ),
    'KeySchema' => array(
        array('AttributeName' => 'UserId', 'KeyType' => 'HASH'),
        array('AttributeName' => 'OSType', 'KeyType' => 'RANGE')
    ),
    'GlobalSecondaryIndexes' => array(
        array(
            'IndexName' => 'IMSIIndex',
            'KeySchema' => array(
                array('AttributeName' => 'IMSI',    'KeyType' => 'HASH')
            ),
            'Projection' => array(
                'ProjectionType' => 'KEYS_ONLY',
            ),
            'ProvisionedThroughput' => array(
                'ReadCapacityUnits'  => 10,
                'WriteCapacityUnits' => 10
            )
        ),
        array(
            'IndexName' => 'MSISDNIndex',
            'KeySchema' => array(
                array('AttributeName' => 'MSISDN',  'KeyType' => 'HASH')
            ),
            'Projection' => array(
                'ProjectionType' => 'KEYS_ONLY',
            ),
            'ProvisionedThroughput' => array(
                'ReadCapacityUnits'  => 10,
                'WriteCapacityUnits' => 10
            )
        ),
        array(
            'IndexName' => 'DeviceIDIndex',
            'KeySchema' => array(
                array('AttributeName' => 'DeviceID',    'KeyType' => 'HASH')
            ),
            'Projection' => array(
                'ProjectionType' => 'KEYS_ONLY',
            ),
            'ProvisionedThroughput' => array(
                'ReadCapacityUnits'  => 10,
                'WriteCapacityUnits' => 10
            )
        ),
    ),
    'ProvisionedThroughput' => array(
        'ReadCapacityUnits'  => 50,
        'WriteCapacityUnits' => 50
    )
));

我得到这个错误:

PHP Fatal error:  Uncaught Aws\DynamoDb\Exception\ValidationException: AWS Error Code: ValidationException, Status Code: 400, AWS Request ID: 70LGIARTTQF90S8P0HVRUKSJ27VV4KQNSO5AEMVJF66Q9ASUAAJG, AWS Error Type: client, AWS Error Message: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions, User-Agent: aws-sdk-php2/2.4.11 Guzzle/3.7.4 curl/7.29.0 PHP/5.4.14

请帮助我了解我做错了什么。我想用GSI创建表,但我就是不能理解DynamoDB中二级索引的本质:(

pgpifvop

pgpifvop1#

只有包含在本地和全局索引中的属性才应在AttributeDefinitions部分中定义。

46qrfjad

46qrfjad2#

这应该是可行的:

$client->createTable(array(
'TableName' => 'User',
'AttributeDefinitions' => array(
    array('AttributeName' => 'UserId',      'AttributeType' => 'S'),
    array('AttributeName' => 'OSType',      'AttributeType' => 'S')
),
'KeySchema' => array(
    array('AttributeName' => 'UserId', 'KeyType' => 'HASH'),
    array('AttributeName' => 'OSType', 'KeyType' => 'RANGE')
),
'GlobalSecondaryIndexes' => array(
    array(
        'IndexName' => 'IMSIIndex',
        'KeySchema' => array(
            array('AttributeName' => 'IMSI',    'KeyType' => 'HASH')
        ),
        'Projection' => array(
            'ProjectionType' => 'KEYS_ONLY',
        ),
        'ProvisionedThroughput' => array(
            'ReadCapacityUnits'  => 10,
            'WriteCapacityUnits' => 10
        )
    ),
    array(
        'IndexName' => 'MSISDNIndex',
        'KeySchema' => array(
            array('AttributeName' => 'MSISDN',  'KeyType' => 'HASH')
        ),
        'Projection' => array(
            'ProjectionType' => 'KEYS_ONLY',
        ),
        'ProvisionedThroughput' => array(
            'ReadCapacityUnits'  => 10,
            'WriteCapacityUnits' => 10
        )
    ),
    array(
        'IndexName' => 'DeviceIDIndex',
        'KeySchema' => array(
            array('AttributeName' => 'DeviceID',    'KeyType' => 'HASH')
        ),
        'Projection' => array(
            'ProjectionType' => 'KEYS_ONLY',
        ),
        'ProvisionedThroughput' => array(
            'ReadCapacityUnits'  => 10,
            'WriteCapacityUnits' => 10
        )
    ),
),
'ProvisionedThroughput' => array(
    'ReadCapacityUnits'  => 50,
    'WriteCapacityUnits' => 50
)));

希望有帮助

相关问题