Skip to main content

WhileIterator

Spark Gem

The WhileIterator allows you to group a set of gems together, and then run them one after another repeatedly in a loop.

WhileIterator

Input

The input of the WhileIterator can be any dataset. Importantly, the output schema of the WhileIterator must match or be a subset of the input schema. This is because the output feeds back into the input as the subgraph is iterating (looping).

Configuration

info

Before setting the WhileIterator configuration, make sure you are familiar with pipeline configurations.

Once you have a WhileIterator subgraph on your canvas, set configurations by opening the subgraph and clicking on Configuration. The following table describes the parameters you need to set.

ParameterTabDescription
Max iterationsSettingsThe maximum number of loops that will be performed.
Populate iteration number in config variableSettingsA checkbox to enable if you want to keep track of iteration numbers in a variable.
Select config variable name to populate iteration numberSettingsThe name of the config variable that will store the iteration number.
SchemaConfigurationA table where you define configuration variables. You can Copy Pipeline Configs to inherit these from your pipeline. If you want to populate the iteration number in a config variable, make sure to include that variable here.
ConfigConfigurationAn area where you can define default values for variables in one or more configurations.

Subgraph

Within the WhileIterator, you can design a flow with multiple gems to create your desired output.

Importantly, you also need to understand the two inputs of the WhileIterator_Output. The order of your inputs is important:

  • The in0 dataset contains the DataFrame for the following iteration. If this dataset becomes empty, the loop is terminated.
  • The in1 dataset passes on an individual iteration. You'll see the entire list of iterations as the output of the WhileIterator gem.

Break condition

There are two scenarios in which the WhileIterator will break:

  • The in0 dataset of the WhileIterator_Output becomes empty.
  • The maximum number of iterations is reached.

Output

There are two outputs of the WhileIterator:

  • The out0 dataset that contains the output of the last iteration of the WhileIterator.
  • The out1 dataset that contains the whole list of iterations produced by the WhileIterator. This is produced by performing a union on all output iterations.

Example

Imagine you are simulating an investment that earns a fixed percentage of interest per year. You want to keep compounding the interest until your investment reaches a desired amount. You can use the WhileIterator gem to produce a dataset demonstrating this yearly growth.

WhileIterator

For this example, the input of the WhileIterator will be a simple table that describes your starting investment and the fixed interest rate.

yearprincipalinterest_rate
010000.05

Add Reformat gem

Now, let's use a Reformat gem in the WhileIterator subgraph to update the Year and Principal each iteration.

  1. Add the Reformat gem inside the WhileIterator.
  2. Connect WhileIterator_Input to the Reformat gem.
  3. Connect the Reformat gem to the WhileIterator_Output.
  4. Configure the Expressions table as it is shown below.
Target ColumnExpression
yearyear + 1
principalprincipal + principal * interest_rate
interest_rateinterest_rate

Terminate loop

The while loop will stop if the in0 dataset becomes empty. How can the in0 dataset become empty? In this example:

  1. Add a Filter gem with the condition principal < 2000.
  2. Connect the Reformat gem output to the input of the Filter gem.
  3. Connect the Filter gem output to the input of WhileIterator_Output.

This way, the Filter gem will return data as-is until the principal reaches 2000. After that, the record is filtered out, and the loop will terminate.

WhileIterator

View output data

The out0 dataset should be empty because we used a Filter gem to return an empty set when the principal > 2000.

Below is an example out1 dataset. This lets you project how your principal will grow over the years.

yearprincipalinterest_rate
110500.05
21102.50.05
31157.6250.05
41215.506250.05
51276.28156250.05
61340.0956406250.05
71407.1004226560.05
81477.4554437890.05
91551.3282159780.05
101628.8946267770.05
111710.3393581160.05
121795.8563260220.05
131885.6491423230.05
141979.9315994390.05
152078.9281794110.05