moh_abk
moh_abk

Reputation: 2164

DynamoDB; Fetch all data in a table - PHP

I found some help in how to get this done via the answer linked here. But it doesn't fit my use case. My goal is to fetch all records in a DynamoDB Table (usernames and emails). Looking through doc I have to use LastEvaluatedKey or ExclusiveStartKey to implement pagination. Any guidance will be appreciated.

Thanks

Upvotes: 1

Views: 2899

Answers (2)

Hassan Raza
Hassan Raza

Reputation: 679

This query will able to get all records from DynamoDB Table.

function scanAllData($table,$limit){

  $result = $this->getClientdb()->scan(array(
        'TableName' => $table,
        'Limit' => $limit,
        'Select' => 'ALL_ATTRIBUTES'                
     ),
    array('limit' => $limit),
  );
    return $result['Items'];
}

You can call this function like this. for example you have 'users' table and columns are usernames and emails

$getobj = $this->scanAllData('users','10');

foreach($getobj as $cols){

   echo $cols['usernames']['S'];
   echo $cols['emails']['S'];    

}

Upvotes: 1

mkobit
mkobit

Reputation: 47269

I don't know PHP, but here is a Java 8 example ran against DynamoDB Local with a dependency on compile group: 'com.amazonaws', name: 'aws-java-sdk', version: '1.10.20'. This shows how Limit (mostly, it doesn't show how filters effect the returned items), LastEvaluatedKey, and ExclusiveStartKey work. You can see how the parameters are set, and compare it to the PHP example:

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.amazonaws.services.dynamodbv2.util.Tables;
import com.google.common.collect.ImmutableList;

import java.util.Map;

public class ScanExample {

    private static final String TABLE_NAME = "test_table";
    private static final String HASH_ATTR_NAME = "hash";

    public static void main(String[] args) throws InterruptedException {
        AWSCredentials awsCredentials = new BasicAWSCredentials("key", "secret");
        AmazonDynamoDB dynamoDBClient = new AmazonDynamoDBClient(awsCredentials);
        dynamoDBClient.setEndpoint("http://localhost:4000");

        if (Tables.doesTableExist(dynamoDBClient, TABLE_NAME)) {
            dynamoDBClient.deleteTable(TABLE_NAME);
        }

        CreateTableRequest createTableRequest = new CreateTableRequest();
        createTableRequest.setTableName(TABLE_NAME);
        createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(50l, 50l));

        createTableRequest.withKeySchema(
                ImmutableList.of(new KeySchemaElement(HASH_ATTR_NAME, KeyType.HASH)))
                .withAttributeDefinitions(ImmutableList.of(
                                new AttributeDefinition(HASH_ATTR_NAME, ScalarAttributeType.N))
                );

        dynamoDBClient.createTable(createTableRequest);
        Tables.awaitTableToBecomeActive(dynamoDBClient, TABLE_NAME);

        final Table table = new DynamoDB(dynamoDBClient).getTable(TABLE_NAME);
        createItems(table, 8);

        final int limit = 3;
        performScan(dynamoDBClient, limit);
    }

    private static void performScan(final AmazonDynamoDB client, final int limit) {
        ScanRequest scanRequest = new ScanRequest(TABLE_NAME)
                .withLimit(limit);

        Map<String, AttributeValue> exclusiveStartKey = null;
        do {
            final ScanResult scanResult = client.scan(scanRequest);
            System.out.println("With exclusiveStartKey=" + exclusiveStartKey);
            scanResult.getItems().forEach(System.out::println);
            exclusiveStartKey = scanResult.getLastEvaluatedKey();
            System.out.println("Result lastEvaluatedKey=" + exclusiveStartKey);
            // Reusing same request object, just setting the start key
            scanRequest.setExclusiveStartKey(exclusiveStartKey);
            System.out.println();
        } while(exclusiveStartKey != null);
    }

    private static void createItems(final Table table, final int n) {
        for (int i = 0; i < n; i++) {
            table.putItem(new Item().withNumber(HASH_ATTR_NAME, i));
        }
    }
}

Example output:

With exclusiveStartKey=null
{hash={N: 2,}}
{hash={N: 1,}}
{hash={N: 3,}}
Result lastEvaluatedKey={hash={N: 3,}}

With exclusiveStartKey={hash={N: 3,}}
{hash={N: 5,}}
{hash={N: 7,}}
{hash={N: 0,}}
Result lastEvaluatedKey={hash={N: 0,}}

With exclusiveStartKey={hash={N: 0,}}
{hash={N: 6,}}
{hash={N: 4,}}
Result lastEvaluatedKey=null

Upvotes: 1

Related Questions