0

Can anyone help me to provide the example for update-item to update the specific field using aws dynamodb? I am unable to find any example for update-item but it is not helping me to update the specific item.

For example:

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

For the above query,I am getting the following error:

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

Can anyone help me to resolve this issue?

3 Answers 3

1

I am having Following fields in my table PlayersInfo that i have assign to $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
    ));

This must work.. I have used id and name both in key because they area HASH and RANGE key so we have to specify both of them in key.

Sign up to request clarification or add additional context in comments.

Comments

1

The above example will not work when u will have some null value to your array..

suppose i am retrieving the value from some form

so i have to do the work like this ..

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

so even if there is a null value to any attribute it will not give error because if you are passing any null value to table it will not update and fire an error

That is the most important thing to take care

one more thing i have PlayerId as HASH key and PlayerName as RANGE key

if we are updating with different value in any of this field it will create anew item as they both make composite key

Comments

0

The parameter --key is not just the name of primary key.

The primary key of the item to be updated in JSON format. Each element consists of an attribute name and a value for that attribute.

Look here for more info "http://docs.aws.amazon.com/cli/latest/reference/dynamodb/update-item.html"

For example if I had a table Employee with emp_id as its primary key the update-item should be called as

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.