I want to write service in php where -
1) DynamoDB will have table t with two columns key and val
2) I will check if some key exists in table t or not.
3) If exist read data .. if don't exist insert new key-value in table t
I was checking some links http://docs.aws.amazon.com/AWSSDKforPHP/latest/index.html#m=AmazonDynamoDB/put_item http://docs.aws.amazon.com/aws-sdk-php/guide/latest/quick-start.html
Which one to follow ?
Also can someone give me quick example and exact syntax.
Thanks in advance.
You now can use a SQL-compatible query language to query, insert, update, and delete table data in Amazon DynamoDB. You now can use PartiQL (a SQL-compatible query language)—in addition to already-available DynamoDB operations—to query, insert, update, and delete table data in Amazon DynamoDB.
Amazon DynamoDB supports PartiQL , a SQL-compatible query language, to select, insert, update, and delete data in Amazon DynamoDB.
The full walkthrough is located HERE. It gives you a step by step outline of the setup process for credentials and comes with a easy to use add on to the PHP SDK for AWS
AWS Setup - AWS doesn't lay out the Credentials setup for you set by step, so I will.1. Go to AWS and get your PUBLIC_KEY and PRIVATE_KEY
2. Open Terminal
3. If you haven't created your credentials yet, in Terminal's fresh page, type in:
nano ~/.aws/credentials
nano
function page will open up. You will see GNU nano 2.0.6...
at the top.4. Inside the nano
page, type in:
[default]
aws_access_key_id = public_key_ABCDEFGHIJKLMNOPQRSTUVWXYZ
aws_secret_access_key = private_key_s0m3_CR42Y_l3tt3rS_i5y0ur53cr3tK3y
5. Once you've typed it out, hit CONTROL + X (Yes...Control, not Command).
2. At the top of the file using the SDK (index.php
or whatever), put in:
require 'aws/aws-autoloader.php';
date_default_timezone_set('America/New_York');
use Aws\DynamoDb\DynamoDbClient;
$client = new DynamoDbClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => 'latest'
]);
The Basic Methods
Describe Table
$result = $client->describeTable(array(
'TableName' => '[Table_Name]'
));
echo $result;
Put Item
$response = $client->putItem(array(
'TableName' => '[Table_Name]',
'Item' => array(
'[Hash_Name]' => array('S' => '[Hash_Value]'),
'[Range_Name]' => array('S' => '[Range_Value]')
)
));
//Echoing the response is only good to check if it was successful. Status: 200 = Success
echo $response;
Get Item
$response = $client->getItem(array(
'TableName' => '[Table_Name]',
'Key' => array(
'[Hash_Name]' => array('S' => '[Hash_Value]'),
'[Range_Name]' => array('S' => '[Range_Value]')
)
));
echo $response;
Delete Item
$response = $client->deleteItem(array(
'TableName' => '[Table_Name]',
'Key' => array(
'[Hash_Name]' => array('S' => '[Hash_Value]'),
'[Range_Name]' => array('S' => '[Range_Value]')
)
));
//Echoing the response is only good to check if it was successful. Status: 200 = Success
echo $response;
Query Item
$response = $client->query(array(
'TableName' => '[Table_Name]',
'KeyConditionExpression' => '[Hash_Name] = :v_hash and [Range_Name] = :v_range',
'ExpressionAttributeValues' => array (
':v_hash' => array('S' => '[Hash_Value]'),
':v_range' => array('S' => '[Range_Value]')
)
));
echo $response;
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With