Introducing Developing With Python and DynamoDB

Introducing Developing With Python and DynamoDB

Amazon Web Services is arguably one of the biggest cloud infrastructure providers in the world. Lots of organizations and companies around the globe make use of the AWS platform for their cloud services, and one of the reasons they go with AWS is because of DynamoDB and several other solutions they offer.

We know programmers love Python for a lot of solid reasons too. Python spares them the need to write loads of declarations, offers tons of data structures, and removes the need for semicolons and braces. 

Now imagine adding all of these exciting benefits with Amazon’s NoSQL key-value-supporting database – DynamoDB. In this article, you’ll find out what DynamoDB and Python Dynamodb are, including the main functionalities of Python DynamoDB, how it’s connected to AWS Python and much more.

Boto3 Resource Dynamodb

The Amazon Web Services’ Boto3 Dynamodb library is typically used to integrate Python applications with different AWS services. Clients and resources are two of the most commonly used features of Boto3 DynamoDB. 

Meanwhile, for the Boto3 Resource DynamoDB, resources mean there is a higher-level abstraction when compared to clients. Resources are generated from resource description, JSON, that is in the boto library. Besides, in Boto3 Resource Dynamodb, resources give an object-based interface when interacting with different AWS services.

What Is DynamoDB?

DynamoDB is a super fast and incredibly flexible NoSQL database service. Amazon Web Service (AWS) offers this superb database service for web apps, gaming, mobile apps, and even IoT devices. 

Amazon DynamoDB is great for developers who need to build serverless and more modern applications with the potential for global scalability while supporting millions of read and write requests every second and petabyte of data. 

What’s more, DynamoDB is built to run high-performing, internet-wide applications. Existing traditional relational databases would’ve overburdened these applications.

Main Concepts of DynamoDB

Several concepts define DynamoDB as a database service. From table to query to scan, here are some of the main concepts of DynamoDB:

  • Table: The table comes as a collection capable of holding virtually infinite numbers of unique items while allowing for secondary indices.
  • Item: As a main concept of DynamoDB, items are its most basic unit. Items hold data attributes that are structured in JSON.
  • Secondary Index: Secondary indices duplicate the table items using different sort-key and primary-key.
  • Streams: It is a constant stream of state-altering operations executed against a table. 
  • Attribute: An attribute is a key-value pair containing informational data points about a particular item in the database table. 
  • Query: It is an operation to retrieve a specific item or set of items. 
  • Sort Key: It is a special attribute form essential in organizing items in a different order of sorting. 
  • Primary Key: Primary key is another special form of the attribute used in referencing items. 
  • Filter: Filters are applicable rules you can use after executing a scan or query while doing so before results are returned to the requester. 
  • Scan: Operation to scan the whole table or a section or part of it.

What Are the Main Functionalities of DynamoDB?

DynamoDB allows users to offload the administrative workload of scaling and operating a distributed database. Users don’t have to worry about hardware provisioning, configuration & setup, replication, cluster scaling, or even software patching. Here are some of the main functionalities of DynamoDB: 

#1. Scalability

DynamoDB offers extreme scalability. Its automatic partitioning model spreads data across partitions while raising throughput as data volume grows. The user does not need to intervene for this to be possible.

#2. Three Basic Data Model

DynamoDB makes use of three basic data model units: Items, Tables, and Attributes. In simple terms, tables are a collection of items while items are a collection of attributes. Attributes are like key-value pairs and are basic units of information.

Tables on the other hand are like tables in relational databases but without the fixed schemas usually associated with them. Items are similar to rows in the RDBM table but with DynamoDB you need a primary key. AWS DynamoDB supports two types of primary keys: hash and range type primary key and hash type primary key.

#3. Predictable Performance

Amazon DynamoDB has predictable performance according to AWS. The DynamoDB should be able to deliver extremely predictable performance if Amazon’s reputation for service delivery is anything to go by. Users get to control the quality of service they get when they choose between Eventual Consistency and Strong Consistency (Read-after-Write).

