Amazon DynamoDB PHP CRUD using aws.phar


Hits: 657  

DynamoDB is fully managed, fast and low cost NoSQL database service by Amazon. DynamoDB allows you to offload the administrative operations and scale highly distributed database cluster.

The AWS SDK PHP enables you to use Amazon Web Services like Amazon S3, EC2, RDS, CloudFront, Redshift, DynamoDB, Push Notification Service (SNS) and more from your PHP code project.

AWS.PHAR Now Download aws-php-sdk Packaged Phar from http://pear.amazonwebservices.com/get/aws.phar

# wget http://pear.amazonwebservices.com/get/aws.phar

You can also download the packaged phar here http://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.phar

Include the SDK in your PHP project require ‘/path/to/aws.phar’;

Now download simple dynamodb-php-wrapper from here https://github.com/masayuki0812/dynamodb-php-wrapper/blob/master/DynamoDBWrapper.php

<?php

require_once 'aws.phar';
require_once 'DynamoDBWrapper.php'; 

$ddb = new DynamoDBWrapper(array(
    'key'    => 'XXXXXXXXXXXXXXXXXXXXX',
    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    'region' => 'us-east-1'
));

createTable

$result = $ddb->createTable(‘Session’, ‘UserId::N’);
// Create a table ‘Session’, which has a hash key ‘UserId’ as NUMBER.

deleteTable

$result = $ddb->deleteTable(‘Session’);

putItem

$item = array(
‘UserId’ => 1,
‘Name’ => ‘Sandeep’,
);
$result = $ddb->put(‘Session’, $item);

updateItem

$key = array(
‘UserId’ => 1,
);
$update = array(
‘Name’ => array(‘PUT’, ‘SVNLabs’)
);
$result = $ddb->update(‘Session’, $key, $update);

deleteItem

$key = array(
‘UserId’ => 1,
);
$result = $ddb->delete(‘Session’, $key);

Query

$keyConditions = array(
‘UserId’ => 1,
);
$result = $ddb->query(‘Session’, $keyConditions);

Scan

$filter = null;

$result = $ddb->scan(‘Session’, $filter, 1000);

Scan with between date filter

$filter = array(
‘userType’ => “member”,
“date” => array(“BETWEEN”, array(“2016/08/01″,”2016/08/17”))
);
$result = $ddb->scan(“Session”, $filter, 1000);

Other Links

https://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html
https://github.com/masayuki0812/dynamodb-php-wrapper/wiki
https://github.com/svnlabs/aws-dynamodb-php

SQL OR NOSQL?


Hits: 3954  

If you are searching for SQL or noSQL solutions, it is very difficult to decide among top SQL / noSQL Databases.

MySQL is the best for multi-document transactions, complex security, complex join, extreme compression.

NoSQL is good for linear scalability, high performance, schema flexibility.

But both depend on data need to manage 😉

Scalability

MySQL support master and slave approach for scaling (vertical scalability).
NoSQL supports horizontal scalability.

Vertical scalability is the ability to increase the capacity of existing hardware or software by adding resources – for example, adding processing power to a server to make it faster. On the other hand, horizontal scalability is the ability to connect multiple entities so that they work as a single logical unit.

Flexibility

SQL is the best for query flexibility, SQL supports both simple and complex queries.
MongoDB supports SQL query and indexes.

Down Time

NoSQL perform well for database size and server performance.

Transactions

SQL is king of transactions.
MongoDB and Redis support limited transactions.

Hosting

Every database solution need deployment, backups, monitoring, and performance tuning.

NoSQL are modern web-scale databases having qualities of non-relational, distributed, open-source and horizontally scalable database.