Reputation: 33223
I am new into java world.. But basically I am trying to write a user-defined-function in pig-latin.
The following is the relevant code.
public class time extends EvalFunc<String>{
public String exec(Tuple input) throws IOException {
if ((input == null) || (input.size() == 0))
return null;
try{
String time = (String) input.get(0) ;
DateFormat df = new SimpleDateFormat("hh:mm:ss.000");
Date date = df.parse(time);
String timeOfDay = getTimeOfDay(date);
return timeOfDay;
} catch (IOException e) {
throw e;
}
}
So basically input is a tuple... I do a check whether the tuple is empty or not.. And then get convert that date string to time object.. and then parse the time part.. and then the function
getTimeOfDay(date) returns a string... like breakfast, lunch dinner.. or empty string depending on the time hours..
Now the issue is that my eclipse says and error (red line) in
Date date = df.parse(time);
String timeOfDay = getTimeOfDay(date);
says
Unhandled exception type ParseException
But no matter what I try (gives me 3 options.. add catch clause to surrounding try, add exception to existing catch block and surround with try/catch..), the error shifts.. but is always tehre.
And i am not even sure that I can change the structure of program..(the method declaration etc)..
How do I resolve this.
A quick guide on udf http://wiki.apache.org/pig/UDFManual
If you know pig.. or knows an easy method.. then basically what i am trying to do is .. given an input string of type "time,id,amount" check at what time of the day the transaction was made?
THanks
Upvotes: 0
Views: 227
Reputation: 10086
throws
declaration will automatically do that.So your code should be:
public class time extends EvalFunc<String>{
public String exec(Tuple input) throws IOException {
if ((input == null) || (input.size() == 0))
return null;
try{
String time = (String) input.get(0) ;
DateFormat df = new SimpleDateFormat("hh:mm:ss.000");
Date date = df.parse(time);
String timeOfDay = getTimeOfDay(date);
return timeOfDay;
} catch (ParseException e) {
//how will I handle when df.parse(time) fails and throws ParseException?
//maybe:
return null;
}
} //exec
} //class
Upvotes: 1