因此自定义InputFormat的核心是自定义一个实现接口RecordReader类似于LineRecordReader的类,该类的核心也正是重写接口RecordReader中的几大方法,
定义一个InputFormat的核心是定义一个类似于LineRecordReader的,自己的RecordReader
示例,数据每一行为 “物体,x坐标,y坐标,z坐标”
ball 3.5,12.7,9.0
car 15,23.76,42.23
device 0.0,12.4,-67.1
每一行将要被解析为<Text, Point3D>(Point3D是我们在上一篇日志中自定义的数据类型)
方式一,自定义的RecordReader使用中LineRecordReader,
publicclassObjectPositionInputFormatextends
FileInputFormat<Text, Point3D> {
publicRecordReader<Text, Point3D>getRecordReader(
InputSplit input, JobConf job, Reporter reporter)
throwsIOException {
reporter.setStatus(input.toString());
returnnewObjPosRecordReader(job, (FileSplit)input);
}
}
classObjPosRecordReaderimplementsRecordReader<Text, Point3D> {
privateLineRecordReader lineReader;
privateLongWritable lineKey;
privateText lineValue;
publicObjPosRecordReader(JobConf job, FileSplit split)throwsIOException {
lineReader =newLineRecordReader(job, split);
lineKey = lineReader.createKey();
lineValue = lineReader.createValue();
}
publicbooleannext(Text key, Point3D value)throwsIOException {
// get the next line
if(!lineReader.next(lineKey, lineValue)) {
returnfalse;
}
// parse the lineValue which is in the format:
// objName, x, y, z
String [] pieces = lineValue.toString().split(",");
if(pieces.length != 4) {
thrownewIOException("Invalid record received");
}
// try to parse floating point components of value
floatfx, fy, fz;
try{
fx = Float.parseFloat(pieces[1].trim());
fy = Float.parseFloat(pieces[2].trim());
fz = Float.parseFloat(pieces[3].trim());
}catch(NumberFormatException nfe) {
thrownewIOException("Error parsing floating point value in record");
}
// now that we know we'll succeed, overwrite the output objects
key.set(pieces[0].trim());// objName is the output key.
value.x = fx;
value.y = fy;
value.z = fz;
returntrue;
}
publicTextcreateKey() {
returnnewText("");
}
publicPoint3DcreateValue() {
returnnewPoint3D();
}
publiclonggetPos()throwsIOException {
returnlineReader.getPos();
}
publicvoidclose()throwsIOException {
lineReader.close();
}
publicfloatgetProgress()throwsIOException {
returnlineReader.getProgress();
}
}
方式二:自定义的RecordReader中使用LineReader,
publicclassObjectPositionInputFormatextendsFileInputFormat<Text, Point3D> {
@Override
protectedbooleanisSplitable(JobContext context, Path filename) {
//TODOAuto-generated method stub
returnfalse;
}
@Override
publicRecordReader<Text, Point3D>createRecordReader(InputSplit inputsplit,
TaskAttemptContext context)throwsIOException, InterruptedException {
//TODOAuto-generated method stub
returnnewobjPosRecordReader();
}
publicstaticclassobjPosRecordReaderextendsRecordReader<Text,Point3D>{
publicLineReader in;
publicText lineKey;
publicPoint3D lineValue;
publicStringTokenizer token=null;
publicText line;
@Override
publicvoidclose()throwsIOException {
//TODOAuto-generated method stub
}
@Override
publicTextgetCurrentKey()throwsIOException, InterruptedException {
//TODOAuto-generated method stub
System.out.println("key");
//lineKey.set(token.nextToken());
System.out.println("hello");
returnlineKey;
}
@Override
publicPoint3DgetCurrentValue()throwsIOException,
InterruptedException {
//TODOAuto-generated method stub
returnlineValue;
}
@Override
publicfloatgetProgress()throwsIOException, InterruptedException {
//TODOAuto-generated method stub
return0;
}
@Override
publicvoidinitialize(InputSplit input, TaskAttemptContext context)
throwsIOException, InterruptedException {
//TODOAuto-generated method stub
FileSplit split=(FileSplit)input;
Configuration job=context.getConfiguration();
Path file=split.getPath();
FileSystem fs=file.getFileSystem(job);
FSDataInputStream filein=fs.open(file);
in=newLineReader(filein,job);
line=newText();
lineKey=newText();
lineValue=newPoint3D();
}
@Override
publicbooleannextKeyValue()throwsIOException, InterruptedException {
//TODOAuto-generated method stub
intlinesize=in.readLine(line);
if(linesize==0)
returnfalse;
token=newStringTokenizer(line.toString());
String []temp=newString[2];
if(token.hasMoreElements()){
temp[0]=token.nextToken();
if(token.hasMoreElements()){
temp[1]=token.nextToken();
}
}
System.out.println(temp[0]);
System.out.println(temp[1]);
String []points=temp[1].split(",");
System.out.println(points[0]);
System.out.println(points[1]);
System.out.println(points[2]);
lineKey.set(temp[0]);
lineValue.set(Float.parseFloat(points[0]),Float.parseFloat(points[1]), Float.parseFloat(points[2]));
System.out.println("pp");
returntrue;
}
}
}
从以上可以看出,自定义一个InputFormat的核心是定义一个类似于LineRecordReader的,自己的RecordReader,而在其中可能会到LineReader/LineRecordReader/KeyValueLineRecordReader类
因此,要自定义InputFormat,这三个类的源码就必须很熟悉~
更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13
