The Problem
I'm trying to look into table item by item, so that if an item already exists then I should be able to update it, if not then I should be able to insert it.
However, I learnt that update works like upsert(update/insert) as well. My case doesn't suit that as well.
My Case
- Check if
item
exists in table and store theflag
(boolean) - Check if
flag
is0
, (item not available) then, insert the item and add current timestamp into columnInserted_dttm
- Check if
flag
is1
, (item available) then, update the item and add current timestamp into columnUpdated_dttm
(and not Inserted_dttm)
The Trial
I've been looking query()
is a good option over get_item()
, however your solutions are welcomed with any of it.
def lambda_handler(event, context):
x = TrainDataProcess()
file_name = 'Training_data/' + event['file_name']
s3.Object(bucket_name, file_name).download_file('/tmp/temp.xlsx')
table_name = 'training_data'
x.load_excel(name='/tmp/temp.xlsx')
x.load_headers(skiprows=0)
x.data_picking()
table = dynamoDB_client.Table(table_name)
load = x.return_records_json()
try:
with table.batch_writer() as batch:
for record in load:
flag = table.query(TableName=table_name, )
if flag == 0:
record['inserted_dttm'] = get_dttm()
batch.put_item(Item=record)
elif flag == 1:
record['updated_dttm'] = get_dttm()
batch.update_item(Item=record)
return True
except Exception as e:
return e