06-22-2022 07:58 PM
Hello,
I have a requirement where a snap has to wait for n amount of seconds before it executes. The n amount of seconds varies based on the output of the previous snap. How do I carry the output of a variable from a previous snap and pass it as the number of seconds to wait for the following snap.
Thanks
Aravind N
Solved! Go to Solution.
06-23-2022 07:04 AM
Yes there is. In the incoming object itself, map a field called “time” for example. Than, in the script, right after the
in_doc=self.input.next()
line, add the following one:
sleep_time = in_doc.get('time')
sleep_time
is your variable you can use to dynamically change the waiting time for every document. Meaning you need to change the sleep function to:
time.sleep(sleep_time)
You can manipulate the waiting time by changing the “time” value in the mapper before the script, or even pass that value from a parameter.
06-23-2022 06:48 AM
Is there a way to send a variable from the previous snap to the script Snap instead of hardcoding the seconds to sleep.
The below script has time.sleep(20), instead can we pass a parameter like time.sleep($sleep_time), where $sleep_time carries the number of seconds from the previous mapper.
def execute(self):
self.log.info(“Executing Transform script”)
while self.input.hasNext():
try:
# Read the next input document, store it in a new dictionary, and write this as an output document.
inDoc = self.input.next()
time.sleep(20)
outDoc = inDoc
self.output.write(inDoc, outDoc)
06-23-2022 07:04 AM
Yes there is. In the incoming object itself, map a field called “time” for example. Than, in the script, right after the
in_doc=self.input.next()
line, add the following one:
sleep_time = in_doc.get('time')
sleep_time
is your variable you can use to dynamically change the waiting time for every document. Meaning you need to change the sleep function to:
time.sleep(sleep_time)
You can manipulate the waiting time by changing the “time” value in the mapper before the script, or even pass that value from a parameter.
06-23-2022 10:14 AM
That works brilliant!! Thanks so much!