0

SimpleDB からデータを取得するにはどうすればよいですか? 私がやりたかったのは、データを入れてからデータを取り出すことだけでした。データを取り出すのは簡単ではないようで、最終的にはループなどで抽出するための計算能力が必要になります。私は正しいですか?

データを抽出するコードは次のとおりです。

<?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>";
?>

これが、抽出しようとしているデータの抜粋です。

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
                                    )

                            )

                    )

            )

    )
)

取得したいのは、次の変数だけです。

msgId (名前)、msgType、msgAddress、msgText、msgSubmitDate

$msgType = Body->SelectResult->Item->Name などを使用して取得できる場合。

4

3 に答える 3

3

XPath を使用すると、約 100 倍速く実行されます。私はこのコードをテストしていませんが、XPath 式はかなり近いはずです。

$msgAddress = (string) $response->body->query('//Attribute[Name[text()="msgAddress"]]/Value')->first();
于 2012-10-05T07:55:48.203 に答える
2

古いTarzanコードを使ってそれを見つけました。これは 2 次元配列 (標準データベースと同様のデータ) に対して機能します。

作業コードは次のとおりです。

<?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>";
?>
于 2012-09-02T16:44:42.707 に答える
1
$body = $response->body->to_array()->getArrayCopy();
echo "ID: " . $msgId . "<br>";
//but $msgId is not set
于 2012-09-02T15:32:49.807 に答える