2012-09-02 2 views
0

어떻게 SimpleDB에서 데이터를 가져 옵니까? 내가하고 싶었던 것은 데이터를 넣은 다음 데이터를 가져 오는 것뿐이었습니다. 데이터가 빠져 나오기가 쉽지 않아서 결국 루핑 등으로 추출 할 컴퓨팅 성능이 필요합니다. 맞습니까?PHP를 사용하여 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] => [email protected] 
            ) 

           [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 [email protected] 
            ) 

          ) 

        ) 

       [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 = 신체 -> SelectResult-> Item-> Name 또는 그와 비슷한 것을 선택하십시오.

+0

내가 데이터를 통해 루프, 토큰 루프 안에 다른 루프를 필요가 있다고 생각합니다. 어떤 도움이 필요합니까? – andrebruton

답변

1
$body = $response->body->to_array()->getArrayCopy(); 
echo "ID: " . $msgId . "<br>"; 
//but $msgId is not set 
+0

$ respone-s content print_r ($ respone)이 무엇입니까? –

+0

질문이 업데이트되었습니다. 감사합니다 – andrebruton

2

오래된 Tarzan 코드를 사용하여 알아 냈습니다.

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

우리의 Github https://github.com/andrebruton/SimpleDB-PHP-Example에서 사용 가능한 작업 코드 – andrebruton

3

XPath를 빨리 약 100 배를 수행 할 예정이다 사용 : 이것은 2 차원 배열 (표준 데이터베이스와 유사한 데이터)

여기 작업 코드입니다 작동합니다. 이 코드를 테스트하지 않은,하지만 XPath 식 꽤 가까워 야한다 :

$msgAddress = (string) $response->body->query('//Attribute[Name[text()="msgAddress"]]/Value')->first(); 
관련 문제