1 public class ObjectPositionInputFormat extends 2 FileInputFormat<Text, Point3D> { 3 4 public RecordReader<Text, Point3D> getRecordReader( 5 InputSplit input, JobConf job, Reporter reporter) 6 throws IOException { 7 8 reporter.setStatus(input.toString()); 9 return new ObjPosRecordReader(job, (FileSplit)input); 10 } 11 } 12 class ObjPosRecordReader implements RecordReader<Text, Point3D> { 13 private LineRecordReader lineReader; 14 private LongWritable lineKey; 15 private Text lineValue; 16 17 public ObjPosRecordReader(JobConf job, FileSplit split) throws IOException { 18 lineReader = new LineRecordReader(job, split); 19 20 lineKey = lineReader.createKey(); 21 lineValue = lineReader.createValue(); 22 } 23 24 public boolean next(Text key, Point3D value) throws IOException { 25 // get the next line 26 if (!lineReader.next(lineKey, lineValue)) { 27 return false; 28 } 29 30 // parse the lineValue which is in the format: 31 // objName, x, y, z 32 String [] pieces = lineValue.toString().split(","); 33 if (pieces.length != 4) { 34 throw new IOException("Invalid record received"); 35 } 36 37 // try to parse floating point components of value 38 float fx, fy, fz; 39 try { 40 fx = Float.parseFloat(pieces[1].trim()); 41 fy = Float.parseFloat(pieces[2].trim()); 42 fz = Float.parseFloat(pieces[3].trim()); 43 } catch (NumberFormatException nfe) { 44 throw new IOException("Error parsing floating point value in record"); 45 } 46 // now that we know we'll succeed, overwrite the output objects 47 key.set(pieces[0].trim()); // objName is the output key. 48 value.x = fx; 49 value.y = fy; 50 value.z = fz; 51 return true; 52 } 53 public Text createKey() { 54 return new Text(""); 55 } 56 public Point3D createValue() { 57 return new Point3D(); 58 } 59 public long getPos() throws IOException { 60 return lineReader.getPos(); 61 } 62 public void close() throws IOException { 63 lineReader.close(); 64 } 65 public float getProgress() throws IOException { 66 return lineReader.getProgress(); 67 } 68 }
1 public class ObjectPositionInputFormat extends FileInputFormat<Text, Point3D> { 2 @Override 3 protected boolean isSplitable(JobContext context, Path filename) { 4 // TODO Auto-generated method stub 5 return false; 6 } 7 @Override 8 public RecordReader<Text, Point3D> createRecordReader(InputSplit inputsplit, 9 TaskAttemptContext context) throws IOException, InterruptedException { 10 // TODO Auto-generated method stub 11 return new objPosRecordReader(); 12 } 13 public static class objPosRecordReader extends RecordReader<Text,Point3D>{ 14 15 public LineReader in; 16 public Text lineKey; 17 public Point3D lineValue; 18 public StringTokenizer token=null; 19 public Text line; 20 @Override 21 public void close() throws IOException { 22 // TODO Auto-generated method stub 23 } 24 @Override 25 public Text getCurrentKey() throws IOException, InterruptedException { 26 // TODO Auto-generated method stub 27 System.out.println("key"); 28 //lineKey.set(token.nextToken()); 29 System.out.println("hello"); 30 return lineKey; 31 } 32 @Override 33 public Point3D getCurrentValue() throws IOException, 34 InterruptedException { 35 // TODO Auto-generated method stub 36 return lineValue; 37 } 38 @Override 39 public float getProgress() throws IOException, InterruptedException { 40 // TODO Auto-generated method stub 41 return 0; 42 } 43 @Override 44 public void initialize(InputSplit input, TaskAttemptContext context) 45 throws IOException, InterruptedException { 46 // TODO Auto-generated method stub 47 FileSplit split=(FileSplit)input; 48 Configuration job=context.getConfiguration(); 49 Path file=split.getPath(); 50 FileSystem fs=file.getFileSystem(job); 51 52 FSDataInputStream filein=fs.open(file); 53 in=new LineReader(filein,job); 54 55 line=new Text(); 56 lineKey=new Text(); 57 lineValue=new Point3D(); 58 } 59 @Override 60 public boolean nextKeyValue() throws IOException, InterruptedException { 61 // TODO Auto-generated method stub 62 int linesize=in.readLine(line); 63 if(linesize==0) 64 return false; 65 token=new StringTokenizer(line.toString()); 66 String []temp=new String[2]; 67 if(token.hasMoreElements()){ 68 temp[0]=token.nextToken(); 69 if(token.hasMoreElements()){ 70 temp[1]=token.nextToken(); 71 } 72 } 73 System.out.println(temp[0]); 74 System.out.println(temp[1]); 75 String []points=temp[1].split(","); 76 System.out.println(points[0]); 77 System.out.println(points[1]); 78 System.out.println(points[2]); 79 lineKey.set(temp[0]); 80 lineValue.set(Float.parseFloat(points[0]),Float.parseFloat(points[1]), Float.parseFloat(points[2])); 81 System.out.println("pp"); 82 return true; 83 } 84 } 85 }