0

Hi want to add continuously to an sub-item which is initially declared empty and later will be updated.

Here is the code -

dynamodb = boto3.resource('dynamodb',endpoint_url='http://localhost:8000')
# Creating the Table
table = dynamodb.create_table(
            TableName='TableN',
            KeySchema=[
                           {
                           'AttributeName': 'name',
                           'KeyType': 'HASH'
                           }
                       ],
            AttributeDefinitions=[

                            {
                                'AttributeName': 'name',
                                'AttributeType': 'S'
                            }
                        ],
            ProvisionedThroughput={
                        'ReadCapacityUnits': 1000,
                        'WriteCapacityUnits': 1000
                        }
        )
# Updating with the Initial Values
table = dynamodb.Table('TableN')
IntValues = {
              "datapoints": [], 
               "attributes": {"host": "server1", "customer": "Ast1"}, 
                "name": "B001_P001"
             }

table.put_item(Item=IntValues)

Now i want to update the "datapoints" with the set of values -

datapoint1 = {"x1":1,"x2":"OK","x3":"23.123"}
datapoint2 = {"x1":3,"x2":"ON","x3":"56.123"}
datapoint3 = {"x1":5,"x2":"OFF","x3":"78.123"}

So that final value should look like -

{
    "name" : "B001_P001",
    "datapoints" : [
        {
          "x1" : 1,
          "x2" : "OK",
          "x3":"23.123"
        },
        {
          "x1" : 1,
          "x2" : "OK",
          "x3":"23.123"
        },
        {
          "x1" : 1,
          "x2" : "OK",
          "x3":"23.123"
        }]
        "attributes": {"host": "server1", "customer": "Ast1"}
}

How can i update the existing sub-item without removing or creating again the entire item ?

1 Answer 1

1

I have used the update_item to update each datapoint-

After the inital update created as shown in question using the put_item, i have used the update_item to update the attributes.

I have used for loop to pass the individual datapoints -

ulist = datapoint1     
table.update_item(
                  Key={'name':'TableN'},
                  UpdateExpression= 'SET #datapoints = list_append(#datapoints,:val1)',
                  ExpressionAttributeNames={'#datapoints': 'datapoints'},
                  ExpressionAttributeValues={':val1':ulist}
                 )
Sign up to request clarification or add additional context in comments.

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.