DynamoDB Cookbook
上QQ阅读APP看书,第一时间看更新

Listing tables using the AWS SDK for PHP

Now, let's understand how to list all the DynamoDB tables using the AWS SDK for PHP.

Getting ready

You can use the IDE of your choice to code these recipes.

How to do it…

In this recipe, we will learn how to list the tables we created earlier using the AWS SDK for PHP:

  1. Create an instance of the DynamoDB client and invoke the listTables method:
    $client = DynamoDbClient::factory(array(
        'profile' => 'default',
        'region' => 'us-west-2'  
    ));
    $response = $client->listTables();
  2. Iterate over the list to get the details of the tables:
    foreach ($response['TableNames'] as $key => $value) {
           echo "$value" . PHP_EOL;
     }
  3. The AWS SDK for PHP also provides the option to limit the number of tables that are retrieved, and it supports pagination as well. If you have a lot of tables created, then you can fetch the results in the paginated format, as follows:
    $tablesArray = array();
    do {
      $response = $client->listTables(array(
        'Limit' => 5,  // Fetching 5 tables at a time per page 'ExclusiveStartTableName' => isset($response) ? $response['LastEvaluatedTableName'] : null)); 
    
      foreach ($response['TableNames'] as $key => $value) {
        echo "$value" . PHP_EOL;
      }
    
      $tablesArray = array_merge($tablesArray, $response['TableNames']);
    }
    while ($response['LastEvaluatedTableName']);
    
    // Print no. of tables
    
    echo "Total number of tables: ";
    print_r(count($tablesArray));
    echo PHP_EOL;

How it works…

The list table API internally calls DynamoDB services to fetch the details of the tables that you have created.