duansha3771 2013-12-22 21:49
浏览 159
已采纳

使用全局二级索引创建DynamoDB表

I'm new to AWS DynamoDB and to nosql in general, and I'm having problem with table creation.

I'm trying to create a table named User with the following Attributes:

  • UserId (HASH)
  • OSType (RANGE)
  • MSISDN
  • IMSI
  • DeviceID

I need to query the table not only by UserId, but also by the following fields:

  • MSISDN
  • IMSI
  • DeviceID

My Logic is as follows:

  1. Query the table by the UserId field.
  2. If the query returned no results create a new one, but check that there isn't other users with the same MSISDN filed OR the same IMSI field OR the same DeviceID field.

After reading the manual about LSI/GSI i'm having difficulties understating how to create the table and define those indexes.

This is the code i'm tring to execute to creater the table using 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
    )
));

I'm getting this error:

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

Please help me understand what am I doing wrong. I want to create the table with GSI, but I just can't comprehend the essence of secondary index in DynamoDB :(

  • 写回答

2条回答 默认 最新

  • dql7588 2014-01-23 06:51
    关注

    This should work:

    $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
    )));
    

    Hope that helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止