greenway
greenway

Reputation: 111

How to insert list of objects in 1 write to DynamoDB using Python?

Hi I am trying to generate a table that will have all the taxi trips of 1 day. One of the attributes of 1 taxi trip would include a list of all the discounts that that were used on that trip.

There can be any number of discounts used. What I am not sure how to do is how can I include that list of discounts into one item attribute? So basically write 1 item with a list of attributes into DynamoDB?

Here is my table:

def create_table(self):
        table = dynamodb.create_table(
            TableName='TaxiTable',
            KeySchema=[
                {
                    'AttributeName': 'trip',
                    'KeyType': 'HASH'
                },
            ],
            AttributeDefinitions=[
                {
                    'AttributeName': 'trip',
                    'AttributeType': 'S'
                },

            ],
            ProvisionedThroughput={
                'ReadCapacityUnits': 10,
                'WriteCapacityUnits': 10
            }
        )

        print("Table status:", table.table_status)

The 'trip' key would be 1 long string with the origin of that taxi ride, where it's last stop of the day was at, the taxi number, the number of stops, and the date of the trip.

trip_key: 12thAve.MapleDrive.0124.02.02202020

underneath as an attribute I would like a list of all the discounts that were used on that taxi trip/ Something like:

trip_key: 12thAve.MapleDrive.0124.02.02202020
discount_map: 
{
  discount1: { name: 'Senior', total_saved: '10'},
  discount2: { name: 'Student', total_saved: '30'},
  discount3: { name: 'Employee', total_saved: '20'},
  discount4: { name: 'Hotel', total_saved: '30'}
}

I do not know how many discounts could be used on one trip. Could be between 5-10. But I would like to include all the discounts used in one insert. I would want to query a specific discount used on a taxi trip and the total saved from this table.

I dont know if first I should convert the list into a JSON object? Or if there is a way to iterate through the list and insert it how I would like.

import boto3

class taxi_discount:
    name = None
    total_saved = None

# rest of logic for this class...


class insert_to_dynamoDb:

    dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")

    taxi_trip_key= None            
    taxi_discounts_list = []

    def __init__(taxi_trip, discount_list):
            self.taxi_trip_key= taxi_trip
            self.discount_list = discount_list



    def write_to_table(self):
            table = dynamodb.Table('TaxiTable')

            response = table.put_item(
                Item={
                    'trip': taxi_trip_key,
                    'discount_map': taxi_discounts_list
                    }
                } )

Upvotes: 1

Views: 4146

Answers (1)

Tom Wojcik
Tom Wojcik

Reputation: 6189

DynamoDB supports batch writing.

Example from docs:

with table.batch_writer() as batch:
    for i in range(50):
        batch.put_item(
            Item={
                'account_type': 'anonymous',
                'username': 'user' + str(i),
                'first_name': 'unknown',
                'last_name': 'unknown'
            }
        )

Guide docs
API docs

Upvotes: 3

Related Questions