Pig - Get Max Count

大兔子大兔子 提交于 2019-12-12 03:38:02

问题


Sample Data

DATE      WindDirection

1/1/2000  SW
1/2/2000  SW
1/3/2000  SW
1/4/2000  NW
1/5/2000  NW

Question below

Every day is unqiue, and wind direction is not unique, SO now we are trying to get the COUNT of the most COMMON wind direction

My query was

weather_data = FOREACH Weather GENERATE $16 AS Date, $9 AS w_direction;
e = FOREACH weather_data 
            {
                unique_winds = DISTINCT weather_data.w_direction;
                GENERATE unique_winds, COUNT(unique_winds);
            }
dump e;

The logic is to find the DISTINCT WindDirections (there are like 7), then group by WindDirection and apply count.

Right now I think get the total number or count of directions of winds.


回答1:


You will have to GROUP BY wind direction and get the counts.Order the counts by desc order and get the top most row.

wd = FOREACH Weather GENERATE $9 AS w_direction;
gwd = GROUP wd BY w_direction;
cwd = FOREACH gwd GENERATE group as wd,COUNT(wd.$0);
owd = ORDER cwd BY $1 DESC;
mwd  = LIMIT owd 1;
DUMP mwd;


来源:https://stackoverflow.com/questions/36753093/pig-get-max-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!