Saraswathy Rajamani
Saraswathy Rajamani

Reputation: 13

Using PIG -how to order sum of two columns in desc and get the first 5 rows in the result

Flaglist = load '/flag.txt' using PigStorage (',') as (countryname:chararray, landmass:int, Zone:int, Area:int, Population:int, Language:int, Religion:int, Bars:int, stripes:int, Colours:int,red:int, green:int, blue:int,  gold:int, white:int, black:int, orange:int, mainhue:chararray, noofcircles:int, noofuprightcrosses:int, noofdiagonalcrosses:int, noofquarters:int, noofsunstarts:int, crescent:int, triangle:int, icon:int ,animate:int, text:int, topleft:chararray, bottomright:chararray );

Sumofbs = FOREACH Flaglist GENERATE Bars+stripes  ;
OrderBS = FOREACH Sumofbs GENERATE  Flaglist.countryname ;

ans:-

2016-03-22 21:47:27,284 [main] INFO org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.MapReduceLauncher - Some jobs have failed! Stop running all dependent jobs 2016-03-22 21:47:27,358 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1066: Unable to open iterator for alias OrderBS. Backend error : Scalar has more than one row in the output. 1st : (Afghanistan,5,1,648,16,10,2,0,3,5,1,1,0,1,1,1,0,green,0,0,0,0,1,0,0,1,0,0,black,green), 2nd :(Albania,3,1,29,3,6,6,0,0,3,1,0,0,1,0,1,0,red,0,0,0,0,1,0,0,0,1,0,red,red)

Upvotes: 0

Views: 1189

Answers (1)

nobody
nobody

Reputation: 11080

Use ORDER BY and LIMIT

Flaglist = load '/flag.txt' using PigStorage (',') as (countryname:chararray, landmass:int, Zone:int, Area:int, Population:int, Language:int, Religion:int, Bars:int, stripes:int, Colours:int,red:int, green:int, blue:int,  gold:int, white:int, black:int, orange:int, mainhue:chararray, noofcircles:int, noofuprightcrosses:int, noofdiagonalcrosses:int, noofquarters:int, noofsunstarts:int, crescent:int, triangle:int, icon:int ,animate:int, text:int, topleft:chararray, bottomright:chararray );

Sumofbs = FOREACH Flaglist GENERATE countryname,Bars+stripes as Total;
OrderBS = ORDER Sumofbs BY $1 DESC;
FinalOrderBS = LIMIT OrderBS 5;
DUMP FinalOrderBS;

Upvotes: 1

Related Questions