Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamodb get_item and put_item without data types in python

I currently have a python script that looks like:

import boto3
...
response = dynamodb.get_item(
  TableName = dynamodb_table_name,
  Key = {
    "snippet_id": {
      "S": snippet_id
    }
  }
)
if "Item" in response:
    item = response["Item"]
    print(json.dumps(item, indent=4, cls=DecimalEncoder))

This prints something akin to:

{
    "var_1": {
        "BOOL": false
    }, 
    "var_2": {
        "S": "Text"
    }, 
    "snippet_id": {
        "S": "3a97e45c-ffed-4c76-8bb4-b2a32f49a5d2"
    }
}

Any idea how to do the type detection and return:

{
    "var_1": False, 
    "var_2": "Text",
    "snippet_id": "3a97e45c-ffed-4c76-8bb4-b2a32f49a5d2"
}

Also, can this be done for the query as well?

like image 895
Optimus Avatar asked Jul 31 '17 17:07

Optimus


People also ask

Which of the data type is not supported by DynamoDB?

Unlike conventional relational databases, DynamoDB does not natively support a date and time data type. It can be useful instead to store data and time data as a number data type, using Unix epoch time.

What are three types of data types offered by DynamoDB?

DynamoDB uses three basic data model units, Tables, Items, and Attributes. Tables are collections of Items, and Items are collections of Attributes.

How does boto3 connect to DynamoDB?

Connecting AWS resources to the python environment requires a boto3 package. Creating a dynamo DB client is a connection instance that lets us connect with our dynamo DB service. We need to specify region_name , aws_access_key_id , aws_secret_access_key in order to connect with our dynamoDb service.


1 Answers

TLDR

Use resource instead of client.

Summary

In essence, you can call boto3.client() or boto3.resource().

Client returns dynamoDB syntax, which looks like this:

'var_1' : {'S':"string"}

Resource returns normal syntax, which looks like this:

'var_1' : "string"

Further Reading

At its core, all that Boto3 does is call AWS APIs on your behalf. For the majority of the AWS services, Boto3 offers two distinct ways of accessing these abstracted APIs:

  • Client: low-level service access
  • Resource: higher-level object-oriented service access
  • Reference: https://realpython.com/lessons/clients-and-resources/
like image 142
Gunt.r Avatar answered Oct 02 '22 20:10

Gunt.r