banner



How To Write Query For Multiple List In Python Code To Fetch Data From Dynamodb

DynamoDB Python Boto3 Query Cheat Canvas [14 Examples]

Rafal Wilinski

What is Boto3?

Boto3 is a Python library for AWS (Amazon Spider web Services), which helps interacting with their services including DynamoDB - you lot can think of it as DynamoDB Python SDK. It empowers developers to manage and create AWS resources and DynamoDB Tables and Items.

If you're looking for similar guide but for Node.js, y'all tin can find it here, for Coffee, for Rust, and for Golang / Become here.

List of DynamoDB Boto3 Query Examples

  • Connecting Boto3 to DynamoDB
  • Create Table
  • Delete Table
  • List tables
  • Get All Items / Scan
  • Get Detail
  • Batch Become Item
  • Put Item
  • Query Ready of Items
  • Query an Index
  • Update Detail
  • Conditionally Update Item
  • Increment Item Attribute
  • Delete Particular
  • Delete All Items
  • Query with Sorting
  • Query Pagination
  • Run DynamoDB Local

DynamoDB is hard. We get it.

Dynobase's codegen figures out queries and writes code for you!

Dynobase Codegen Feature

Connecting Boto3 to DynamoDB

Connecting to DynamoDB with boto3 is uncomplicated if you want to do that using Access and Undercover Fundamental combination:

        

Keep in mind that using access and secret keys is against best security practices, and yous should instead use IAM roles/policies to interact with DynamoDB. Boto3, if ran on Lamba function or EC2 instance, will automatically consume IAM Role attached to it.

Create Table with Boto3

DynamoDB structures data in tables, so if yous want to save some data to DynamoDB, showtime yous need to create a table. Yous can do that using AWS Panel, AWS CLI or using boto3, similar this:

        

Keep in mind that provisioning a table takes some before it's active. If you lot want to know when information technology'southward ready to be used, yous can use waiter function.

        

Delete table

If you lot changed your listen and need to remove DynamoDB table, don't worry, it'due south simple:

        

List Tables

If you want to check what tables are available at your disposal in current region, use list-tables phone call. Keep in mind that if selected region has more than 100 tables you'll have to paginate through them to fetch a complete list.

        

Boto3 Get All Items aka Browse

To get all items from DynamoDB table, you tin can use Scan functioning. The problem is that Scan has one MB limit on the corporeality of data it will render in a request, so we need to paginate through the results in a loop.

        

Yous can use FilterExpression attribute in gild to filter the results like this:

        

Boto3 Go Item

To get a single item from DynamoDB using Partitioning Key (and Sort Fundamental if using composite key), you can use GetItem performance.

        

Keep in mind to replace primaryKeyName and sortKeyName with bodily keys from your table.

Boto3 Batch Get Item

If you desire to retrieve multiple items identified by a key(s) in 1 call, use batch_get_item phone call with the following syntax:

        

Keep in listen that batch_get_item is express to 100 items and 16 MB of data.

Boto3 Put Item

To write a single particular into the DynamoDB Table, use PutItem operation:

        

Boto3 Query for a fix of items

Alternative way to go a collection of items is the Query method. Query is much faster than Scan considering it uses Indexes. It should exist your preferred style to get a collection of items with the aforementioned partition key.

        

Keep in mind that Query can return up to 1MB of information and you can as well apply FilterExpressions here to narrow the results on non-key attributes.

If you don't know how to construct your Query, use Dynobase with Query Lawmaking Generation feature which will automatically generate information technology for you.

DynamoDB is hard. We become information technology.

Dynobase's codegen figures out queries and writes code for y'all!

Dynobase Codegen Feature

Query an Index

DynamoDB allows querying not only on the principal table alphabetize, but as well on LSIs (Local Secondary Indexes) and GSIs (Global Secondary Indexes).

To do that in SDK, yous demand to modify two things:

  • Specify the index you want to query using IndexName parameter
  • Provide right KeyConditionExpression with corresponding ExpressionAttributeValues and ExpressionAttributeNames`

So, to give you an example. Imagine your table is having a Global Secondary Index called GSI1 with cardinal aspect gsi1pk. If yous'd like to query that index in Node.js, it volition look like this:

        

Boto3 Update Item

DynamoDB update_item operation consists of three master attributes:

  • Key - which object should be updated
  • ExpressionAttributeValues - map with new values
  • UpdateExpression - how these new values should exist applied to the object in the table

They can be used like this:

        

Boto3 Conditionally Update Item

Moreover, you can as well add a ConditionExpression parameter, which restricts the update logic only if the evaluated expression equals truthful.

        

Boto3 Increment Item Attribute

Incrementing a Number value in DynamoDB particular can be accomplished in two ways:

  1. Fetch detail, update the value with code and send a Put request overwriting item
  2. Using update_item operation.

While it might be tempting to employ commencement method because Update syntax is unfriendly, I strongly recommend using second ane because of the fact information technology's much faster (requires simply ane request) and atomic (imagine value updated by other client later on you fetched item).

To do that using unmarried update_item operation, apply following syntax:

        

Boto3 Delete Item

Deleting a single particular from DynamoDB table is similar to GetItem operation. Key statement accepts primary central and sort/range key if table has composite cardinal.

        

Boto3 Delete All Items

Unfortunately, there's no like shooting fish in a barrel style to delete all items from DynamoDB simply like in SQL-based databases by using DELETE FROM my-table;. To accomplish the same effect in DynamoDB, you need to query/browse to become all the items in a table using pagination until all items are scanned and then perform delete operation i-by-i on each record.

        

Fortunately, this is possible just with 3 clicks using Dynobase.

Boto3 Query with Sorting

Unfortunately, DynamoDB offers merely one way of sorting the results on the database side - using the sort central. If your table does not have 1, your sorting capabilities are limited to sorting items in application code after fetching the results. However, if you lot need to sort DynamoDB results on sort key descending or ascending, you can utilise following syntax:

        

Boto3 Query Pagination

Similar to Scan operation, Query returns results up to 1MB of items. If you need to fetch more records, yous need to issue a second phone call to fetch the next folio of results. If LastEvaluatedKey was present in response object, this table has more items like requested and another phone call with ExclusiveStartKey should be sent to fetch more of them:

        

Using Boto3 with DynamoDB Local/Offline

If you need to use DynamoDB offline locally, you tin can use DynamoDB local distributed by AWS or DynamoDB from Localstack. Connecting to it is as easy equally changing the endpoint parameter in boto3.resource call.

        

Learn more almost running and using DynamoDB locally.

Login to the AWS Console less. Use Dynobase.

Showtime 7 days are. No credit card needed.

How To Write Query For Multiple List In Python Code To Fetch Data From Dynamodb,

Source: https://dynobase.dev/dynamodb-python-with-boto3/

Posted by: grunewaldwaragod.blogspot.com

0 Response to "How To Write Query For Multiple List In Python Code To Fetch Data From Dynamodb"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel