这里是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。]
是什么myudf。你的包UDFDate中有java文件myudf吗? –
'DateFormat.java'文件中的第一行是什么? –
对不起我们..我错过了包名.. – TKHN