Transferred from: http://www.linuxidc.com/Linux/2012-04/57831.htm
The default linerecordreader is the key value when the map output is the offset of each line, the contents of each line as the value of the map, the default delimiter is carriage return and line wrapping.
Now to change the input <key,value> value of the map, the path (or filename) of the file corresponding to the key, value corresponds to the contents of the file (content).
Then we need to rewrite InputFormat and Recordreader, because Recordreader is called in InputFormat, of course, rewriting Recordreader is the point!
Here's a look at the rewrite of code InputFormat:
- Public class Chdicinputformat extends fileinputformat<text,text>
- implements jobconfigurable{
- Private compressioncodecfactory compressioncodecs = null;
- Public void Configure (jobconf conf) {
- Compressioncodecs = new compressioncodecfactory (conf);
- }
- /**
- * @brief issplitable file is not sliced, the file must be processed as a whole
- *
- * @param FS
- * @param file
- *
- * @return False
- */
- protected boolean issplitable (FileSystem FS, Path file) {
- //Compressioncodec codec = compressioncodecs.getcode (file);
- return false; //In file units, each unit as a split, even if the size of a single file exceeds 64M, that is, Hadoop a block size, also do not shard
- }
- Public recordreader<text,text> getrecordreader (Inputsplit genericsplit,
- jobconf job, Reporter Reporter) throws ioexception{
- Reporter.setstatus (Genericsplit.tostring ());
- return new Chdicrecordreader (Job, (Filesplit) genericsplit);
- }
- }
Here's a look at Recordreader's rewrite:
- Public class Chdicrecordreader implements recordreader<text,text> {
- private static final log log = Logfactory.getlog (chdicrecordreader. class. GetName ());
- Private compressioncodecfactory compressioncodecs = null;
- Private long start;
- Private long pos;
- Private long end;
- private byte[] buffer;
- private String keyName;
- private fsdatainputstream Filein;
- Public chdicrecordreader (Configuration job,filesplit split) throws ioexception{
- Start = Split.getstart (); ///from which you can see that each file is as a split
- End = Split.getlength () + start;
- Final path Path = Split.getpath ();
- KeyName = Path.tostring ();
- Log.info ("filename in HDFs is:" + keyName);
- Final FileSystem fs = Path.getfilesystem (Job);
- Filein = Fs.open (path);
- Filein.seek (start);
- Buffer = new byte[(int) (End-start)];
- This . pos = start;
- }
- Public Text CreateKey () {
- return new Text ();
- }
- Public Text createvalue () {
- return new Text ();
- }
- Public long getPos () throws ioexception{
- return pos;
- }
- Public float getprogress () {
- if (start = = end) {
- return 0. 0f;
- } Else {
- return math.min (1. 0f, (Pos-start)/(float) (End-start));
- }
- }
- Public boolean Next (text key, text value) throws ioexception{
- While (pos < end) {
- Key.set (KeyName);
- Value.clear ();
- Filein.readfully (Pos,buffer);
- Value.set (buffer);
- //Log.info ("---content:" + value.tostring ());
- pos + = Buffer.length;
- Log.info ("End is:" + end + "POS is:" + pos);
- return true;
- }
- return false;
- }
- Public void close () throws ioexception{
- if(filein! = null) {
- Filein.close ();
- }
- }
- }
This new read-in format is available by using the code above and then setting the InputFormat class in the main function.
"Reprint" Hadoop custom Recordreader