Skip to content

Instantly share code, notes, and snippets.

@prufrock
Created April 7, 2012 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prufrock/2332386 to your computer and use it in GitHub Desktop.
Save prufrock/2332386 to your computer and use it in GitHub Desktop.
Performing a between query on an Amazon DynamoDB database.
<?php
//much of this borrowed from the amazon documentation
//http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/LowLevelPHPTableOperationsExample.html
require_once('include.php');
$response = $dynamoDB->query(array(
'TableName' => $properties["table"]["TableName"]
, 'HashKeyValue' => array(
$properties["table"]["KeySchema"]["HashKeyElement"]["AttributeType"]
=> $hashKey )
, 'RangeKeyCondition' => array(
'ComparisonOperator' => AmazonDynamoDB::CONDITION_BETWEEN,
'AttributeValueList' => array(
array(
$properties["table"]["KeySchema"]["RangeKeyElement"]["AttributeType"]
=> (String)($currentTime + 60) )
, array(
$properties["table"]["KeySchema"]["RangeKeyElement"]["AttributeType"]
=> (String)($currentTime + 200))
)
)
));
// 200 response indicates Success
print_r($response);
?>
<?php
//much of this borrowed from the amazon documentation
//http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/LowLevelPHPTableOperationsExample.html
require_once('include.php');
//Create a new DynamoDB table
$response = $dynamoDB->create_table($properties["table"]);
if ($response->isOK()){
echo "Congratz, the table has begun the creation processes." . PHP_EOL;
} else {
echo "Unable to create the table, clearly something went wrong." . PHP_EOL;
print_r($response);
}
$count = 0;
do {
sleep(1);
$count++;
$response = $dynamoDB->describe_table(array(
'TableName' => $properties["table"]["TableName"]
));
}
while ((string) $response->body->Table->TableStatus !== 'ACTIVE');
echo "The table \"{$properties["table"]["TableName"]}\" has been created. (slept ${count} seconds)."
. PHP_EOL;
<?php
//much of this borrowed from the amazon documentation
//http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/LowLevelPHPTableOperationsExample.html
require_once('include.php');
// Get an item
$response = $dynamoDB->get_item(array(
'TableName' => $properties["table"]["TableName"]
, 'Key' => array(
'HashKeyElement' => array(
$properties["table"]["KeySchema"]["HashKeyElement"]["AttributeType"]
=> '1'
)
, 'RangeKeyElement' => array(
$properties["table"]["KeySchema"]["RangeKeyElement"]["AttributeType"]
=> (String)$currentTime
)
)
));
// Check for success...
if ($response->isOK())
{
print_r($response);
} else {
print_r($response);
}
<?php
require_once('sdk.class.php');
$propertiesFile = "prop.js";
$properties = json_decode(file_get_contents("props.js"), TRUE);
//Initialize dynamoDB object
$dynamoDB = new AmazonDynamoDB(Array("key" => $properties["aws_key"]
, "secret" => $properties["aws_secret"]
, "default_cache_config" => "/tmp/"
));
// All items have the same hashKey to ensure they can be
// retrieved by a range queury
$hashKey = '1';
// The current time is static to make query's for the range key
// consistent and reproducible. I artificially push the time
// forward by adding seconds.
$currentTime = 1333856150;
{
"aws_key" : ""
, "aws_secret" : ""
, "aws_cache_dir" : "/tmp/"
, "table" : {
"TableName" : "testTable"
, "KeySchema" : {
"HashKeyElement" : {
"AttributeName" : "Id"
, "AttributeType" : "N"
}
, "RangeKeyElement" : {
"AttributeName" : "Date"
, "AttributeType" : "N"
}
}
, "ProvisionedThroughput" : {
"ReadCapacityUnits" : 5
, "WriteCapacityUnits" : 5
}
}
}
<?php
//much of this borrowed from the amazon documentation
//http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/LowLevelPHPTableOperationsExample.html
require_once('include.php');
function addItemToBatch($dynamoDB, $properties, $queue, $hashKey, $currentTime
, $timeIncrement){
$dynamoDB->batch($queue)->put_item(array(
'TableName' => $properties["table"]["TableName"],
'Item' => array(
$properties["table"]["KeySchema"]["HashKeyElement"]["AttributeName"]
=> array(
$properties["table"]["KeySchema"]["HashKeyElement"]["AttributeType"]
=> $hashKey )
, $properties["table"]["KeySchema"]["RangeKeyElement"]["AttributeName"]
=> array(
$properties["table"]["KeySchema"]["RangeKeyElement"]["AttributeType"]
=> (String)($currentTime + $timeIncrement) )
,'val1' => array( AmazonDynamoDB::TYPE_STRING => 'value1' )
,'val2' => array( AmazonDynamoDB::TYPE_STRING => 'value2' )
,'val3' => array( AmazonDynamoDB::TYPE_STRING => 'value3' )
)
));
}
$timeIncrement1 = 0;
$timeIncrement2 = 60;
$timeIncrement3 = 120;
echo PHP_EOL . PHP_EOL;
echo "# Adding data to the table..." . PHP_EOL;
// Set up batch requests
$queue = new CFBatchRequest();
$queue->use_credentials($dynamoDB->credentials);
//add Items to the batch
addItemToBatch($dynamoDB, $properties, $queue, $hashKey, $currentTime
, $timeIncrement1);
addItemToBatch($dynamoDB, $properties, $queue, $hashKey, $currentTime
, $timeIncrement2);
addItemToBatch($dynamoDB, $properties, $queue, $hashKey, $currentTime
, $timeIncrement3);
// Execute the batch of requests in parallel
$responses = $dynamoDB->batch($queue)->send();
// Check for success...
if ($responses->areOK())
{
echo "The data has been added to the table." . PHP_EOL;
}
else
{
print_r($responses);
}
@ermst4r
Copy link

ermst4r commented Jul 3, 2014

whats in the var: $properties["table"]["KeySchema"]["HashKeyElement"]["AttributeType"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment