Pig UDFS
----------
UDF ---> user defined functions.
adv:
i) custom functionalities.
ii) reusability.
Pig UDFs can be developed by
java
python
ruby
c++
javascript
perl
step1:
Develop udf code.
step2:
export into jar file
ex: /home/cloudera/Desktop/pigs.jar
step3:
register jar file into pig.
grunt> register Desktop/pigs.jar
step4:
create temporory function for udf class.
grunt> define ucase pig.analytics.ConvertUpper();
step5:
calling the function:
grunt>e = foreach emp generate
id, ucase(name) as name, sal,
ucase(sex) as sex, dno;
package pig.analytics;
import .....
--> ucase(name) ---> upper conversion
public class ConvertUpper extends EvalFunc<String>
{
public String exec(Tuple v)
throws IOException
{
String str = (String)v.get(0);
String res = str.toUpperCase();
retrun res;
}
}
--------------------------
$ cat > samp
100,230,400
123,100,90
140,560,430
$ hadoop fs -copyFromLocal samp piglab
grunt> s = load 'piglab/samp'
using PigStorage(',')
as (a:int, b:int, c:int);
package pig.analytics;
....
public class RMax extends EvalFunc<Integer>
{
public Integer exec(Tuple v)
throws IOException
{
int a =(Integer) v.get(0);
int b =(Integer) v.get(1);
int c =(Integer) v.get(2);
int big = a; // 10,20,3
if (a>big) big = a;
if (b>big) big = b;
if (c>big) big = c;
return big;
}
}
export into jar : Desktop/pigs.jar
grunt> register Desktop/pigs.jar;
grunt> define rmax pig.analytics.RMax();
grunt> res = foreach s generate *,
rmax(*) as max;
--------------------------------
package pig.analytics;
.......
public class RowMax
extends EvalFunc<Integer>
{
public Integer exec(Tuple v) throws IOException
{
List<Object> lobs = v.getAll() ;
int max = 0;
int cnt =0;
// -20,-3,-40
for(Object o : lobs)
{
cnt++;
int val = (Integer)o;
if(cnt==1) max = val;
max = Math.max(max, val);
}
return max;
}
}
export in to jar : Desktop/pigs.jar
grunt> register Desktop/pigs.jar
grunt> define dynmax pig.analytics.RowMax();
grunt> r = foreach s generate *, dynmax(*) as m;
-----------------------------------------
emp = load 'piglab/emp' using PigStorage(',')
as (id:int, name:chararray, sal:int,
sex:chararray, dno:int);
grade()
dname()
gender()
package pig.analytics;
public class Gender extends EvalFunc<String>
{
public String exec(Tuple v) throws IOException
{
String s =(String) v.get(0);
s = s.toUpperCase();
if (s.matches("F"))
s = "Female";
else
s = "Male";
return s;
}
}
-----------------
package pig.analytics;
public class Grade extends EvalFunc<String>
{
public String exec(Tuple v) throws IOException
{
String sal =(Integer) v.get(0);
String grade;
if (sal>=70000)
grade="A";
else if (sal>=50000)
grade="B";
else if (sal>=30000)
grade="C";
else
grade="D";
return grade;
}
}
------
package pig.analytics;
public class DeptName extends EvalFunc<String>
{
public String exec(Tuple v) throws IOException
{
int dno = (Integer)v.get(0);
String dname;
switch (dno){
case 11 :
dname = "Marketing";
break;
case 12 :
dname = "HR";
break;
case 13 :
dname = "Finance";
break;
default:
dname = "Others";
}
return dname;
}
}
---------------------------------
---------------------------
export into jar : Desktop/pigs.jar;
grunt> register Desktop/pigs.jar;
grunt> define gender pig.analytics.Gender();
grunt> define grade pig.analytics.Grade();
grunt> define dept pig.analytics.DeptName();
grunt> res = foreach emp generate
id, ucase(name) as name,
sal, grade(sal) as grade,
gender(sex) as sex,
dept(dno) as dname ;
---------------------------------
Very Well Written Article onHadoop Technology. Please Post More Post of this Technology To grab latest Updates and Information.
ReplyDeleteHadoop Training in Bangalore
Hadoop Training in Marathahalli
Extremely Elegantly composed Article adoption Innovation. It would be ideal if you Post More Post of this Innovation To snatch most recent Updates and Data.
ReplyDeleteBig data
Hi,
ReplyDeleteThanks for sharing the great information about Hadoop… Its useful and helpful information…Keep Sharing.
Thanks
Online Hadoop training
Hai,
ReplyDeleteIt's very nice blog
Thank you for giving valuable information on Hadoop
I'm expecting much more from you...
nice information on Hadoop training in hyderabad
ReplyDeletenice one.
ReplyDeleteHadoop Training in Gurgaon
Hello....your blog is awesome and thanks for Posting. i read your content thats very interesting so please keep it up.
ReplyDeleteHadoop Training Center in Gurgaon
its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end. Big Data Hadoop Online Training Hyderabad
ReplyDeleteThanks for sharing wonderfull blog Big Data Hadoop Training In Hyderabad
ReplyDeleteHi Sir, really appreciate the work you are doing here.
ReplyDeleteCan you please statistics all sessions in the blog. I am naive in Data Science area, so want to learn basics of statistics. Will be very helpful if you post them here.
ReplyDeleteHi Your Blog is very nice!!
Get All Top Interview Questions and answers PHP, Magento, laravel,Java, Dot Net, Database, Sql, Mysql, Oracle, Angularjs, Vue Js, Express js, React Js,
Hadoop, Apache spark, Apache Scala, Tensorflow.
Mysql Interview Questions for Experienced
php interview questions for freshers
php interview questions for experienced
python interview questions for freshers
tally interview questions and answers
codeingniter interview questions
cakephp interview questions
express Js interview questions
react js interview questions
laravel Interview questions and answers
Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.
ReplyDeleteBig data training in bangalore
Hadoop training institute in bangalore
Such a nice blog, I really like what you write in this blog, I also have some relevant Information about Best Core HR Training In Hyderabad | Core Hr training institute in Hyderabad! if you want more information.
ReplyDeleteExcellent blog I visit this blog it's really awesome. The important thing is that in this blog content written clearly and understandable. The content of information is very informative. Thanks for the excellent and great idea. keep blogging
ReplyDeleteBest HR Online Training Institute | Best Core HR Online Training Institute!
I am very happy to visit your blog. This is definitely helpful to me, eagerly waiting for more updates.
ReplyDeleteBlue Prism Training in Chennai
Blue Prism Training Institute in Chennai
UiPath Training in Chennai
Data Science Training in Chennai
RPA course in Chennai
RPA Training Institute in Chennai
Blue Prism Training in Anna Nagar
Blue Prism Training in T Nagar
Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart. I need a help for our website Visit our website once http://talentflames.com/index.html training with placementcompany in Hyderabad
ReplyDeleteThanks for sharing the good information and post more information. I need some facilitate to my website. please check once http://talentflames.com/
ReplyDeletetraining and placement company in Hyderabad