Question

What are these constants defined in Amazon DynamoDB?

Can someone please explain the need of these constants? Why are these placed in key of associative array?

I see a strange notation during putting items in table like

'Name'     => array( AmazonDynamoDB::TYPE_STRING => 'Amazon S3')

PHP SDK guide says 4 type of constants.

  • TYPE_ARRAY_OF_NUMBERS
  • TYPE_ARRAY_OF_STRINGS
  • TYPE_NUMBER
  • TYPE_STRING
$dynamodb->batch($queue)->put_item(array(
'TableName' => 'Forum',
'Item' => array(
    'Name'     => array( AmazonDynamoDB::TYPE_STRING => 'Amazon S3'), // Hash Key
    'Category' => array( AmazonDynamoDB::TYPE_STRING => 'Amazon Web Services'), 
    // Range Key
    'Threads'  => array( AmazonDynamoDB::TYPE_NUMBER => '0')
    )
  ));
Was it helpful?

Solution

These constants reflect the four available Amazon DynamoDB Data Types:

String - Strings are Unicode with UTF8 binary encoding. There is no limit to the string size when you assign it to an attribute except when the attribute is part of the primary key. [...]

Number - Numbers are positive or negative exact-value decimals and integers. A number can have up to 38 digits of precision after the decimal point, and can be between 10^-128 to 10^+126. The representation in Amazon DynamoDB is of variable length. [...]

String and Number Sets - Amazon DynamoDB also supports both Number Sets and String Sets. [...] Note that, because it is a set, the values in the set must be unique. String Sets and Number Sets are not ordered; the order of the values returned in a set is not preserved.

You will need to specify or handle these data types in various API calls, e.g. for the KeySchema in CreateTable or the Item in PutItem as seen in the example you provided.

OTHER TIPS

The newest version of the AWS PHP SDK adds convenience methods to help make formatting your request easier. See the docs for the attributes() method. For example:

$dynamodb->put_item(array(
    'TableName' => 'my-table',
    'Item' => $dynamodb->attributes(array(
        'id'            => 1,
        'key1'          => 'value1',
        'key2'          => 'value2',
    ))
));

Seems much easier to work with this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top