pythonamazon-dynamodb

How to create a request for batch write in dynamodb for string set datatype?


I uitilize boto3 to create a dynamo client and do an put_item operation. The item that is pushed to dynamodb is of following format. It has a string set datatype , which works.

my_list = ["abc", "efg" ...]
{
   "pk" : {"S": "2308ryfoif"}, 
   "some_list" : {"SS" : my_list}
    
}

now instead of each put_item , i want to use batch writer

dynamodb_table = boto3.resource('dynamodb').Table('example')
   with dynamodb_table.batch_writer() as batch:
        for i in list:
        batch.put_item(Item=i)

but the format or schema for batch operation , i understand is we don't have to specify datatype , such as

{
   "pk" : "2308ryfoif", 
   "some_list" : my_list
    
}

but this translates to a list in dynamodb, how can i post this as a string set ?


Solution

  • It saves as a list because you pass it a list. If you want a set, then you can convert your list to a set type:

    {
       "pk" : "2308ryfoif", 
       "some_list" : set(my_list)
    }
    

    More info on python data structures here: https://docs.python.org/3/tutorial/datastructures.html#sets