More so, DynamoDB offers Provisioned Capacity. With this, users can bank at least 5 minutes of unused capacity which comes in handy during short bursts of activity.

#4. DynamoDB Index

Local Secondary Index (LSI) and Global Secondary Index (GSI) are the two types of indexes in AWS DynamoDB. You need a mandatory range key for an LSI while a GSI requires a hash range key or a hash key. What’s more, GSI’s are placed in separate tables, spanning multiple partitions and DynamoDB supports up to 5 global secondary indexes. Overall, you need to judiciously select your hash key while you create a GSI. This is because the key you choose will be used for partitioning.

#5. DynamoDB Partitions

With DynamoDB, a hash key automatically partitions data and so you will need to select a hash key when implementing a GSI. Throughput and table size are two factors that determine the partitioning logic.

Do you have any additional questions about how DynamoDB works?

At Innuy, we have a team of experts that can help you. Feel free to drop us a line to receive a relevant consultation.



Get in touch

Here’s How It’s Connected To AWS Python

Python DynamoDB users require a basic understanding of NoSQL databases and solid experience with Python. In addition to that, here are some other things you need to set up DynamoDB Python:

  • DynamoDB Local: You need to download and configure DynamoDB before being able to connect and set up Python DynamoDB. 
  • Python: You need to try downloading and installing Python for python dynamodb. It is ideal to go for Python version 2.7 or later versions and you can download it from Python’s website.
  • IDE: Lastly, for your DynamoDB python, there’s a need to use a code editor that you prefer or an integrated development environment.

Steps To Connect Dynamo To Python and Set Up Python DynamoDB 

Step 1

First, to set up Dynamodb Python, you need to download and install the Java SE. You need at least version 8.x or a much newer version of Java Runtime Environment (JRE) to run DynamoDB on your computer. It doesn’t run on the earlier versions of JRE.

 Step 2

Download and install the AWS CLI Installer. After that, type this command aws –version in the command prompt to verify your installation. Getting an error means you may need to add a Path variable.

Step 3

The next step to setting up Python DynamoDB is downloading and extracting the Amazon DynamoDB. After this, go to the folder where you made the extraction and type this command java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb in a command prompt.

Step 4

Next, try to configure credentials by typing in the aws configure command in a new command prompt.

Step 5

After the above step, type in this aws dynamodb list-tables –endpoint-url http://localhost:8000 as the next command. Doing so should bring back an empty list of tables if you don’t already give existing ones. 

Here’s How You Can Alternatively Setup Amazon DynamoDB As a Web Service

To connect to your DB via Python, you need to first set up and activate a virtual environment with this command:

/* Install virtual environment */

pip install virtualenv

/* Create a virtual environment */

python -m virtualenv venv 

/* If the above doesn’t work, try the following */

python -m venv venv

/* Activate the virtual environment */

venv/Scripts/activate

Next, use the boto3 module, so you interact with the DynamoDB’s local instance

pip install boto3

After that, you will have to import the library. In addition to this, you also need to create a database object with this: import boto3

Next, create a class and add the CRUD operations as the methods of the class.

class dtable:

    db = None

    tableName = None

    table = None

    table_created = False

def __init__(self):

    self.db  = boto3.resource(‘dynamodb’,

    endpoint_url=”http://localhost:8000″)

    print(“Initialized”)

You will need to test your code by creating an instance of the class

if __name__ == ‘__main__’:

  movies = table()

Do you need an Experienced team to get started?

Don’t hesitate to drop us a line and schedule a consultation.



Get in touch

Conclusion

Python DynamoDB is one of the best choices for web apps, mobile apps, gaming, and IoT devices. What’s more, Python has excellent support for DynamoDB. Amazon’s DynamoDB is great at providing NoSQL services with a solid, consistent, and predictable performance. 

It saves users from the difficulties encountered when they use a distributed system. If you’re looking to build your next big internet-based scale application, Python DynamoDB, along with a strong ecosystem, will be massive.

Leave a Reply

Your email address will not be published. Required fields are marked *