Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to check if table exists in DynamoDB?

What is the best way to check if table exists in DynamoDb?

I would appreciate it if the code would be in PHP.

Either active or not.

* Added later as an example to various cases for error code 400

It's very easy to check if the table exist, it can have one of the following TableStatus => CREATING, ACTIVE, DELETING or UPDATING

but in case i get error 400 it can mean more than one thing.

1) sent null string as a table name by mistake.

[x-aws-body] => {"TableName":""} )

[body] => CFSimpleXML Object
    (
        [__type] => com.amazon.coral.validate#ValidationException
        [message] => The paramater 'tableName' must be at least 3 characters long and at most 255 characters long
    )

[status] => 400

2) syntax error in the command sent to DynamoDB, for example writting tabel_name instead of table_name.

[x-aws-body] => {"TabelName":"test7"} )

[body] => CFSimpleXML Object
    (
        [__type] => com.amazon.coral.validate#ValidationException
        [message] => The paramater 'tableName' is required but was not present in the request
    )

[status] => 400

3) I would guess but didn't check, if I exceed at that same time the provisioned capacity on the table.

like image 833
Wiz Cat Avatar asked Sep 05 '12 07:09

Wiz Cat


People also ask

Which is the most efficient operation to retrieve data from a DynamoDB table?

GetItem – Retrieves a single item from a table. This is the most efficient way to read a single item because it provides direct access to the physical location of the item. (DynamoDB also provides the BatchGetItem operation, allowing you to perform up to 100 GetItem calls in a single operation.)

Does DynamoDB have tables?

In DynamoDB, tables, items, and attributes are the core components that you work with. A table is a collection of items, and each item is a collection of attributes. DynamoDB uses primary keys to uniquely identify each item in a table and secondary indexes to provide more querying flexibility.


1 Answers

You can have a look at "describe_table" of the official PHP SDK. 400 means "does not exist" There is a pretty extensive example in the official documentation. Look at how it is used in the "delete" example, right at the bottom.

http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/LowLevelPHPTableOperationsExample.html

Here is the (stripped) example from the doc

<?php
require_once dirname(__FILE__) . '/sdk/sdk.class.php';

$dynamodb = new AmazonDynamoDB();
$table_name = 'ExampleTable';
$response = $dynamodb->describe_table(array('TableName' => $table_name));

if((integer) $response->status !== 400)
{
    $error_type = $response->body->__type;
    $error_code = explode('#', $error_type)[1];
    if($error_code == 'ResourceNotFoundException')
    {
        echo "Table ".$table_name." exists.";
    }
}
?>
like image 81
yadutaf Avatar answered Nov 15 '22 22:11

yadutaf