ForEaach Activity runs same element multiple times in Aure datafactory

DR 1 Reputation point
2022-02-21T09:26:26.737+00:00

From Lookup activity taking the output and passing to foreach for Iteration. Set the batch count as 10 and trying to process each item.
However, the foreach activity is processing same element 4 times.
The output from Lookup has :1,2,3...10
Have to create separate files for each item as 1.txt,2.txt ,3.txt....10.txt
Have set the batch count as 4.
Howvever, the foreach activity iterating the first item value 1 four times and trying to create the file 1.txt and hence failing with error as trying to access same file.
Why the Foreach activity processing same item multiple times when Batch count is set?

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,646 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MartinJaffer-MSFT 26,236 Reputation points
    2022-02-22T19:05:52.71+00:00

    Hello @DR and welcome to Microsoft Q&A.

    Please help me verify I understand you ask correctly.

    You have a source data you are doing a lookup on, data is something like

    Num, Text  
    1, abc  
    2, def  
    3 ghi  
    4 jkl  
    ...  
    8, tuv  
    9, wxy  
    10, ZZZ  
    

    You want to take each row and create a file for each, named as <Num>.txt . This is done somehow inside a ForEach loop, iterating over the output of the lookup. I'm guessing there is SQL involved somewhere.

    The problem you are seeing is processing the same item multiple times. Please let me know if there is a Set Variable activity inside the ForEach loop. The behavior you have described matches some of my experiments with what happens to a Set Variable activity inside a non-sequential loop.

    From those experiments, I learned there is only 1 "slot" for a variable. All reads and writes access this same "slot". This means when the simultaneous instances of the loop activate, they can interfere with each other. This is called a "race condition". The main solutions is to either make the loop sequential, or to be clever with the Dynamic Expression so you do not need to use the Variable.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.