问题
1 I am trying to extract element from dynamdb recurselvely
2 I have dynamodb table with below attributes
`id, masterId, masterName`
3 I need to go to each id
in the dynamodb check whether 'masterId' != '' and 'masterName' != 'Reserved'
4 if 3rd one got success then I need to extract ``'masterId' and 'masterName' to
totallist `
- ** Now pass
masterId
to same function asid
this will keep on running till'masterId' == '' and 'masterName' == 'Reserved'
- else: add to
newlist_dict
Basically for the incoming id
check the masterID
is null or not if not null put into totallist else put in to newlist_dict
Below is the Code
def recursion(singledict, table_name):
totallist = []
newlist_dict = {}
if singledict.get('masterId') != '' and singledict.get('masterName') != 'Reserved':
singledict_response = client.get_item(
TableName=table_name,
Key={
'id': {'S': singledict['masterId']
}})
totallist_dict = {}
totallist_dict['id'] = singledict_response['Item']['masterId']['S']
totallist_dict['name'] = singledict_response['Item']['masterName']['S']
totallist .append(totallist_dict)
if singledict_response['Item']['masterId']['S'] != '' and \
singledict_response['Item']['masterName']['S'] != 'Reserved':
recursion(singledict, table_name)
else:
newlist_dict['id'] = singledict_response['Item']['id']['S']
return totallist,newlist_dict
I got error in maximum recursion dept
The error is at the recursion(singledict, table_name)
My code is running fine without this but single value only i m able to extract
example is below dynamodb details
id,masterId,masterName
100,1,A
1,2,B
2,3,D
3,'',''
singledict={'id':100}
Expected out
totallist = [{id:1,name:A}, {id:2,name:B}]
newlistdict = {id:3}
来源:https://stackoverflow.com/questions/64598874/how-to-loop-extract-element-from-dynamodb-recursively