Amazon web services 使用amazon dynamodb更新项目

Amazon web services 使用amazon dynamodb更新项目,amazon-web-services,Amazon Web Services,是否有人可以帮助我提供updateitem的示例,以使用aws dynamodb更新特定字段?我找不到更新项目的任何示例,但它无法帮助我更新特定项目 例如: aws dynamodb update-item --table-name x --key y --attribute-updates "z #abc" --endpoint-url://localhost:8000 对于上述查询,我得到以下错误: Error parsing parameter '--key': Invalid Json

是否有人可以帮助我提供
updateitem
的示例,以使用aws 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

有人能帮我解决这个问题吗?

我的table PlayerInfo中有以下字段 我已分配给$newtablename的

PlayerId->HASH PlayerName->RANGE 玩家价格 播放类型 游戏性

$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
    ));
这一定行。。
我在key中使用了id和name,因为它们是区域散列和范围键,所以我们必须在key中同时指定它们。

当数组中有一些空值时,上面的示例将不起作用

假设我正在从某种形式检索值

所以我必须这样做

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作为范围键

如果我们在任何一个字段中使用不同的值进行更新,它将创建一个新项,因为它们都是复合键

参数--key不仅仅是主键的名称

要以JSON格式更新的项的主键。每个元素由属性名和该属性的值组成

请查看此处以获取详细信息“”

例如,如果我有一个emp_id作为主键的表Employee,那么更新项应该被称为

aws dynamodb update-item  --table-name Employee --key '{ "emp_id" : { "N" : "006" } }' --attribute-updates <value>
aws dynamodb更新项--表名Employee--键“{”emp_id:{”N:“006”}”--属性更新