Eitanmg
Eitanmg

Reputation: 586

How can order a list by the second char in Apache Pig?

How can order a list by the second char?

For example this list:

apple
mango
orange

I want to be ordered by the second letter (alphabetical order)

mango
apple
orange

thanks !

Upvotes: 0

Views: 140

Answers (2)

Abhijit Bashetti
Abhijit Bashetti

Reputation: 8658

My data is

1,The Nightmare Before Christmas
2,The Mummy
3,Orphans of the Storm
4,The Object of Beauty

   A = LOAD '/home/abhijit/Downloads/movies.txt' USING PigStorage(',') as (a1:int,a2:chararray);
   B = FOREACH A GENERATE a2,SUBSTRING(a2,1,2) as a3;
   C = ORDER B BY a3;
   D = FOREACH C GENERATE a2;
   DUMP D;

Upvotes: 0

nobody
nobody

Reputation: 11080

Generate second field from the second character of the first field and then order by the second field.Finally get only the first field from the ordered relation.

A = LOAD 'test3.txt' USING PigStorage('\t') as (a1:chararray);
B = FOREACH A GENERATE a1,SUBSTRING(a1,1,2) as a2;
C = ORDER B BY a2;
D = FOREACH C GENERATE a1;
DUMP D;

Output

Output

Upvotes: 2

Related Questions