0
votes

Recently DynamoDB released document types (list or map). See here: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataModel.html#DataModel.DataTypes or here: http://www.allthingsdistributed.com/2014/10/document-model-dynamodb.html

Now am trying to store the following array in DynamoDB

array("key"=>"value")

I am using PHP SDK 2.7.0 and 2.7.2 and 3.0.0 beta (tried all those to solve my problem, every time the same issue). My code below which stores two strings works perfectly:

require ("aws2.7.0/aws-autoloader.php");
use Aws\DynamoDb\DynamoDbClient;
$client = DynamoDbClient::factory(array(
                'key'    => 'KEY',
                'secret' => 'SECRET',
                'region' => 'eu-west-1'
));

$result = $client->putItem(array(
    'TableName' => 'requests-test',
    'Item' => array(
        'messageId'      => array('S' => "test-message1"),
        'data'      => array('S' => "message-data-here"),
        ),
    )
));

Now I am trying to store a simple array with Map data type instead of string:

require ("aws2.7.0/aws-autoloader.php");
use Aws\DynamoDb\DynamoDbClient;
$client = DynamoDbClient::factory(array(
                'key'    => 'KEY',
                'secret' => 'SECRET',
                'region' => 'eu-west-1'
));

$result = $client->putItem(array(
    'TableName' => 'requests-test',
    'Item' => array(
        'messageId'      => array('S' => "test-message1"),
        'data'      => array('M' => array("key"=>"value")),
        ),
    )
));

this results in the following error:

Fatal error: Uncaught Aws\DynamoDb\Exception\DynamoDbException: AWS Error Code: SerializationException, Status Code: 400, AWS Request ID: 8H9URPVBNPCTG4VALA62XXXX3NVV4KQNSO5AEMVJF66Q9ASUAYGX, AWS Error Type: client, AWS Error Message: Expected null, User-Agent: aws-sdk-php2/2.7.0 Guzzle/3.9.2 curl/7.36.0 PHP/5.3.28 thrown in /path/aws2.7.0/Aws/Common/Exception/NamespaceExceptionFactory.php on line 91

AWS PHP SDK 2.7.0 and above should support Map and List type: https://github.com/aws/aws-sdk-php/releases/tag/2.7.0

How can I store arrays in DynamoDB? Is it possible yet with the current SDKs out there - or do they need to release an update to fully support those new data types? Or is the issue in my code?

I'd be really grateful for any comments or possible solutions. Thanks in advance!

2
more information about this new functionality can be found on the aws blog: aws.amazon.com/blogs/aws/dynamodb-update-json-and-moreCamillo

2 Answers

8
votes

In DynamoDB, the value of a document (list or map) should be an attribute value.

'Item' => array(
    'messageId'      => array('S' => "test-message1"),
    'data'      => array('M' => array("key"=> 
                                          array('S' => "value"))),
    ),
)

Similarly in a list, you would have this:

array('L' => array( 
           array('S' => "key"), array('S' => "value")));

I hope this helps!

0
votes

This is in in PHP

I have been with the same problem and thought is somewhat explained here and there, this is what finally worked out

$res =$this->dynamodb->updateItem([
            'TableName' => 'your_table',
            'Key' => [
                    'key1' => ['N' =>  $detail1,
                    'key2' => ['S' => $detail2,
            "ExpressionAttributeNames" => ["#theList" => "my_list_iten_name"],
            "ExpressionAttributeValues" => [
                ':empty_list' => ['L'=>[]],
                ':addVal' => [
                    'L' => [
                        [
                            'M' =>  [
                                'val1' => ['S' => 'one'],
                                'val2' => ['S' => 'two!!!'],
                            ]
                        ],
                        [
                            'M' =>  [
                                'val1' => ['S' => 'one1'],
                                'val2' => ['S' => 'two2!!!'],
                            ]
                        ]
                    ]
                ]
            ],
            'UpdateExpression' => 'SET #theList = list_append(if_not_exists(#theList, :empty_list), :addVal)',
        ]);