Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamodb getitem using php - I only want to retrieve the value

I'm able to query my dynamodb tables, but I only want to retrieve the actual value. I don't want the formatting output. This same question has been answered here for Java, but I'm looking for the PHP solution: Retrieving just the item value from a dynamodb table?

Here is my getitem query:

$response = $dynamodb->getItem(array(
    "TableName" => $tableName,
    "ConsistentRead" => true,
    "Key" => array(
        "userguid" => array(Type::STRING => $userguid)
    ),
    "AttributesToGet" => array("token")
));
print_r($response["Item"]["token"]);

Here is the output:

Array
(
    [S] => 9d194513
)

All I want to get back is:

9d194513

I assumed the logical answer would be to change the last line to:

print_r($response["Item"]["token"]["S"]);

But then my code doesn't return anything at all. Obviously still learning PHP here, and any help would be appreciated.

like image 791
user2463882 Avatar asked Jun 07 '13 14:06

user2463882


People also ask

How to retrieve an item in DynamoDB?

Retrieving an item in DynamoDB requires using GetItem, and specifying the table name and item primary key. Be sure to include a complete primary key rather than omitting a portion. For example, omitting the sort key of a composite key. GetItem behaviour conforms to three defaults −

What are getitem parameters in DynamoDB?

These parameters allow you to override the default GetItem behaviour. DynamoDB ensures reliability through maintaining multiple copies of items across multiple servers. Each successful write creates these copies, but takes substantial time to execute; meaning eventually consistent.

What does getitem return in SQL?

GetItem. The GetItem operation returns a set of attributes for the item with the given primary key. If there is no matching item, GetItem does not return any data and there will be no Item element in the response. GetItem provides an eventually consistent read by default.

Is there a difference between get and query in DynamoDB?

There may be a couple a milliseconds differences on the DynamoDB servers themselves as suggested by Chen Harel but these are negligible because of the HTTP request RTT. This said, it's a good practice to issue a GET instead of QUERY when you have enough informations to do so.


2 Answers

Don't use print_r function, just either echo your variables

echo $response["Item"]["token"]["S"];

or store in a variable for later use

$res_token = $response["Item"]["token"]["S"];
like image 144
Fabio Avatar answered Sep 22 '22 18:09

Fabio


You can also use the getPath convenience method built into the Model object that the SDK returns for operations.

echo $response->getPath('Item/token/S');

For more information about working with responses in the SDK, see the Response Models page in the AWS SDK for PHP User Guide.

like image 31
Jeremy Lindblom Avatar answered Sep 20 '22 18:09

Jeremy Lindblom