Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

hadoop - Pig: is it possible to write a loop over variables in a list?

I have to loop over 30 variables in a list

[var1,var2, ... , var30]

and for each variable I use some PIG group by statement such as

grouped = GROUP data by var1;
data_var1 = FOREACH grouped{
                            GENERATE group as mygroup,
                                     COUNT(data) as count;
                            };

Is there a way to loop over the list of variables or I am forced to repeat the code above manually 30 times in my code?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think what you're looking for is the pig macro

Create a relation for your 30 variables, and iterate on them by foreach, and call a macro which get 2 params: your data relation and the var you want to group by. Just check the example in the link the macro is really similar what you'd like to do.

UPDATE & code

So here's the macro you can use:

DEFINE my_cnt(data, group_field) RETURNS C {
        $C = FOREACH (GROUP $data by $group_field) GENERATE
                group AS mygroup,
                COUNT($data) AS count;
};

Use the macro:

IMPORT 'cnt.macro';

data = LOAD 'data.txt' USING PigStorage(',') AS (field:chararray, value:chararray);
DESCRIBE data;

e = my_cnt(data,'the_field_you_group_by');
DESCRIBE e;
DUMP e;

I'm still thinking on how can you iterate through on your fields you'd like to group by. My original suggestion to foreach through a relation what contains the filed names not correct. (To create a UDF for this always works.) Let me think about it. But this macro works as is if you call by all the filed name you want to group.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...