Apr 1, 2016

Apache Pig Exercises: 25 List Employee names starting with ‘S’ and having five characters



In this post the sample Apache Pig script will List Employee names starting with ‘S’ and having five characters with Apache Pig.

Using Apache Pig version r0.15.0.


@ Test data structure:
Please refer to APACHE PIG ~ ALL SAMPLE TABLES and STRUCTURES post for the file structures, visit the reference section shown at the bottom of the post for more. 


@ Sample data:

Employees data table:


@ Apache Pig Script:

a) List Employee names starting with ‘S’ and having five characters with Apache Pig:


grunt> 
data = LOAD 'Documents/tbl_EMP.txt' USING PigStorage(',') as (empno:int, ename:chararray, job:chararray, mgr:int, hiredate:chararray, sal:float, comm:float, deptno:int);
all_recs = foreach data generate empno,ename, job,mgr,hiredate, sal, comm,deptno;
rec_fltr = filter all_recs by   SIZE(ename) == 5) and (SUBSTRING(ename,0,1) == 'S');
rec_ordr = order rec_fltr by sal;
dump rec_fltr;


@Apache Pig Output on Grunt Shell:  

(7369,SMITH,CLERK,7902,1980-12-17,800.0,,20)
(7788,SCOTT,ANALYST,7566,1982-12-09,3000.0,,20)

----------------------------------------------------------------------------------------------------------------------------------------------------------

OR

b) List Employee names starting with ‘S’ and having five characters with Apache Pig:

grunt> 
data = LOAD 'Documents/tbl_EMP.txt' USING PigStorage(',') as (empno:int, ename:chararray, job:chararray, mgr:int, hiredate:chararray, sal:float, comm:float, deptno:int);
all_recs = foreach data generate empno,ename, job,mgr,hiredate, sal, comm,deptno;
rec_fltr = filter all_recs by (SIZE(ename) == 5) and (STARTSWITH(ename,'S') == true);
rec_ordr = order rec_fltr by sal;
dump rec_fltr;

@Apache Pig Output on Grunt Shell: 

(7369,SMITH,CLERK,7902,1980-12-17,800.0,,20)
(7788,SCOTT,ANALYST,7566,1982-12-09,3000.0,,20)



@ Apache Pig Reference/s:
  • https://pig.apache.org
  • http://pig.apache.org/docs/r0.15.0/
_________________
Thank you!

0 comments:

Post a Comment