31
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DynamoDBのjsonサポートを試してみた。

Last updated at Posted at 2014-10-09

新しく追加された型

キー名 データ型 説明
L List jsonの配列のような順序付きの集合
M Map jsonのobjectのような、順序を持たないkey-valueのペアの集合
BOOL Boolean true か false
NULL Null

<?php
require_once 'vendor/autoload.php';

use Aws\DynamoDb\DynamoDbClient;

$dynamo = DynamoDbClient::factory(array(
    'key'    => AWS_KEY,
    'secret' => AWS_SECRET,
    'region' => AWS_REGION,
));
$id = (string) time();

$result = $dynamo->putItem(
                           [
                            'TableName' => 'new_sdk_test',
                            'Item'      => [
                                            'id'         => ['S' => $id],
                                            'value_list' => ['L' => [['S'=>'Tokyo'], ['S' => 'Kanagawa'], ['S' =>'Saitama']]],
                                            'value_map'  => ['M' => ['name' => ["S" => "John"], 'age' => ["N" => 20]]],
                                            'value_bool' => ['BOOL' => true],
                                            // 'value_null' => ['NULL' => null], // なぜかエラー
                                            ],
                            'ReturnConsumedCapacity' => 'TOTAL'
                            ]
                           );


$args = array(
        'TableName' => 'new_sdk_test',
        'Key' => array(
                       'id' => array('S' => $id),
        ),
        'ConsistentRead' => true,
 );
$result = $dynamo->getItem($args);

print_r($result);

出力結果


Guzzle\Service\Resource\Model Object
(
    [structure:protected] =>
    [data:protected] => Array
        (
            [Item] => Array
                (
                    [id] => Array
                        (
                            [S] => 1412874519
                        )

                    [value_map] => Array
                        (
                            [M] => Array
                                (
                                    [name] => Array
                                        (
                                            [S] => John
                                        )

                                    [age] => Array
                                        (
                                            [N] => 20
                                        )

                                )

                        )

                    [value_bool] => Array
                        (
                            [BOOL] => 1
                        )

                    [value_list] => Array
                        (
                            [L] => Array
                                (
                                    [0] => Array
                                        (
                                            [S] => Tokyo
                                        )

                                    [1] => Array
                                        (
                                            [S] => Kanagawa
                                        )

                                    [2] => Array
                                        (
                                            [S] => Saitama
                                        )

                                )

                        )

                )

        )

)

なんかこれ、json なのに、いちいち型を "N" とか "S" とか指定しないといけないのが
超だるいな…

返ってきた値もjsonじゃないし。コレジャナイ感。

補足1(2014/10/14)

NULLを登録するには、NULL => true とすると登録できた。
これが正しい挙動なのかは不明。(どこかにドキュメントに記載あったら教えて下さい)

$data = [
 'TableName' => 'new_sdk_test',
 'Item'      => [
                 'id'         => ['S' => $id],
                 'value_null' => ['NULL' => true]
                 ],
 'ReturnConsumedCapacity' => 'TOTAL'
 ];

$result = $dynamo->putItem($data);

ちなみに、getItem すると、 NULL => 1 みたいに返ってきて、何も考えずに使うとアプリ側で意図しないバグを生みそうな気がする。

31
32
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?