AWS Dynamodb: Transforming Data with Dynamodb.Update

Asked

Viewed 16 times

1

I would like to know if anyone else needed to make a change to the recorded data in an AWS Dynamodb table where it is necessary to use the original recorded data and change it.

Example:

  I have recorded in a table:

{
    "Name": {
        "S": "John Doe"
    }
}

  after the process needs to stay:

{
    "Name": {
        "S": "John"
    }
}

What I do today is a Get (to get the information), process the data and then a Update to save.

I know that the Update already reads and can transform table elements with the SET, however, I did not reach the conclusion whether within SET it is possible to take the existing information from the bank and process it within the command itself.

1 answer

1

All right?

You probably already solved this Eldad Hauzman, because this post is from 4 months ago, but for those who are coming with this doubt can now benefit. So I’ll try to answer.

I’m kind of new, too, but I think I can help with that.

Important

 1 - A sua tabela deve ter uma chave de partição pelo menos, senão vc não consegue criá-la.
 2 - Não é obrigatório ter atributos adicionais, lembre-se que é um DB orientado a documentos, então só a chave é suficiente para trabalhar com o documento.
 3- Se 'Name' é a chave de particionamento da sua tabela não há como vc alterá-la.

So, if you have a key, for example, the person’s email and a 'Name' attribute, you can do everything by update_item in a single operation.

EXAMPLE:

import boto3

def botoConfig():
    my_config = Config(
        region_name='sa-east-1',
        signature_version='v4',
        retries={
            'max_attempts': 10,
            'mode': 'standard'
        }
    )
    return my_config

dynamodb = boto3.resource('dynamodb', config=botoConfig())
pessoas= dynamodb.Table('pessoas')


def update_name(v_email, v_new_name):
    pessoas.update_item(
        Key={
            'email': v_email,
        },
        UpdateExpression='SET Name= :nm',
        ExpressionAttributeValues={
            ':nm': v__new_name,
        }
    )

I hope I helped! Hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.