How to work with different data types in dynamo db in python?

0

I create an item , a python dictonary and use the boto3 client to put the item created. I get an error , with code below that the paid attribute is expected as string . how can i create/update an item with a boolean attribute in python?

       dynamo = boto3.client('dynamo')
        try:
            dynamo.put_item(
                Item={
                    "dob": 1985,
                    "name": title,
                    "info": {"email": abc@xyz.com, "phone": "12093849393"},
                     "paid" : {"BOOL": False}
                }
            )
        except ClientError as err:
            logger.error(err)
gefragt vor 5 Monaten235 Aufrufe
3 Antworten
1

In your code, you are using a mixture of data types with the low level client, which supports DynamoDB-JSON only.

Have a read of this blog post: https://aws.amazon.com/blogs/database/exploring-amazon-dynamodb-sdk-clients/

Using low level client would be:

{'mybool': {'BOOL': False}}

dynamo = boto3.client('dynamodb') #dynamodb not dynamo
try:
       dynamo.put_item(
        TableName='MyTable',
        Item={
            "dob": {"N": "1985"},
            "name": {"S": title},
            "info": {
                "M": {
                    "email": {"S": "abc@xyz.com"},
                    "phone": {"S": "12093849393"}
                }
            },
            "paid": {"BOOL": False}
        }
    )
except ClientError as err:
     logger.error(err)

High level client:

'mybool' : False

dynamo = boto3.client('dynamodb') #dynamodb not dynamo
table = dynamo.Table('MyTable')
try:
       dynamo.put_item(
        Item={
      "dob": 1985,
      "name": "title",
      "info": {
           "email": "abc@xyz.com",
           "phone": "12093849393"
       },
        "paid": False
     }
    )
except ClientError as err:
     logger.error(err)

profile pictureAWS
EXPERTE
beantwortet vor 5 Monaten
0

Hello.

I have checked the following documents.
I confirmed that I can register it with DynamoDB by modifying the code as below in my environment.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb/client/put_item.html

    dynamo.put_item(
        TableName='test',  # Please specify a TableName
        Item={
            "dob": {"N": "1985"},
            "name": {"S": title},
            "info": {
                "M": {
                    "email": {"S": "abc@xyz.com"},
                    "phone": {"S": "12093849393"}
                }
            },
            "paid": {"BOOL": False}
        }
    )
profile picture
EXPERTE
beantwortet vor 5 Monaten
profile pictureAWS
EXPERTE
überprüft vor 5 Monaten
  • @Riku - False specified in the book value , is python right ? How does dynamo db deserialize it ?

  • I didn't have an environment to run boto3 in my local environment, so I executed it with Lambda.

    import boto3
    
    dynamo = boto3.client('dynamodb')
    title = "Test Title"
    
    def lambda_handler(event, context):
        dynamo.put_item(
            TableName='test',  # Please specify a TableName
            Item={
                "dob": {"N": "1985"},
                "name": {"S": title},
                "info": {
                    "M": {
                        "email": {"S": "abc@xyz.com"},
                        "phone": {"S": "12093849393"}
                    }
                },
                "paid": {"BOOL": False}
            }
        )
    
-1

In your code, you should not use the dictionary with "BOOL" as the key for a boolean attribute. The "BOOL" key is not required when specifying boolean values in DynamoDB.

Here's how you can update the code to create/update an item with a boolean attribute in Python:

dynamo = boto3.client('dynamo')
        try:
            dynamo.put_item(
                Item={
                    "dob": 1985,
                    "name": title,
                    "info": {"email": abc@xyz.com, "phone": "12093849393"},
                    "paid" : False
                }
            )
        except ClientError as err:
            logger.error(err)
beantwortet vor 5 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen