samedi 3 octobre 2015

dynamo db $client->getItem() returning extra repeat attribute value after successful $client->updateItem()

I have a DynamoDb Table with a sample item as follows which will be manipulated using a PHP script- AWS sdks for PHP.

f_handle     sub_id         members_json
b_AZVK     1                  {"MEMBERS":[]}

Following are various states on subsequent runs of the script which attempts to add new member to the members_json attribute which is empty by default {"MEMBERS":[]}

1st RUN

$response = $client->getItem ( array (
                    "TableName" => $table_flokk_coll,
                    "ConsistentRead" => true,
                    "Key"       => array(
                        "f_handle"   => array('S' => "b_AZVK"),
                        "sub_id" => array('S' => '1')
                    ),
                    "ProjectionExpression" => "members_json" 
                ));

print_r ( $response["Item"]);

[Item] => Array
                (
                    [members_json] => Array
                        (
                            [S] => {"MEMBERS":[]}
                        )
                )

Adding Member {"uid":"11","name":""}

 $members_json_str = "{"uid":"11","name":""}";
 $response = $client->updateItem ( array (
                        "TableName" => $table_flokk_coll,
                        "Key" => array (
                            "f_handle" => array (
                                "S" => "b_AZVK" 
                            ),
                            "sub_id" => array (
                                "S" => '1' 
                            ) 
                        ),
                        "UpdateExpression" => "set #ATTR1 = :VAL1",
                        "ExpressionAttributeNames" => array (
                            "#ATTR1" => "members_json"                                
                        ),
                        "ExpressionAttributeValues" =>  array (
                            ":VAL1" => array("S" => stripslashes($members_json_str))                                
                        ),                            
                        "ReturnValues" => "ALL_NEW" 
                    ) );
print_r ( $response["Attributes"]);

[Attributes] => Array
                (
                    [members_json] => Array
                        (
                            [S] => {"MEMBERS":[{"uid":"11","name":""}]}
                        )

                    [cluster_id] => Array
                        (
                            [S] => 1
                        )

                    [f_handle] => Array
                        (
                            [S] => b_AZVK
                        )

                    [sub_cluster] => Array
                        (
                            [S] => null
                        )

                    [sub_id] => Array
                        (
                            [S] => 1
                        )

                )

2nd RUN

$response = $client->getItem ( array (
                    "TableName" => $table_flokk_coll,
                    "ConsistentRead" => true,
                    "Key"       => array(
                        "f_handle"   => array('S' => "b_AZVK"),
                        "sub_id" => array('S' => '1')
                    ),
                    "ProjectionExpression" => "members_json" 
                ));

print_r ( $response["Item"]);

[Item] => Array
                (
                    [members_json] => Array
                        (
                            [S] => {"MEMBERS":[{"uid":"11","name":""}]}
                        )
                )

Adding Member {"uid":"12","name":""}

 $members_json_str = "{"uid":"12","name":""}";
 $response = $client->updateItem ( array (
                        "TableName" => $table_flokk_coll,
                        "Key" => array (
                            "f_handle" => array (
                                "S" => "b_AZVK" 
                            ),
                            "sub_id" => array (
                                "S" => '1' 
                            ) 
                        ),
                        "UpdateExpression" => "set #ATTR1 = :VAL1",
                        "ExpressionAttributeNames" => array (
                            "#ATTR1" => "members_json"                                
                        ),
                        "ExpressionAttributeValues" =>  array (
                            ":VAL1" => array("S" => stripslashes($members_json_str))                                
                        ),                            
                        "ReturnValues" => "ALL_NEW" 
                    ) );
print_r ( $response["Attributes"]);

[Attributes] => Array
                (
                    [members_json] => Array
                        (
                            [S] => {"MEMBERS":[{"uid":"11","name":""},{"uid":"12","name":""}]}
                        )

                    [cluster_id] => Array
                        (
                            [S] => 1
                        )

                    [f_handle] => Array
                        (
                            [S] => b_AZVK
                        )

                    [sub_cluster] => Array
                        (
                            [S] => null
                        )

                    [sub_id] => Array
                        (
                            [S] => 1
                        )

                )

3rd RUN

$response = $client->getItem ( array (
                    "TableName" => $table_flokk_coll,
                    "ConsistentRead" => true,
                    "Key"       => array(
                        "f_handle"   => array('S' => "b_AZVK"),
                        "sub_id" => array('S' => '1')
                    ),
                    "ProjectionExpression" => "members_json" 
                ));

print_r ( $response["Item"]);

[Item] => Array
          (
         [members_json] => Array
         (
    [S] => {"MEMBERS":[{"uid":"11","name":""},{"uid":"12","name":""},{"uid":"12","name":""}]}
          )
      )

THE PROBLEM is ABOVE

I have no clue how it is fetching the extra attribute value for {"uid":"12","name":""} which it clearly show was non-existent in the "members_json" after the 2nd run.

Adding Member {"uid":"13","name":""}

 $members_json_str = "{"uid":"13","name":""}";
 $response = $client->updateItem ( array (
                        "TableName" => $table_flokk_coll,
                        "Key" => array (
                            "f_handle" => array (
                                "S" => "b_AZVK" 
                            ),
                            "sub_id" => array (
                                "S" => '1' 
                            ) 
                        ),
                        "UpdateExpression" => "set #ATTR1 = :VAL1",
                        "ExpressionAttributeNames" => array (
                            "#ATTR1" => "members_json"                                
                        ),
                        "ExpressionAttributeValues" =>  array (
                            ":VAL1" => array("S" => stripslashes($members_json_str))                                
                        ),                            
                        "ReturnValues" => "ALL_NEW" 
                    ) );
print_r ( $response["Attributes"]);

[Attributes] => Array
  (
  [members_json] => Array
            (
[S] => {"MEMBERS":[{"uid":"11","name":""},{"uid":"12","name":""},{"uid":"12","name":""},{"uid":"13","name":""}]}
                        )

                    [cluster_id] => Array
                        (
                            [S] => 1
                        )

                    [f_handle] => Array
                        (
                            [S] => b_AZVK
                        )

                    [sub_cluster] => Array
                        (
                            [S] => null
                        )

                    [sub_id] => Array
                        (
                            [S] => 1
                        )

                )



Aucun commentaire:

Enregistrer un commentaire