"Reprint" Hadoop custom Recordreader

Source: Internet
Author: User

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:

  1. Public class Chdicinputformat extends fileinputformat<text,text>
  2. implements jobconfigurable{
  3. Private compressioncodecfactory compressioncodecs = null;
  4. Public void Configure (jobconf conf) {
  5. Compressioncodecs = new compressioncodecfactory (conf);
  6. }
  7. /**
  8. * @brief issplitable file is not sliced, the file must be processed as a whole
  9.      *
  10. * @param FS
  11. * @param file
  12.      *
  13. * @return False
  14.      */
  15. protected boolean issplitable (FileSystem FS, Path file) {
  16. //Compressioncodec codec = compressioncodecs.getcode (file);
  17. 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
  18. }
  19. Public recordreader<text,text> getrecordreader (Inputsplit genericsplit,
  20. jobconf job, Reporter Reporter) throws ioexception{
  21. Reporter.setstatus (Genericsplit.tostring ());
  22. return new Chdicrecordreader (Job, (Filesplit) genericsplit);
  23. }
  24. }

Here's a look at Recordreader's rewrite:

  1. Public class Chdicrecordreader implements recordreader<text,text> {
  2. private static final log log = Logfactory.getlog (chdicrecordreader.   class. GetName ());
  3. Private compressioncodecfactory compressioncodecs = null;
  4. Private long start;
  5. Private long pos;
  6. Private long end;
  7. private byte[] buffer;
  8. private String keyName;
  9. private fsdatainputstream Filein;
  10. Public chdicrecordreader (Configuration job,filesplit split) throws ioexception{
  11. Start = Split.getstart (); ///from which you can see that each file is as a split
  12. End = Split.getlength () + start;
  13. Final path Path = Split.getpath ();
  14. KeyName = Path.tostring ();
  15. Log.info ("filename in HDFs is:" + keyName);
  16. Final FileSystem fs = Path.getfilesystem (Job);
  17. Filein = Fs.open (path);
  18. Filein.seek (start);
  19. Buffer = new byte[(int) (End-start)];
  20. This . pos = start;
  21. }
  22. Public Text CreateKey () {
  23. return new Text ();
  24. }
  25. Public Text createvalue () {
  26. return new Text ();
  27. }
  28. Public long getPos () throws ioexception{
  29. return pos;
  30. }
  31. Public float getprogress () {
  32. if (start = = end) {
  33. return 0. 0f;
  34. } Else {
  35. return math.min (1. 0f, (Pos-start)/(float) (End-start));
  36. }
  37. }
  38. Public boolean Next (text key, text value) throws ioexception{
  39. While (pos < end) {
  40. Key.set (KeyName);
  41. Value.clear ();
  42. Filein.readfully (Pos,buffer);
  43. Value.set (buffer);
  44. //Log.info ("---content:" + value.tostring ());
  45. pos + = Buffer.length;
  46. Log.info ("End is:" + end + "POS is:" + pos);
  47. return true;
  48. }
  49. return false;
  50. }
  51. Public void close () throws ioexception{
  52. if(filein! = null) {
  53. Filein.close ();
  54. }
  55. }
  56. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.