2014-09-02 114 views
0

任何人都可以幫助我提供update-item的示例使用aws dynamodb更新特定字段嗎?我無法找到update-item的任何示例,但它不幫助我更新特定項目。使用亞馬遜dynamodb更新項目

例如:

aws dynamodb update-item --table-name x --key y --attribute-updates "z #abc" --endpoint-url://localhost:8000 

以上查詢,我收到以下錯誤:

Error parsing parameter '--key': Invalid Json : y 

誰能幫我解決這個問題?

回答

1

我有我的表PlayersInfo 以下字段,我必須分配給$ newtablename

PlayerId-> HASH PlayerName-> RANGE PlayerPrice PlayerType PlayerNationality

$response = $client->updateItem(array(
     "TableName" => $newtablename, 
     "Key" => array(
      "PlayerId" => array('N' => 1), 
      "PlayerName" => array('S' => "Virat Kohli") 
     ), 
     "AttributeUpdates" =>array("PlayerPrice"=>array("Value" => array('N'=>1000)),array("PlayerType"=>array("Value" => array('S'=>"Batsman")),array("PlayerNationality"=>array("Value" => array('S'=>"India")), 
); 

     "ReturnValues" => \Aws\DynamoDb\Enum\ReturnValue::ALL_NEW 
    )); 

這必須工作.. 我在密鑰中使用了id和name,因爲它們分別是HASH和RANGE鍵,所以我們必須在key中指定它們。

1

上面的例子將不工作當u會有一些空值的數組..

假如我是某種形式的

檢索值,所以我必須做這樣的工作..

if (!empty($_POST['update'])) { 


    $id = intval($_POST['id']); 
    $name = strval($_POST['name']); 
    $type = strval($_POST['type']); 
    $nationality = strval($_POST['nationality']); 
    $price = intval($_POST['price']); 


    if(!empty($price)) 
    { 

     $value['PlayerPrice']=array(
       "Value" => array('N' => $price) 
      ); 
    } 
    if(!empty($type)) 
    { 

     $value['PlayerType']=array(
       "Value" => array('S' => $type) 
      ); 
    } 
    if(!empty($nationality)) 
    { 

     $value['PlayerNationality']=array(
       "Value" => array('S' => $nationality) 
      ); 
    } 

    print_r($value); 

    $response = $client->updateItem(array(
     "TableName" => $newtablename, 
     "Key" => array(
      "PlayerId" => array('N' => $id), 
      "PlayerName" => array('S' => $name) 
     ), 
     "AttributeUpdates" =>$value, 

     "ReturnValues" => \Aws\DynamoDb\Enum\ReturnValue::ALL_NEW 
    )); 
    echo "Record Updated"; 
} 

所以即使有一個空值的任何屬性它不會給錯誤,因爲如果你傳遞任何空值表也不會更新,並觸發一個錯誤

這是最重要的事情是照顧

一件事我有PlayerId哈希鍵和PlayerName作爲RANGE鍵

如果我們用不同的值在任何這一領域它會創建重新項的更新因爲他們都製作複合鍵

0

參數- 鍵不只是主鍵的名稱。

要以JSON格式更新的項目的主鍵。每個元素由屬性名稱和該屬性的值組成。

看看這裏的更多信息「http://docs.aws.amazon.com/cli/latest/reference/dynamodb/update-item.html

例如,如果我有一個表員工emp_id爲作爲其主鍵更新項目應被稱爲

aws dynamodb update-item --table-name Employee --key '{ "emp_id" : { "N" : "006" } }' --attribute-updates <value>