user1432155
user1432155

Reputation: 733

Update 2 Columns in PIG

I want to update to columns when a particular condition satisfies.

For example:

We will first load data

A = load 'students.txt' as (name:chararray, age:int, gpa:float);

Now,

B = foreach A generate name, (age==18?1:age) as age, gpa;

Here whenever my condition for age is satisfied at the same instant I want to update one more column say is_adult and set it's value to true and this column in created dynamically(As you observe is_adult column is not there in original schema).

Your help will be highly appreciated.

Upvotes: 0

Views: 296

Answers (1)

Prasoon Joshi
Prasoon Joshi

Reputation: 799

A = LOAD 'students.txt' AS (name:chararray, age:int, gpa:float);
B = FOREACH A GENERATE
    name,
    (age==18?1:age) AS age,
    (age>=18?'true':'false') AS adult,
    gpa ;

The adult column would be updated with true or false based on the value of age. This is a pretty standard way of doing this. The new schema/alias obtained in the FOREACH loop can have more (or less) number of columns than the original alias.

Upvotes: 1

Related Questions