Hey guys,
I just thought of writing few commands for some one who is beginner in PIG .
So let's get started. We will consider the data in file in this format.
year,product,quantity
2000, iphone, 1000
2001, iphone, 1500
2002, iphone, 2000
2000, nokia, 1200
2001, nokia, 1500
2002, nokia, 900
1) Load Command
A = LOAD '/home/Desktop/Projects/Book/UserTest' USING PigStorage(',') AS (year:chararray,product:chararray,quantity:int);
2) If you want to see the records.
Dump A;
3) To check the schema.
Describe A;
4) Filter :
B = Filter A BY year == '2000';';
5) group :
C = Group A BY year;
This command will give Tuples.
(2000,{(2000, nokia, 1200),(2000, iphone, 1000)})
(2001,{(2001, nokia, 1500),(2001, iphone, 1500 )})
(2002,{(2002, nokia, 900),(2002, iphone, 2000)})
// here 1st field Key on which data is grouped and
// 2nd field is Bag of Tuples
You can also try this :
describe C;
C: {group: chararray, A: {(year: chararray,product: chararray,quantity: chararray)}}
Here "group" is the alias given to grouping field by Pig.
A is the data on which grouping is done.
6) max_quant = foreach C GENERATE group, MAX (A.quantity);
Dump max_quant;
(2000,1200)
(2001,1500)
(2002,2000)
7) ILLUSTRATE: ILLUSTRATE A; // TO GET IDEA OF DATA AND QUERY
----------------------------------------------------------------------------
| A | year:chararray | product:chararray | quantity:int |
----------------------------------------------------------------------------
| | 2001 | iphone | 1500 |
----------------------------------------------------------------------------
That's it for the day. I think these commands are good enough to get anyone started with PIG..
Thanks
Varun
I just thought of writing few commands for some one who is beginner in PIG .
So let's get started. We will consider the data in file in this format.
year,product,quantity
2000, iphone, 1000
2001, iphone, 1500
2002, iphone, 2000
2000, nokia, 1200
2001, nokia, 1500
2002, nokia, 900
1) Load Command
A = LOAD '/home/Desktop/Projects/Book/UserTest' USING PigStorage(',') AS (year:chararray,product:chararray,quantity:int);
2) If you want to see the records.
Dump A;
3) To check the schema.
Describe A;
4) Filter :
B = Filter A BY year == '2000';';
5) group :
C = Group A BY year;
This command will give Tuples.
(2000,{(2000, nokia, 1200),(2000, iphone, 1000)})
(2001,{(2001, nokia, 1500),(2001, iphone, 1500 )})
(2002,{(2002, nokia, 900),(2002, iphone, 2000)})
// here 1st field Key on which data is grouped and
// 2nd field is Bag of Tuples
You can also try this :
describe C;
C: {group: chararray, A: {(year: chararray,product: chararray,quantity: chararray)}}
Here "group" is the alias given to grouping field by Pig.
A is the data on which grouping is done.
6) max_quant = foreach C GENERATE group, MAX (A.quantity);
Dump max_quant;
(2000,1200)
(2001,1500)
(2002,2000)
7) ILLUSTRATE: ILLUSTRATE A; // TO GET IDEA OF DATA AND QUERY
----------------------------------------------------------------------------
| A | year:chararray | product:chararray | quantity:int |
----------------------------------------------------------------------------
| | 2001 | iphone | 1500 |
----------------------------------------------------------------------------
That's it for the day. I think these commands are good enough to get anyone started with PIG..
Thanks
Varun
No comments:
Post a Comment