Package com.mr.test;
Import java.io.IOException;
Import org.apache.hadoop.io.BytesWritable;
Import org.apache.hadoop.io.LongWritable;
Import Org.apache.hadoop.mapreduce.InputSplit;
Import Org.apache.hadoop.mapreduce.RecordReader;
Import Org.apache.hadoop.mapreduce.TaskAttemptContext;
Import Org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;
Import Org.apache.hadoop.mapreduce.lib.input.CombineFileRecordReader;
Import Org.apache.hadoop.mapreduce.lib.input.CombineFileSplit;
public class Combinesmallfileinputformat extends Combinefileinputformat<longwritable, byteswritable> { @Override public recordreader<longwritable, byteswritable> Createrecordreader (Inputsplit split, Taskattemptcontext context) throws IOException {Combinefilesplit combinefilesplit = (combinefilesplit) s
Plit; Combinefilerecordreader<longwritable, byteswritable> recordreader = new combinefilerecordreader<Longwritable, byteswritable> (combinefilesplit, context, combinesmallfilerecordreader.class);
try {recordreader.initialize (combinefilesplit, context); The catch (Interruptedexception e) {new RuntimeException ("Error to initialize Combinesmallfilerecordreader.")
);
return recordreader; }
}
Package com.mr.test;
Import java.io.IOException;
Import Org.apache.hadoop.fs.Path;
Import org.apache.hadoop.io.BytesWritable;
Import org.apache.hadoop.io.LongWritable;
Import Org.apache.hadoop.mapreduce.InputSplit;
Import Org.apache.hadoop.mapreduce.RecordReader;
Import Org.apache.hadoop.mapreduce.TaskAttemptContext;
Import Org.apache.hadoop.mapreduce.lib.input.CombineFileSplit;
Import Org.apache.hadoop.mapreduce.lib.input.FileSplit;
Import Org.apache.hadoop.mapreduce.lib.input.LineRecordReader; public class Combinesmallfilerecordreader extends Recordreader<longwritable, byteswritable> {private C
Ombinefilesplit Combinefilesplit;
Private Linerecordreader Linerecordreader = new Linerecordreader ();
Private path[] paths;
private int totallength;
private int currentindex;
private float currentprogress = 0;
Private Longwritable Currentkey; Private byteswritable CurrentValue = new BYTESWRItable (); Public Combinesmallfilerecordreader (Combinefilesplit combinefilesplit, Taskattemptcontext context, Integer index)
Throws IOException {super ();
This.combinefilesplit = Combinefilesplit; This.currentindex = index; The small file block currently being processed is indexed in Combinefilesplit} @Override public void Initialize (Inputsplit split, Taskatt Emptcontext context) throws IOException, interruptedexception {this.combinefilesplit = (combinefilesplit) split
; Handles a small file block in Combinefilesplit, because using Linerecordreader, you need to construct a Filesplit object before you can read the data filesplit filesplit = new Fil Esplit (Combinefilesplit.getpath (Currentindex), Combinefilesplit.getoffset (Currentindex),
Combinefilesplit.getlength (Currentindex), combinefilesplit.getlocations ());
Linerecordreader.initialize (filesplit, context);
This.paths = Combinefilesplit.getpaths ();
Totallength = Paths.length; Context.getconfiguration (). Set ("Map.input.file.name", Combinefilesplit.getpath (Currentindex). GetName ()); @Override public longwritable Getcurrentkey () throws IOException, interruptedexception {Curr
Entkey = Linerecordreader.getcurrentkey ();
return currentkey; } <strong><span style= "color: #ff0000;" > @Override public byteswritable getcurrentvalue () throws IOException, interruptedexception {System.ou
T.println ("Linerecordreader:" +linerecordreader.getcurrentvalue (). toString ());
byte[] content = Linerecordreader.getcurrentvalue (). toString (). GetBytes ();
System.out.println ("Content:" +new String (content));
CurrentValue = new Byteswritable ();
Currentvalue.set (content, 0, content.length);
System.out.println ("CurrentValue:" +new String (Currentvalue.getbytes ()));
return currentvalue; }</span></strong> public static void Main (String args[]) {byteswritable CV = new byteswritable ();
String str1 = "1234567";
String str2 = "123450";
Cv.set (Str1.getbytes (), 0, Str1.getbytes (). length);
System.out.println (New String (Cv.getbytes ()));
Cv.setcapacity (0);
Cv.set (Str2.getbytes (), 0, Str2.getbytes (). length);
System.out.println (New String (Cv.getbytes ())); @Override public boolean Nextkeyvalue () throws IOException, interruptedexception {if (currentindex
>= 0 && Currentindex < totallength) {return linerecordreader.nextkeyvalue ();
else {return false; @Override public float getprogress () throws IOException {if Currentindex >= 0 &A
mp;& Currentindex < totallength) {currentprogress = (float) currentindex/totallength;
return currentprogress; Return to currentProgress;
@Override public void Close () throws IOException {linerecordreader.close (); }
}
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/database/extra/