0

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!

1

2 Answers 2

8

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Raymond. Thank you for your reply, it finally allowed me to store a map inside DynamoDB. However, I am experiencing some issues with querying (scanning) the database based on values INSIDE of the map. See my new question here: stackoverflow.com/questions/26678376/… I am guessing I need to set up some indexes, or am I wrong? Thanks for your help, and hope we can get this working as soon as possible!
0

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)',
        ]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.