Newer Version Available

This content describes an older version of this product. View Latest

foreach

Applies a set of expressions to every row in a dataset. This action is often referred to as projection.

Syntax

The output column names are specified with the as keyword. The output data is ungrouped.

Using foreach with Ungrouped Data

When used with ungrouped data, the foreach statement maps the input rows to output rows. The number of rows remains the same.

Example

a2 = foreach a1 generate carrier as carrier, miles as miles;

Using foreach with Grouped Data

When used with grouped data, the foreach statement behaves differently than it does with ungrouped data.

Fields can be directly accessed only when the value is the same for all group members. For example, the fields that were used as the grouping keys have the same value for all group members. Otherwise, use aggregate functions to access the members of a group. The type of the column determines which aggregate functions you can use. For example, if the column type is numeric, you can use the sum() function.

Example

z = foreach y generate day as day, unique(origin) as uorg, count() as n;

Using foreach with a case Expression

To create logic in a foreach statement that chooses between conditional statements, use a case expression.

Example

This example query uses the simple case expression syntax:

Example

This example query uses the searched case expression syntax:

Use Unique Names

Using a name multiple times in a projection throws an error.

For example, the last line in this query is invalid and throws an error: