Saturday, June 18, 2016

Pig Lab7

package pig.udfs;

---
----
public  class Concat
   extends EvalFunc<>
{
  public  String  exec(Tuple v)  
    throws IOException
  {
     String fn =(String) v.get(0);
     String sn =(String) v.get(1);
    String name = fn+" "+sn;  
     return name;
  }
}
_____________________________

when java class extended EvalFunc ,. 

the class will get udf functionality.

  the udf code(logic) to be written in 

exec method(function).

 exec has  Tuple variable, to capture 

passed values from function call of 

foreach statement.

ex:

x = foreach y generate fn, sn, 
   concat(fn,sn) as name;

Tuple variable can hold multiple 

values.

    to get first value.

     v.get(0)
2nd value ---  v.get(1)...
______________________________
 to get all values,
             v.getAll()
__________________________________

exec() function is executed for n 

times. n is number of tuples in the 

relation.

_________________________________

steps of udf
________________

step1) Develop UDF class 
step2) export class(es) into jar file.
step3) register jar file in pig.
step4) create temporary function for 

Pig Udf class.
step5) call the function.
_________________________________
grunt> emp = load 'pdemo/emp'        
>>    using PigStorage(',') 
>>   as (id:int, name:chararray, 

sal:int,
>>     sex:chararray, dno:int);
grunt> cat pdemo/emp
101,vino,26000,m,11
102,Sri,25000,f,11
103,mohan,13000,m,13
104,lokitha,8000,f,12
105,naga,6000,m,13
101,janaki,10000,f,12
201,aaa,30000,m,12
202,bbbb,50000,f,13
203,ccc,10000,f,13
204,ddddd,50000,m,13
grunt> 

task:
 design udf to convert name into 

uppercase.


step1>

 Design udf class.

elipse steps.

create project:

file ---> new  ---> java project.
    MyUdfs

configure pig jar file:

src ---> build path ---> configure 

build path ---> libraries --> add 

external jar .

   /usr/lib/pig/pig-core.jar 

create package

 src ---> new --> package 

ex:      pig.udfs

create java class.
  
 package(pig.udfs) ---> new ---> class

 ex:   ToBigCase

package pig.udfs;

import java.io.IOException;

import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;

public class ToBigCase extends 

EvalFunc<String>
{
    public   String exec(Tuple v)
     throws IOException
     {
     String val =(String) v.get(0);
     String bname = 

val.toUpperCase();
     return  bname;
     }
}

Step2.
 Export into jar file.

  project(MyUdfs) --->
     export --->
       java --->
          jar file

    ex:  

/home/training/Desktop/pjars.jar

Step3:
 Register jar file into pig.

grunt> register Desktop/pjars.jar;

step4: create temporary function.

grunt> define tobig  

pig.udfs.ToBigCase();

step5: call the function.

grunt> 

grunt> describe emp;
emp: {id: int,name: chararray,sal: 

int,sex: chararray,dno: int}
grunt> e = foreach emp generate 
>>    id, tobig(name) as name, sal ,   


>>      tobig(sex) as sex, dno;
grunt> dump e

(101,VINO,26000,M,11)
(102,SRI,25000,F,11)
(103,MOHAN,13000,M,13)
(104,LOKITHA,8000,F,12)
(105,NAGA,6000,M,13)
(101,JANAKI,10000,F,12)
(201,AAA,30000,M,12)
(202,BBBB,50000,F,13)
(203,CCC,10000,F,13)
(204,DDDDD,50000,M,13)
_____________________________
Assignment:

comment file:

 hadoop       is    going to be 

platform of all bigdata frames.





lines = load 'pdemo/comment' 
    as (line:chararray);

nlines = foreach lines generate 
     removeWhiteSp(line) as line;

 expected output:
   
  hadoop is going to be .....
___________________________-
Assignment 2.
 top 3 salaries.

[training@localhost ~]$ cat > profiles
ravi,90000
rani,90000
giri,10000
mani,50000
siva,50000
hari,50000
mani,50000 
vani,10000
veni,5000 
[training@localhost ~]$ hadoop fs -

copyFromLocal profiles pdemo
[training@localhost ~]$ 

_______________________

Assignment 3.

 make all hyphons  to comma.



101,aaa,30000-11-hyd,m
   :
   :
109,bbbb,40000-12-del,f
______________________________







 ex udfs ...  neutral()

public class Neutral extends
    EvalFunc<String>
{
  public String exec(Tuple v) 
   throws IOException
  {
     String val =(String) v.get(0);
   String nval =   val.replaceAll

("-",",");
    return nval
  }
}

export into -->  pjars.jar.

 register Desktop/pjars.jar;

define  neutral  pig.udfs.Nuetral();

 ds = load 'pdemo/file1' 
   as (line:chararray);

 nds = foreach ds generate   
         neutral(line) as line;

     line ---> 101,aaa,3000,11,hyd,m
     
 store nds into 'myloc';

 x = load 'myloc/part-m-00000'
   using PigStorage(",")
  AS (id:int, name:chararray, sal:int, 
  dno:int, city:chararray, 

sex:chararray);

______________________________Assignment2 solution is given next post.-----------------

Pig Lab6

task:

  getting top3 salaried list.

 (case: a salary can be taken by multiple people).

[training@localhost ~]$ cat > samps
aaa,10000
bbb,80000
ccc,90000
ddd,90000
eeee,90000
ffff,80000
mmmmm,80000
nnnnn,70000
nnnn,70000
nn,60000 
m,65000 
xx,10000
[training@localhost ~]$ hadoop fs -copyFromLocal samps pdemo
[training@localhost ~]$ 

grunt> e = load 'pdemo/samps'     
>>     using PigStorage(',')
>>   as (name:chararray, sal:int);
grunt> sals = foreach e generate sal;
grunt> sals = distinct sals;
grunt> sals2 = order sals by sal desc;
grunt> top3 = limit sals2 3;
grunt> dump top3

grunt> describe top3
top3: {sal: int}
grunt> describe e
e: {name: chararray,sal: int}
grunt> res = join e by sal , top3 by sal;
grunt> describe res;
res: {e::name: chararray,e::sal: int,top3::sal: int}
grunt> res = foreach res generate e::name as name,
>>         e::sal as sal;
grunt> dump res
(nnnnn,70000)
(nnnn,70000)
(bbb,80000)
(ffff,80000)
(mmmmm,80000)
(ccc,90000)
(ddd,90000)
(eeee,90000)
_____________________________________
Cross:
 gives cartisian product.

used for non-equi functionalities of joins.

[training@localhost ~]$ cat > matrimony
Ravi,25,m
Rani,24,f
Ilean,23,f
trisha,27,f
Kiran,29,m
madhu,22,m
avi,26,m
srithi,21,f
[training@localhost ~]$ hadoop fs -copyFromLocal matrimony pdemo
[training@localhost ~]$ 
grunt> matri = load 'pdemo/matrimony' 
>>    using PigStorage(',')
>>   as (name:chararray, age:int, sex:chararray);
grunt> males = filter matri by (sex=='m');
grunt> fems = filter matri by (sex=='f');
grunt> cr = cross males, fems;
grunt> describe cr
cr: {males::name: chararray,males::age: int,males::sex: chararray,fems::name: chararray,fems::age: int,fems::sex: chararray}
grunt> mf = foreach cr generate males::name as mname, fems::name as fname , males::age as mage,
>>  fems::age as fage;
grunt> 
grunt> describe mf
mf: {mname: chararray,fname: chararray,mage: int,fage: int}
grunt> mlist = filter mf by                
>>   (mage>fage  and (mage-fage)<4);

grunt> dump mlist;
(madhu,srithi,22,21)
(avi,Rani,26,24)
(avi,Ilean,26,23)
(Kiran,trisha,29,27)
(Ravi,Rani,25,24)
(Ravi,Ilean,25,23)
_________________________________

to submit scripts

3 commands:
 i) Pig
 ii) exec
 iii) run 

 pig to submit from command prompt.
  aliases will not be available in grunt.

 exec- to submit script from grunt shell. aliases will not be available.

 run- to submit script from grunt,
 aliases will be available.
 so that we reuse them.

[training@localhost ~]$ cat script1.pig
emp = load 'pdemo/emp' using PigStorage(',')
    as (id:int, name:chararray, sal:int, sex:chararray, dno:int);
e = foreach emp generate sex, sal;
bySex = group e by sex;
res = foreach bySex generate group as sex, SUM(e.sal) as tot;
dump res

$ pig script1.pig

grunt> exec script1.pig

grunt> run script1.pig

______________________________

register, define.

Pig Joins








Pig Lab5

grunt> ls pdemo/e1

hdfs://localhost/user/training/pdemo/e1<r 1>    39
grunt> cat pdemo/e1
ravi,30000,m
rani,40000,f
giri,50000,m
grunt> cat pdemo/e2
hari,60000,11
hara,70000,12
grunt> e1 = load 'pdemo/e1' using PigStorage(',')
>>   as (name:chararray, sal:int, sex:chararray);
grunt> cat pdemo/e2
hari,60000,11
hara,70000,12
grunt> e2 = load 'pdemo/e2' using PigStorage(',')
>>   as (name:chararray, sal:int ,
>>    dno:int);
grunt> ee1 = foreach e1 generate *,0 as dno;
grunt> describe ee1;
ee1: {name: chararray,sal: int,sex: chararray,dno: int}
grunt> 
grunt> ee2 = foreach e2 generate name,sal,
>>        '*' as sex, dno;
grunt> describe ee2
ee2: {name: chararray,sal: int,sex: chararray,dno: int}
grunt> e = union ee1, ee2;
grunt> dump e;

(ravi,30000,m,0)
(rani,40000,f,0)
(giri,50000,m,0)
(hari,60000,*,11)
(hara,70000,*,12)
________________________________________
sql-->
  select dno , sum(sal) as tot from emp
     group by dno
    order by tot desc
    limit 3;

   ______________________________
   emp = load 'pdemo/emp' using PigStorage(',')
    as (id:int, name:chararray, sal:int, 
     sex:chararray, dno:int);

   e = foreach emp generate dno, sal;
   grp = group e by dno;
   res = foreach grp generate 
       group as dno, SUM(e.sal) as tot;
   ores = order res by tot desc;
   top3 = limit ores 3;

     above model is correct , if each dno total salary is unique.
   if total sal is taken by multiple dno s.
  you get wrong results.

  solution:  joins/ udfs. 

 ex:
      15   9L
      18   9L
      25   9L
      30   8L
      40   7L

    top3;
      15   9L
      18   9L
      25   9L
    2nd top, 3rd top were missed.
_________________________________________

Joins examples;


[training@localhost ~]$ cat emp
101,vino,26000,m,11
102,Sri,25000,f,11
103,mohan,13000,m,13
104,lokitha,8000,f,12
105,naga,6000,m,13
101,janaki,10000,f,12
201,aaa,30000,m,12
202,bbbb,50000,f,13
203,ccc,10000,f,13
204,ddddd,50000,m,13
304,xxx,70000,m,14
305,yyy,80000,f,15
[training@localhost ~]$ 

[training@localhost ~]$ cat > dept
11,marketing,hyd
12,hr,del
13,finance,hyd
20,prod,hyd
21,admin,chennai
[training@localhost ~]$ 

[training@localhost ~]$ hadoop fs -copyFromLocal emp pdemo/empl
[training@localhost ~]$ hadoop fs -copyFromLocal dept  pdemo
[training@localhost ~]$ 



 emp = load ' ... '   ----
 dept = load ' ... '  ------

 ij = join emp by dno, dept by dno;
 lj = join emp by dno left outer, dept by dno;
 rj = join emp by dno right outer, dept by dno;
 fj = join emp by dno full outer, dept by dno;

 dump fj;
(101,vino,26000,m,11,11,marketing,hyd)
(102,Sri,25000,f,11,11,marketing,hyd)
(104,lokitha,8000,f,12,12,hr,del)
(101,janaki,10000,f,12,12,hr,del)
(201,aaa,30000,m,12,12,hr,del)
(103,mohan,13000,m,13,13,finance,hyd)
(105,naga,6000,m,13,13,finance,hyd)
(202,bbbb,50000,f,13,13,finance,hyd)
(203,ccc,10000,f,13,13,finance,hyd)
(204,ddddd,50000,m,13,13,finance,hyd)
(304,xxx,70000,m,14,,,)
(305,yyy,80000,f,15,,,)
(,,,,,20,prod,hyd)
(,,,,,21,admin,chennai)

grunt> describe fj
fj: {emp::id: int,emp::name: chararray,emp::sal: int,emp::sex: chararray,emp::dno: int,dept::dno: int,dept::dname: chararray,dept::loc: chararray}
grunt> fj2 = foreach fj generate 
>>         emp::dno as dno1 ,
>>        dept::dno as dno2,
>>         emp::sal as sal;
grunt> describe fj2;
fj2: {dno1: int,dno2: int,sal: int}
grunt> 

(11,11,26000)
(11,11,25000)
(12,12,8000)
(12,12,10000)
(12,12,30000)
(13,13,13000)
(13,13,6000)
(13,13,50000)
(13,13,10000)
(13,13,50000)
(14,,70000)
(15,,80000)
(,20,)
(,21,)

grunt> fj3 = foreach fj2 generate 
>>      (dno1 is not null and dno2 is not null ?    'Working':(dno1 is not null and dno2 is null ?     'BenchTeam':'BenchProj')) as stat, sal;
grunt> describe fj3;
fj3: {stat: chararray,sal: int}
grunt> dump fj3

(Working,26000)
(Working,25000)
(Working,8000)
(Working,10000)
(Working,30000)
(Working,13000)
(Working,6000)
(Working,50000)
(Working,10000)
(Working,50000)
(BenchTeam,70000)
(BenchTeam,80000)
(BenchProj,)
(BenchProj,)

grunt> describe fj3
fj3: {stat: chararray,sal: int}
grunt> grp = group fj3 by stat;
grunt> res = foreach grp generate
>>    group as stat , SUM(fj3.sal) as tot;
grunt> dump res

(Working,228000)
(BenchProj,)
(BenchTeam,150000)

Pig Lab4


cogroup:-
_________

to get seperate inner bags (data groups) for each dataset.
 so that, we can perform seperate aggregations on each data set.

  ds1
_______
 (a,10)
 (b,20)
 (a,30)
 (b,40)
_________

  ds2
_________
 (a,30)
 (c,30)
 (c,40)
 (a,20)
_________

cg = cogroup ds1 by $0, ds2 by $0;
 --> group, ds1, ds2
(a,{(a,10),(a,30)},{(a,30),(a,20)})
(b,{(b,20),(b,40)},{})
(c,{},{(c,30),(c,40)})

res = foreach cg generate
      group as mykey, 
  COUNT(ds1) as cnt1,
  COUNT(ds2) as cnt2;
(a,2,2)
(b,2,0)
(c,0,2)
_________________________________

[training@localhost ~]$ cat > sales1
p1,2000
p2,3000
p1,4000
p1,5000
p2,4000
p3,5000
[training@localhost ~]$ cat > sales2
p1,6000
p2,8000
p1,1000
p1,5000
p1,6000
p2,6000
p2,8000
[training@localhost ~]$ hadoop fs -copyFromLocal sales1  pdemo


[training@localhost ~]$ hadoop fs -copyFromLocal sales2  pdemo

grunt> s1 = load 'pdemo/sales1'                
>>     using PigStorage(',')
>>    as (pid:chararray, price:int);
grunt> s2 = load 'pdemo/sales2'         
>>     using PigStorage(',')        
>>    as (pid:chararray, price:int);
grunt> cg = cogroup s1 by pid, s2 by pid;
grunt> describe cg
cg: {group: chararray,s1: {pid: chararray,price: int},s2: {pid: chararray,price: int}}
grunt> dump cg

(p1,{(p1,2000),(p1,4000),(p1,5000)},{(p1,6000),(p1,1000),(p1,5000),(p1,6000)})
(p2,{(p2,3000),(p2,4000)},{(p2,8000),(p2,6000),(p2,8000)})
(p3,{(p3,5000)},{})

grunt> res = foreach cg generate
>>    group as pid, SUM(s1.price) as tot1,
>>    SUM(s2.price) as tot2;
grunt> dump res

(p1,11000,18000)
(p2,7000,22000)
(p3,5000,)

___________________________________

cleaning nulls

grunt> describe res;
res: {pid: chararray,tot1: long,tot2: long}.

grunt> res = foreach res generate             
>>    pid,                                
>>    (tot1 is null   ?   0:tot1) as tot1,
>>   (tot2 is null ? 0:tot2) as tot2;     
grunt> 


(p1,11000,18000)
(p2,7000,22000)
(p3,5000,0)

    
grunt> res = foreach res generate
>>      *,  tot1+tot2 as totall;
grunt> dump res

(p1,11000,18000,29000)
(p2,7000,22000,29000)
(p3,5000,0,5000)

___________________________________

assignment:-
______________

task: cleaning 
schema -->
  trid, prid, price, mrp, qnt, discount

if  price missed, replace it by mrp.
if qnt missed , replace it by 1.
if discount missed , replace it by 0.

 sankara.deva2016@gmail.com
_______________________________

union:-
______

 ds1
________
name, sal
__________
aaa,10000
bbbb,30000
cccc,40000
___________

ds2
____________
name,sal
___________
 xxx,30000
 yyy,40000
 zzz,60000
______________

 e = union ds1  , ds2



if all files schema is different.
 ds3
_________
sal, name
___________
10000,abc
20000,def
____________

ds4 = foreach ds3 generate name, sal;

ds = union ds1, ds2, ds4;
___________________________

if files have different number of fields.

[training@localhost ~]$ cat > e1
ravi,30000,m
rani,40000,f
giri,50000,m
[training@localhost ~]$ cat > e2
hari,60000,11
hara,70000,12
[training@localhost ~]$ hadoop fs -copyFromLocal e1 pdemo
[training@localhost ~]$ hadoop fs -copyFromLocal e2 pdemo

grunt> e1 = load 'pdemo/e1' using PigStorage(',')
>>   as (name:chararray, sal:int, sex:chararray);
grunt> e2 = load 'pdemo/e2' using PigStorage(',')
>>   as (name:chararray, sal:int, dno:int);
grunt> ee1 = foreach e1 generate *,0 as dno;
grunt> ee2 = foreach e2 generate name,
>>         sal,'*' as sex, dno;
grunt> ee = union ee1, ee2;
grunt> 

________________________________________

distinct:- to eliminate duplicates

    ds
  _id,name_________
  (101,a)
  (102,b)
  (103,c)
  (101,a)
  (102,b)
  (101,a)
  (102,b)
  (102,x)
_____________

  ds2 = distinct ds;
  (101,a)
  (102,b)
  (103,c)
  (102,x)
___________________________

emp ---> id,name,sal,sex,dno

 what are different dno(departments)s

 e = foreach emp geneate dno;
 e2 = distinct e

 how many unique depts?

  grp = group e2 all;
 res = foreach grp generate
 COUNT(e2) as cnt; 

_____________________________

order:
______

 e = order emp by name;
 e2 = order emp by sal desc;
 e3 = order emp by sal desc,
              dno , sex desc;

--- no limit for max number of sort fields.

Pig Lab3

load, foreach , 


3) dump :-
    
    to execute data flow.
   writes output into console.

    entire dataflow will be converted as single map reduce job.

   ex:

  grunt> dump res;

______________________________________________

4) store:-

    to execute data flow.
   writes output into disk (local/hdfs)

   grunt> store res into 'results1';
    -- tab is delimiter.
         
   grunt> store res into 'results2' 
          using PigStorage(',');

   load and store operators use Storage Methods.
_______________________________________
     
5) filter :-

      to create subsets , based on given criteria.

     (row filter, equivalant to 'where' clause of sql  select statement )

 grunt> e1 = filter emp by (sex=='m');
grunt> dump e1

grunt> e1 = filter emp by (sex=='m' and dno==12)
>> ;
grunt> dump e1
   
___________________________________________

6) Limit :-

    to fetch top n number of tuples.

 grunt> top3 = limit emp 3;
grunt> dump top3

(101,vino,26000,m,11)
(102,Sri,25000,f,11)
(103,mohan,13000,m,13)

7) sample:-

     to create subsets, in random sample style.

(102,Sri,25000,f,11)
(104,lokitha,8000,f,12)
(203,ccc,10000,f,13)

grunt> rs = sample emp 0.5;
grunt> dump rs      

_________________________________________________

8) Aggregated functions in PigLatin.

   SUM(), AVG(), MAX(), MIN(), COUNT()

   
grunt> r = foreach emp generate SUM(sal) as tot;
grunt> dump r

   ABOVE statement will be failed during execution,.

    bcoz, AGGREGATED functions are applied only on inner bags.
   when you group data , inner bags will be produced.

_________________________________________

9) group: -
      to get inner bags foreach data group.
   based on grouping field.

grunt> describe emp;
emp: {id: int,name: chararray,sal: int,sex: chararray,dno: int}
grunt> -- select sex, sum(sal) from emp group by sex
grunt> e = foreach emp generate sex, sal;
grunt> bySex = group e by sex;
grunt> describe bySex
bySex: {group: chararray,e: {sex: chararray,sal: int}}
grunt> dump bySex

grunt> res = foreach bySex generate
>>         group as sex, SUM(e.sal) as tot;
grunt> describe res
res: {sex: chararray,tot: long}
grunt> store res into 'myhdfs1';

grunt> cat myhdfs1/part-r-00000
f       103000
m       125000
____________________________________


grunt> describe emp
emp: {id: int,name: chararray,sal: int,sex: chararray,dno: int}
grunt> ee = foreach emp generate dno, sal;
grunt> byDno = group ee by dno;
grunt> res = foreach byDno generate 
>>      group as dno, SUM(ee.sal) as tot;
grunt> store res into 'pdemo/res1';

grunt> ls pdemo/res1
hdfs://localhost/user/training/pdemo/res1/_logs <dir>
hdfs://localhost/user/training/pdemo/res1/part-r-00000<r 1>       28
grunt> cat pdemo/res1/part-r-00000
11      51000
12      48000
13      129000
grunt> 

grunt> -- single grouping and multiple aggregations
grunt> res1 = foreach bySex generate
>>    group as sex, 
>>     SUM(e.sal) as tot,
>>    AVG(e.sal) as avg,
>>    MAX(e.sal) as min,
>>    MIN(e.sal) as mn,
>>    COUNT(e) as cnt;
grunt> dump res1
(f,103000,20600.0,50000,8000,5)
(m,125000,25000.0,50000,6000,5)

_________________________________

grunt> -- multi grouping..
grunt> e = foreach emp generate dno, sex, sal;
grunt> grp = group e by dno, sex;
2016-06-09 19:28:35,893 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1000: Error during parsing. Unrecognized alias sex
Details at logfile: /home/training/pig_1465437040017.log
grunt> 
  above statement is invalid.
  pig does not allow groping by multiple fields.

  solution:
   make multiple fields as a tuple field, and group it by tuple.

grunt> grp = group e by (dno, sex);
grunt> describe grp
grp: {group: (dno: int,sex: chararray),e: {dno: int,sex: chararray,sal: int}}
grunt> res = foreach grp generate 
>>     group.dno, group.sex, SUM(e.sal) as tot; 
grunt> dump res
(11,f,25000)
(11,m,26000)
(12,f,18000)
(12,m,30000)
(13,f,60000)
(13,m,69000)
_________________________________
grunt> -- select sum(sal) from emp;
grunt> -- old one
grunt> e = foreach emp generate 'ibm' as org, sal;
grunt> dump e; 

(ibm,26000)
(ibm,25000)
(ibm,13000)
(ibm,8000)
(ibm,6000)
(ibm,10000)
(ibm,30000)
(ibm,50000)
(ibm,10000)
(ibm,50000)

grunt> grp = group e by org;
grunt> res = foreach grp generate 
>>         SUM(e.sal) as tot;
grunt> dump res
(228000)

_____________________________________

 2nd one --- for entire column aggregation.

grunt> describe emp;
emp: {id: int,name: chararray,sal: int,sex: chararray,dno: int}
grunt> e = foreach emp generate sal;
grunt> grp = group e all;
grunt> dump grp

(all,{(26000),(25000),(13000),(8000),(6000),(10000),(30000),(50000),(10000),(50000)})

grunt> res = foreach grp generate 
>>     SUM(e.sal) as tot, 
>>    AVG(e.sal) as avg, MAX(e.sal) as max,
>>    MIN(e.sal) as min, COUNT(e) as cnt;
grunt> dump res
(228000,22800.0,50000,6000,10)
_____________________________________________
Assignment:

  describe emp--> id,name,sal,sex,dno

________________________________
    Marketing A 30
    marketing   B       40
     :
     :
    Fin A 50
     :
    Fin D       30
_________________________

____________________________________________________

Cogroup:

Pig Lab2


Load :
_____
   used to load data from file to pig relation.

   the file can be from local/hdfs , depends on Pig Start up mode.

 [training@localhost ~]$ cat > file1
aaaaaaaaa
aaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaa
[training@localhost ~]$ hadoop fs -copyFromLocal file1 pdemo
[training@localhost ~]$ 

grunt> ls pdemo
hdfs://localhost/user/training/pdemo/comment<r 1>88
hdfs://localhost/user/training/pdemo/file1<r 1> 64
grunt> s = load 'pdemo/file1' as (line:chararray);
grunt> describe s
s: {line: chararray}
grunt> dump s

Total input paths to process : 1
(aaaaaaaaa)
(aaaaaaaaaaaaa)
(aaaaaaaaaaaaaaaaa)
(aaaaaaaaaaaaaaaaaaaaa)

if File has multiple fields:-



[training@localhost ~]$ cat > test1
100     200     4000
2000    300     400
1000    10      100
1       2       3
[training@localhost ~]$ hadoop fs -copyFromLocal test1 pdemo
[training@localhost ~]$ 

grunt> s = load 'pdemo/test1' as 
>>      (a:int, b:int, c:int);
grunt> dump s

(100,200,4000)
(2000,300,400)
(1000,10,100)
(1,2,3)

_____________________________________

if file has ',' or any other delimiter..


[training@localhost ~]$ cat > test2
10,20,30
100,2000,300
1,20,500
[training@localhost ~]$ hadoop fs -copyFromLocal test2 pdemo
[training@localhost ~]$ 


grunt> s1 = load 'pdemo/test2' as 
>>       (a:int, b:int, c:int);
grunt> dump s1

 Total input paths to process : 1
(,,)
(,,)
(,,)

grunt> s2 = load 'pdemo/test2' as 
>>     (a:chararray, b:int, c:int);
grunt> dump s2

(10,20,30,,)
(100,2000,300,,)
(1,20,500,,)

in above two cases, pig's default delimiter  tab space.
  but in file 0 tabs are existed.
 so entire line is treated as single field.

___________________________________

Pig has two Storage methods.


   i) PigStorage()-- is for Text input Format.
  ii) BinStorage()-- is for sequence input Format.
                            (Binary)


   default is PigStorage().

 for the PigStorage('\t'), tab is default delimiter.

grunt> cat pdemo/test1
100     200     4000
2000    300     400
1000    10      100
1       2       3
grunt> s1 = load 'pdemo/test1'        
>>         using PigStorage('\t') 
>>       as (a:int, b:int, c:int);
grunt> s2 = load 'pdemo/test1'        
>>         using PigStorage()     
>>       as (a:int, b:int, c:int);
grunt> s3 = load 'pdemo/test1'        
>>       as (a:int, b:int, c:int);
grunt> 

output of s1,s2,s3  is same.

in s2, PigStorage() is applied with out delimiter,
    \t is applied. (default)

in s3, no storage method is specified, 
   by default PigStorage() with \t delimiter will be applied.

_______________________________________
grunt> s4 = load 'pdemo/test1'
>>      as (a:int, b:int);
grunt> s5 = load 'pdemo/test1'
>>      as (a:int, b:int,c:int, d:int);
grunt> dump s5

in s4, first two fields will be loaded, 3rd field will be skipped.

 in s5,   d field will become null, bcoz no 4th field in the file.

(100,200,4000,)
(2000,300,400,)
(1000,10,100,)
(1,2,3,)

________________________________________________

grunt> cat pdemo/test2
10,20,30
100,2000,300
1,20,500
grunt> ds = load 'pdemo/test2'
>>      using PigStorage(',')
>>    as (a:int, b:int, c:int);
grunt> dump ds

(10,20,30)
(100,2000,300)
(1,20,500)

______________________________________


[training@localhost ~]$ cat emp
101,vino,26000,m,11
102,Sri,25000,f,11
103,mohan,13000,m,13
104,lokitha,8000,f,12
105,naga,6000,m,13
101,janaki,10000,f,12
201,aaa,30000,m,12
202,bbbb,50000,f,13
203,ccc,10000,f,13
204,ddddd,50000,m,13
[training@localhost ~]$ hadoop fs -copyFromLocal emp pdemo
[training@localhost ~]$ 


grunt> emp = load 'pdemo/emp' 
>>    using PigStorage(',')
>>  as (id:int, name:chararray, sal:int,
>>    sex:chararray, dno:int);
grunt> illustrate emp

------------------------------------------------------------------------------------------------
| emp     | id: bytearray | name: bytearray | sal: bytearray | sex: bytearray | dno: bytearray | 
------------------------------------------------------------------------------------------------
|         | 101           | vino            | 26000          | m              | 11             | 
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------
| emp     | id: int | name: chararray | sal: int | sex: chararray | dno: int | 
------------------------------------------------------------------------------
|         | 101     | vino            | 26000    | m              | 11       | 
------------------------------------------------------------------------------

grunt> 
initially each field is loaded as bytearray,
  later that will be converted into given data types.
____________________________________________

Foreach:
_________
   i) filtering fields.
     (subsetting fields)

grunt> describe emp
emp: {id: int,name: chararray,sal: int,sex: chararray,dno: int}
grunt> e = foreach emp generate name,sal,dno;
grunt> describe e;
e: {name: chararray,sal: int,dno: int}
grunt> dump e 

(vino,26000,11)
(Sri,25000,11)
(mohan,13000,13)
(lokitha,8000,12)
(naga,6000,13)
(janaki,10000,12)
(aaa,30000,12)
(bbbb,50000,13)
(ccc,10000,13)
(ddddd,50000,13)
______________________________________

 ii)  generate new fields.

grunt> e2 = foreach emp generate *, 
>>      sal*0.1 as tax, sal*0.2 as hra,
>>        sal-tax+hra as net;

  above statement will be failed,
   new field aliases can not be reused in same statement.

solution:

grunt> e2 = foreach emp generate *,        
>>      sal*0.1 as tax, sal*0.2 as hra;
grunt> e2 = foreach e2 generate *,         
>>            sal-tax+hra as net;
grunt> dump e2
(101,vino,26000,m,11,2600.0,5200.0,28600.0)
(102,Sri,25000,f,11,2500.0,5000.0,27500.0)
(103,mohan,13000,m,13,1300.0,2600.0,14300.0)
(104,lokitha,8000,f,12,800.0,1600.0,8800.0)
(105,naga,6000,m,13,600.0,1200.0,6600.0)
(101,janaki,10000,f,12,1000.0,2000.0,11000.0)
(201,aaa,30000,m,12,3000.0,6000.0,33000.0)
(202,bbbb,50000,f,13,5000.0,10000.0,55000.0)
(203,ccc,10000,f,13,1000.0,2000.0,11000.0)
(204,ddddd,50000,m,13,5000.0,10000.0,55000.0)

grunt> store e2 into 'pigRes3'
>>   using PigStorage(',');

grunt> ls pigRes3
hdfs://localhost/user/training/pigRes3/_logs    <dir>
hdfs://localhost/user/training/pigRes3/part-m-00000<r 1>  420
grunt> cat pigRes3/part-m-00000
101,vino,26000,m,11,2600.0,5200.0,28600.0
102,Sri,25000,f,11,2500.0,5000.0,27500.0
103,mohan,13000,m,13,1300.0,2600.0,14300.0
104,lokitha,8000,f,12,800.0,1600.0,8800.0
105,naga,6000,m,13,600.0,1200.0,6600.0
101,janaki,10000,f,12,1000.0,2000.0,11000.0
201,aaa,30000,m,12,3000.0,6000.0,33000.0
202,bbbb,50000,f,13,5000.0,10000.0,55000.0
203,ccc,10000,f,13,1000.0,2000.0,11000.0
204,ddddd,50000,m,13,5000.0,10000.0,55000.0
grunt> 

_________________________________

 iii) changing data types.

grunt> describe e2
2016-06-08 19:30:30,480 [main] WARN  org.apache.pig.PigServer - Encountered Warning IMPLICIT_CAST_TO_DOUBLE 3 time(s).
e2: {id: int,name: chararray,sal: int,sex: chararray,dno: int,tax: double,hra: double,net: double}
grunt> e3 = foreach e2 generate id, name, 
>>    sal, sex, dno, (int)tax, (int)hra,
>>        (int)net;
grunt> describe e3;
2016-06-08 19:32:01,456 [main] WARN  org.apache.pig.PigServer - Encountered Warning IMPLICIT_CAST_TO_DOUBLE 3 time(s).
e3: {id: int,name: chararray,sal: int,sex: chararray,dno: int,tax: int,hra: int,net: int}
grunt> 

__________________________________________________
iv) renaming fields.
__________________

   grunt> describe emp
emp: {id: int,name: chararray,sal: int,sex: chararray,dno: int}
grunt> e4  = foreach emp generate   
>>    id as ecode, name , sal as income, 
>>    sex as gender, dno;
grunt> describe e4;
e4: {ecode: int,name: chararray,income: int,gender: chararray,dno: int}
grunt> 

_________________________________

 v) Conditional transformations:

[training@localhost ~]$ cat > samp1
100,300
400,150
1,5
5,3
[training@localhost ~]$ hadoop fs -copyFromLocal samp1 pdemo
[training@localhost ~]$ 

grunt> x1 = load 'pdemo/samp1' using
>>    PigStorage(',') as (a:int, b:int);
grunt> x2 = foreach x1 generate *, 
>>        (a>b  ?   a:b) as big;
grunt> dump x2

(100,300,300)
(400,150,400)
(1,5,5)
(5,3,5)



nested conditions:

[training@localhost ~]$ cat > samp2
10      34      56
10      8       2
12      45      9  
[training@localhost ~]$ hadoop fs -copyFromLocal samp2 pdemo
[training@localhost ~]$ 

grunt> xx = load 'pdemo/samp2' 
>>     as (a:int, b:int, c:int);
grunt> y = foreach xx generate *,
>>      (a>b ?   (a>c ? a:c):(b>c ? b:c)) as big;
grunt> dump y

(10,34,56,56)
(10,8,2,10)
(12,45,9,45)

____________________________________
grunt> e = foreach emp generate 
>>    id, name, sal , (sal>=70000 ? 'A':
>>           (sal>=50000 ? 'B':
>>            (sal>=30000 ? 'C':'D'))) as grade,
>>    (sex=='m' ? 'Male':'Female') as sex,
>>  (dno==11 ? 'Mrkt':
>>    (dno==12 ? 'Hr':
>>     (dno==13 ? 'Fin':'Others'))) as dname;
grunt> store e into 'pigRes4';

grunt> ls pigRes4
hdfs://localhost/user/training/pigRes4/_logs    <dir>
hdfs://localhost/user/training/pigRes4/part-m-00000<r 1>  271
grunt> cat pigRes4/part-m-00000
101     vino    26000   D       Male    Mrkt
102     Sri     25000   D       Female  Mrkt
103     mohan   13000   D       Male    Fin
104     lokitha 8000    D       Female  Hr
105     naga    6000    D       Male    Fin
101     janaki  10000   D       Female  Hr
201     aaa     30000   C       Male    Hr
202     bbbb    50000   B       Female  Fin
203     ccc     10000   D       Female  Fin
204     ddddd   50000   B       Male    Fin
grunt> 

____________________________________________

vi) copy relation to relation

   e = foreach emp generate *;