2016-07-29 85 views
1

这里是UDF代码猪的Java版本UDF

package myudf; 
import java.io.IOException; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

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

public class DateFormat extends EvalFunc<String> { 
    public String exec(Tuple input) throws IOException { 
     if (input == null || input.size() == 0) { 
      return null; 
     } 

     try { 
      String dateStr = (String)input.get(0); 
      SimpleDateFormat readFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss.SSS aa"); 
      SimpleDateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
      Date date = null; 
      try { 
       date = readFormat.parse(dateStr); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 

      return writeFormat.format(date).toString(); 
     } catch(Exception e) { 
      throw new IOException("Caught exception processing input row ", e); 
     } 
    } 
} 

导出的这一个Jar和咕噜注册

Register /local/path/to/UDFDate.jar; 
    A = LOAD 'hdfs date file'; 
    B = FOREACH A GENERATE UDFDate.myudf.DateFormat($0); 

提供错误

[主要] ERROR org.apache。 pig.tools.grunt.Grunt - 错误1070:不能 使用导入来解析UDFDate.DateFormat:[,java.lang。, org.apache.pig.builtin。, org.apache.pig.impl.builtin。]

+0

是什么myudf。你的包UDFDate中有java文件myudf吗? –

+0

'DateFormat.java'文件中的第一行是什么? –

+0

对不起我们..我错过了包名.. – TKHN

回答

1

你不需要指定jar名称(UDFDate.myudf.DateFormat)来调用jar中的函数。它应该是“packageName.className”(myudf.DateFormat)。


如果DateFormatmyudf包,那么你应该运行为:

B = FOREACH A GENERATE myudf.DateFormat($0);


如果DateFormatdefault包,那么你应该为正在运行:

B = FOREACH A GENERATE DateFormat($0);

+0

工作!!!!!谢谢 – TKHN

0

打电话给你的UDF为:

packagename.classname($0); 
0

答案已经给出了已经但为了基本上不会重新定义UDF调用每次可以将其简化:

Register /local/path/to/UDFDate.jar; 
DEFINE myDateFormat myudf.DateFormat(); 
A = LOAD 'hdfs date file'; 
B = FOREACH A GENERATE myDateFormat($0);