Skip to content

Latest commit

 

History

History
159 lines (120 loc) · 3.53 KB

AwsDynamoDbKeyValue.md

File metadata and controls

159 lines (120 loc) · 3.53 KB

AWS DynamoDB

<?php
$dynamodb = \ByJG\AnyDataset\NoSql\Factory::getInstance('dynamodb://access_key:secret_key@region/tablename');

The full connection string can be:

dynamodb://AKA12345678899:aaaaaaaaaaaaaaaaaaaaaaaaa@us-east-1/mytable

You can add any extra arguments supported by the DynamoDB api. You can get a full list here:

One of the most populars is the parameter endpoint where we can set a custom endpoint to access an DynamoDB compatible interface.

An example can be:

s3://AKA12345678899:aaaaaaaaaaaaaaaaaaaaaaaaa@us-east-1/tablename?endpoint=http://localhost:8000

Preparing to use DynamoDb

DynamoDb stores the information slightly different from a model dto structure.

Here an example how DynamoDb requires a model:

[
    'id'      => ['N' => '1201'],
    'time'    => ['N' => $time],
    'error'   => ['S' => 'Executive overflow'],
    'message' => ['S' => 'no vacant areas']
]

and a definition more usual is to have :

[
    'id'      => 1201,
    'time'    => $time,
    'error'   => 'Executive overflow',
    'message' => 'no vacant areas'
]

We will use the second definition. However, every put/get/remove method we will need to set up a list of options to define this data model.

basically we have to create an array with 2 keys:

  • KeyName: Contains the key hame (Currently supports table with only one key)
  • Types: Defines the field names and type

Example:

<?php

$options = [
    "KeyName" => "id",
    "Types" => [
        "id" => "N",
        "time" => "N"
    ]
];

The examples below will use this definition.

Inserting/Updating data

<?php
$dynamodb = \ByJG\AnyDataset\NoSql\Factory::getInstance('dynamodb://....');
$dynamodb->put(
    1201,
    [
        "time" => 1234567899,
        "error" => 'Executive overflow',
        "Message" => "No Vacant Areas"
    ],
    $options  // See above
);

Retrieve a value

<?php
$dynamodb = \ByJG\AnyDataset\NoSql\Factory::getInstance('dynamodb://....');
$value = $dynamodb->get(1201, $options);

/* Should Return:
[
    'id'      => 1201,
    'time'    => $time,
    'error'   => 'Executive overflow',
    'message' => 'no vacant areas'
]
*/

Remove a value

<?php
$dynamodb = \ByJG\AnyDataset\NoSql\Factory::getInstance('dynamodb://....');
$dynamodb->remove(1201);

Listing objects

To get a list of the objects you need to pass an array of options with the keys KeyConditions or ScanFilter.

Example:

<?php
$dynamodb = \ByJG\AnyDataset\NoSql\Factory::getInstance('dynamodb://....');

$options = [
   "KeyConditions" => [
       "id" => [
           "AttributeValueList" => [
               ["N" => "1201"]
           ],
           "ComparisonOperator" => "EQ"
       ]
   ]
];

$iterator = $dynamodb->getIterator($options);
print_r($iterator->toArray());

Check if a key exists

<?php
$dynamodb = \ByJG\AnyDataset\NoSql\Factory::getInstance('dynamodb://....');
if ($dynamodb->has(1201)) {
    echo "exist!";
}

Further reading


Open source ByJG