2014-09-02 139 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>