0

How do I get data out of SimpleDB? All I wanted to do was put data in and then get data out. It looks like the data is not easy to get out and in the end will require computing power to extract by looping etc. Am I correct?

Here is my code to extract the data:

<?php
// Include the SDK
require_once 'sdk.class.php';

// Instantiate
$sdb = new AmazonSDB();

$select_expression = 'SELECT * FROM ' . $domain_name;
$next_token = null;

do {
  if ($next_token) {
    $response = $sdb->select($select_expression, array(
        'NextToken' => $next_token,
      ));
  } else {
    $response = $sdb->select($select_expression);
  }

  // Get Data for row 
  $body = $response->body->to_array()->getArrayCopy();

  echo "ID: " . $msgId . "<br>";

  $next_token = isset($response->body->SelectResult->NextToken)
    ? (string) $response->body->SelectResult->NextToken
    : null;
}
while ($next_token);

echo "<br>";
?>

Here is the extract of the data I'm trying to extract.

Array
(
  [@attributes] => Array
    (
        [ns] => http://sdb.amazonaws.com/doc/2009-04-15/
    )

[SelectResult] => Array
    (
        [Item] => Array
            (
                [0] => Array
                    (
                        [Name] => 1
                        [Attribute] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => msgAddress
                                        [Value] => +2782555555
                                    )

                                [1] => Array
                                    (
                                        [Name] => msgType
                                        [Value] => S
                                    )

                                [2] => Array
                                    (
                                        [Name] => msgSubmitDate
                                        [Value] => 2012-09-02 15:48:46
                                    )

                                [3] => Array
                                    (
                                        [Name] => msgText
                                        [Value] => Test SMS message for ID no 1
                                    )

                            )

                    )

                [1] => Array
                    (
                        [Name] => 2
                        [Attribute] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => msgAddress
                                        [Value] => +27825555555
                                    )

                                [1] => Array
                                    (
                                        [Name] => msgType
                                        [Value] => P
                                    )

                                [2] => Array
                                    (
                                        [Name] => msgSubmitDate
                                        [Value] => 2012-09-02 15:48:46
                                    )

                                [3] => Array
                                    (
                                        [Name] => msgText
                                        [Value] => Test phone message for ID no 2
                                    )

                            )

                    )

                [2] => Array
                    (
                        [Name] => 3
                        [Attribute] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => msgAddress
                                        [Value] => name@domain.co.za
                                    )

                                [1] => Array
                                    (
                                        [Name] => msgType
                                        [Value] => E
                                    )

                                [2] => Array
                                    (
                                        [Name] => msgSubmitDate
                                        [Value] => 2012-09-02 15:48:46
                                    )

                                [3] => Array
                                    (
                                        [Name] => msgText
                                        [Value] => Test email message for ID no 3 to name@domain.co.za
                                    )

                            )

                    )

                [3] => Array
                    (
                        [Name] => 4
                        [Attribute] => Array
                            (
                                [0] => Array
                                    (
                                        [Name] => msgAddress
                                        [Value] => andrebruton
                                    )

                                [1] => Array
                                    (
                                        [Name] => msgType
                                        [Value] => P
                                    )

                                [2] => Array
                                    (
                                        [Name] => msgSubmitDate
                                        [Value] => 2012-09-02 15:48:46
                                    )

                                [3] => Array
                                    (
                                        [Name] => msgText
                                        [Value] => Test push notification message for ID no 4
                                    )

                            )

                    )

            )

    )
)

All I want to get is the following variables:

msgId (the Name), msgType, msgAddress, msgText, msgSubmitDate

If I can get it using $msgType = Body->SelectResult->Item->Name or something like that.

andrebruton
  • 2,268
  • 4
  • 29
  • 36

3 Answers3

3

Using XPath is going to perform about 100x faster. I haven't tested this code, but the XPath expression should be pretty close:

$msgAddress = (string) $response->body->query('//Attribute[Name[text()="msgAddress"]]/Value')->first();
Ryan Parman
  • 6,855
  • 1
  • 29
  • 43
2

I figured it out using some old Tarzan code. This works for a 2 dimensional array (data similar to a standard database)

Here is the working code:

<?php
// Include the SDK
require_once 'sdk.class.php';

// Instantiate
$sdb = new AmazonSDB();

$select_expression = 'SELECT * FROM ' . $domain_name;
$next_token = null;

do {
  if ($next_token) {
    $response = $sdb->select($select_expression, array(
        'NextToken' => $next_token,
    ));
  } else {
    $response = $sdb->select($select_expression);
  }

  // Get Data for row 
  foreach ($response->body->SelectResult->Item as $item) 
    {
        $msgId = $item->Name;
      foreach ($item->Attribute as $attr)
        {
              if ($attr->Name == 'msgAddress') {
                  $msgAddress = $attr->Value;
                }
              if ($attr->Name == 'msgType') {
                  $msgType = $attr->Value;
                }
              if ($attr->Name == 'msgSubmitDate') {
                  $msgSubmitDate = $attr->Value;
                }
              if ($attr->Name == 'msgText') {
                  $msgText = $attr->Value;
                }
        }

      echo "<br>msgId: $msgId<br>";
      echo "msgAddress: $msgAddress<br>";
      echo "msgType: $msgType<br>";
      echo "msgSubmitDate: $msgSubmitDate<br>";
      echo "msgText: $msgText<br><br>";

    }

  $next_token = isset($response->body->SelectResult->NextToken)
    ? (string) $response->body->SelectResult->NextToken
    : null;
}
while ($next_token);

echo "<br>";
?>
andrebruton
  • 2,268
  • 4
  • 29
  • 36
1
$body = $response->body->to_array()->getArrayCopy();
echo "ID: " . $msgId . "<br>";
//but $msgId is not set
Solo Omsarashvili
  • 940
  • 2
  • 11
  • 16