Saturday, December 26, 2015

Amazon AWS DynamoDB Tutorial PHP

DynamoDB
This is Low level PHP API using dynamodb as Database.

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

date_default_timezone_set('UTC');

use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Exception\DynamoDbException;

$client = DynamoDbClient::factory(array(
    'key' => '#############',
'secret' => '###############',
    'region'  => 'ap-southeast-1',
'version' =>'latest'
));



$t1=$_GET['t1'];
$t2=$_GET['t2'];
$t3=$_GET['t3'];
$getpost=$_GET['gp'];
if($getpost=='POST'){


$response = $client->putItem([
    'TableName' => 'tbl_truck_ori',
    'Item' => [
        'orient_id' => ['N' => LastEvaluatedKey+1],
'test' => ['L' => [['S'=>$t1],['S'=>$t2]]],
'truck_ori' => ['S' => $t3]
    ]
]);
}
else{
$response = $client->scan([
        'TableName' => 'tbl_truck_ori',
        'ProjectionExpression' => 'orient_id, truck_ori',
        'ExpressionAttributeValues' => [
            ':sg' => ['S' => 'sg']] ,
        'FilterExpression' => 'truck_ori = :sg',
]);


foreach ($response['Items'] as $key => $value) {
        echo 'Id: ' . $value['truck_ori']['S'] . "\n<br/>";
}
echo $response['LastEvaluatedKey'];
}

$response = $client->scan([
    'TableName' => 'tbl_truck_ori'
]);

print_r($response['Items']);

foreach ($response['Items'] as $key => $value) {
    echo 'orient_id: ' . $value['orient_id']['N'] . "\n";
    echo 'test: ' . $value['test']['S'] . "\n";
    echo 'truck_ori: ' . $value['truck_ori']['S'] . "\n";
   
    echo "\n<br/>";
}

$response = $client->putItem ([
    'TableName' => $tbl_truck_ori,
    'Item' => [
        'chat_id' => [ 'N' => '101' ],
'orient_id' => ['N' => '130'],
        'owner_id' => [ 'N' => '130' ],
'lat' => [ 'N' => '18.3333'],
'log' => ['73.55555']
    ] 
]);

//multiple data get in dynamdb
$response = $client->scan([
    'TableName' => 'lorry_chat_admin'
]);

foreach ($response['Items'] as $key => $value) {
    echo 'chat: ' . $value['chat_id']['N'] . "\n";
    echo 'owner: ' . $value['owner_id']['S'] . "\n";
}

//get item from dynamodb
$response = $client->getItem(array(
    'TableName' => 'tbl_truck_ori',
    'Key' => array(
       'orient_id' => ['N' => '133']
    )
));
print_r($response['Item']);


//delete Item from dynamodb
$response = $client->deleteItem(array(
    'TableName' => 'ds_chat_admin',
    'Key' => array(
        'chat_id'   => array('N' => '120'),
        'owner_id'  => array('S' => 'Book 120 Title')
    )
));
print_r($response['Item']);

?>

No comments:

Post a Comment

Dharamart.blogspot.in