如何获得猪的MAX和MIN值的名称

时间:2017-04-27 14:45:01

标签: apache-pig

我有猪代码来获取最大值和最小值。我可以展示它们,但没有名称是max和min

在代码中你可以看到我写了data.KEY但是这个会打印所有的名字

film = LOAD './film.csv' USING PigStorage(',') AS 
					(film_id:int,title:chararray,description:chararray,release_year:int,language_id:int,rental_duration:int,
					rental_rate:int,length:float,replacement_cost:float,rating:chararray,special_features:chararray);
film_category = LOAD './film_category.csv' USING PigStorage(',') AS (film_id:int , category_id:int);
category = LOAD './category.csv' USING PigStorage(',') AS (category_id:int , name:chararray);

result1 = JOIN film BY film_id , film_category BY film_id;
result2 = JOIN result1 BY film_category::category_id , category BY category_id;

result3 =  foreach (GROUP result2 BY category::name) generate group as KEY , AVG(result2.length) as avg_value;
data = ORDER result3 BY KEY ASC;
grouped = GROUP data All;

max = foreach grouped generate data.KEY as name1, MAX(data.avg_value) as max_value;
min = foreach grouped generate data.KEY as name2, MIN(data.avg_value) as min_value;

values = foreach grouped GENERATE max.name1, max.max_value  , min.name2, min.min_value;

DUMP values;

1 个答案:

答案 0 :(得分:0)

您正在产生'价值观'通过使用分组生成所有名称的关系,因为你按所有分组。在分组为max之后,按desc分组并获得顶行。类似于min,按asc排序并获得顶行。

max_min = foreach grouped {
            desc_order = order data by avg_value DESC;
            asc_order = order data by avg_value ASC;
            desc_limit = limit desc_order 1;
            asc_limit = limit asc_order 1;
            generate flatten(desc_limit),flatten(asc_limit);
}
DUMP max_min;