Menu
Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

Working with Global Secondary Indexes: PHP

You can use the AWS SDK for PHP Low-Level API to create a table with one or more global secondary indexes, describe the indexes on the table, and perform queries using the indexes. These operations map to the corresponding DynamoDB actions. For more information, see PHP Code Samples.

The following are the common steps for table operations using the AWS SDK for PHP.

  1. Create an instance of the DynamoDB client.

  2. Provide the parameters for a DynamoDB operation, including any optional parameters.

  3. Load the response from DynamoDB into a local variable for your application.

Create a Table With a Global Secondary Index

You can create global secondary indexes at the same time that you create a table. To do this, use CreateTable and provide your specifications for one or more global secondary indexes. The following PHP code snippet creates a table to hold information about weather data. The partition key is Location and the sort key is Date. A global secondary index named PrecipIndex allows fast access to precipitation data for various locations.

The following code snippet creates a table (WeatherData) with a global secondary index (PrecipIndex). The index partition key is Date and its sort key is Precipitation. All of the table attributes are projected into the index. Users can query this index to obtain weather data for a particular date, optionally sorting the data by precipitation amount.

Note that since Precipitation is not a key attribute for the table, it is not required; however, WeatherData items without Precipitation will not appear in PrecipIndex.


require 'vendor/autoload.php';

$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest'
]);

$dynamodb = $sdk->createDynamoDb();

$tableName = 'WeatherData';

$result = $dynamodb->createTable([
    'TableName' => $tableName,
    'AttributeDefinitions' => [
        [ 'AttributeName' => 'Location', 'AttributeType' => 'S' ],
        [ 'AttributeName' => 'Date', 'AttributeType' => 'S' ],
        [ 'AttributeName' => 'Precipitation', 'AttributeType' => 'N' ]
    ],
    'KeySchema' => [
        [ 'AttributeName' => 'Location', 'KeyType' => 'HASH' ],  //Partition key
        [ 'AttributeName' => 'Date', 'KeyType' => 'RANGE' ]  //Sort key
    ],
    'GlobalSecondaryIndexes' => [
        [
            'IndexName' => 'PrecipIndex',
            'ProvisionedThroughput' => [
                'ReadCapacityUnits' => 5,
                'WriteCapacityUnits' => 5
            ],
            'KeySchema' => [
                [ 'AttributeName' => 'Date', 'KeyType' => 'HASH' ],  //Partition key
                [ 'AttributeName' => 'Precipitation', 'KeyType' => 'RANGE' ]  //Sort key
            ],
            'Projection' => [ 'ProjectionType' => 'ALL' ]
        ]
    ],
    'ProvisionedThroughput' => [
        'ReadCapacityUnits' => 5,
        'WriteCapacityUnits' => 5
    ]
]);

You must wait until DynamoDB creates the table and sets the table status to ACTIVE. After that, you can begin putting data items into the table.

Describe a Table With a Global Secondary Index

To get information about global secondary indexes on a table, use DescribeTable. For each index, you can access its name, key schema, and projected attributes.


$tableName = 'WeatherData';

$result = $dynamodb->describeTable([
    'TableName' => $tableName
]);

foreach ($result['Table']['GlobalSecondaryIndexes'] as $key => $value) {
    echo "Info for index " . $value['IndexName'] . ":\n"; 
    foreach ($value['KeySchema'] as $attribute => $keyschema) {
            echo "\t" . 
            $value['KeySchema'][$attribute]['AttributeName'] . ': ' .
            $value['KeySchema'][$attribute]['KeyType'] . "\n";
    }
    echo "\tThe projection type is: " . $value['Projection']['ProjectionType'] . "\n";
}

Query a Global Secondary Index

You can use Query on a global secondary index, in much the same way you Query a table. You need to specify the index name, the query criteria for the index partition key and sort key (if present), and the attributes that you want to return. In this example, the index is PrecipIndex, which has a partition key of Date and a sort key of Precipitation. The index query returns all of the weather data for a particular date, where the precipitation is greater than zero.

The attribute name Date is a DynamoDB reserved word. Therefore, we must use an expression attribute name as a placeholder in the KeyConditionExpression.


$tableName = 'WeatherData';

$response = $dynamodb->query([
    'TableName' => $tableName,
    'IndexName' => 'PrecipIndex',
    'KeyConditionExpression' => '#dt = :v_dt and Precipitation > :v_precip',
    'ExpressionAttributeNames' => ['#dt' => 'Date'],
    'ExpressionAttributeValues' => [
        ':v_dt' => ['S' => '2014-08-01'],
        ':v_precip' ['N' => '0']
    ],
    'Select' => 'ALL_ATTRIBUTES',
    'ScanIndexForward' => true,
]);

foreach ($response['Items'] as $item) {
    echo "Date ---> " . $item['Date']['S'] . "\n";
    echo "Location ---> " . $item['Location']['S'] . "\n";
    echo "Precipitation ---> " . $item['Precipitation']['N'] . "\n";
    echo "\n";
}