手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop pipes编程

[日期:2012-04-02] 来源:Linux社区  作者:anbo724 [字体:大 中 小]
(2) Reducer编写方法
Reducer组件的编写方法跟Mapper组件类似,它需要继承虚基类public HadoopPipes::Reducer。
与Mapper组件唯一不同的地方时,map函数的参数类型为HadoopPipes::ReduceContext,它包含一个nextValue()方法,这允许用于遍历当前key对应的value列表,依次进行处理。
接下来介绍RecordReader, Partitioner和RecordWriter的编写方法:
(3) RecordReader编写方法
用户自定义的RecordReader类需要继承虚基类HadoopPipes::RecordReader,它的声明如下:
  1. class RecordReader: public Closable {  
  2.    
  3. public:  
  4.    
  5. virtual bool next(std::string& key, std::string& value) = 0;  
  6.    
  7. virtual float getProgress() = 0;  
  8.    
  9. };  
用户需要实现next和 getProgress两个方法。
用户自定义的RecordReader的构造函数可携带类型为HadoopPipes::MapContext的参数,通过该参数的getInputSplit()的方法,用户可以获取经过序列化的InpuSplit对象,Java端采用不同的InputFormat可导致InputSplit对象格式不同,但对于大多数InpuSplit对象,它们可以提供至少三个信息:当前要处理的InputSplit所在的文件名,所在文件中的偏移量,它的长度。用户获取这三个信息后,可使用libhdfs库读取文件,以实现next方法。
下面介绍一下反序列化InputSplit对象的方法:
【1】 如果Java端采用的InputFormat为WordCountInpuFormat,可以这样:
  1. lass XXXReader: public HadoopPipes::RecordReader {  
  2.    
  3. public:  
  4.    
  5. XXXReader (HadoopPipes::MapContext& context) {  
  6.    
  7. std::string filename;  
  8.    
  9. HadoopUtils::StringInStream stream(context.getInputSplit());  
  10.    
  11. HadoopUtils::deserializeString(filename, stream);  
  12.    
  13. ……  
  14.    
  15. };  
【2】 如果Java端采用的InputFormat为TextInpuFormat,可以这样:
  1. class XXXReader: public HadoopPipes::RecordReader {  
  2.    
  3. public:  
  4.    
  5. XXXReader (HadoopPipes::MapContext& context) {  
  6.    
  7. std::string filename;  
  8.    
  9. HadoopUtils::StringInStream stream(context.getInputSplit());  
  10.    
  11. readString(filename, stream);  
  12.    
  13. int start = (int)readLong(stream);  
  14.    
  15. int len = (int)readLong(stream);  
  16.    
  17. ……  
  18.    
  19. private:  
  20.    
  21. void readString(std::string& t, HadoopUtils::StringInStream& stream)  
  22.    
  23. {  
  24.    
  25. int len = readShort(stream);  
  26.    
  27. if (len > 0) {  
  28.    
  29. // resize the string to the right length   
  30.    
  31. t.resize(len);  
  32.    
  33. // read into the string in 64k chunks   
  34.    
  35. const int bufSize = 65536;  
  36.    
  37. int offset = 0;  
  38.    
  39. char buf[bufSize];  
  40.    
  41. while (len > 0) {  
  42.    
  43. int chunkLength = len > bufSize ? bufSize : len;  
  44.    
  45. stream.read(buf, chunkLength);  
  46.    
  47. t.replace(offset, chunkLength, buf, chunkLength);  
  48.    
  49. offset += chunkLength;  
  50.    
  51. len -= chunkLength;  
  52.    
  53. }  
  54.    
  55. } else {  
  56.    
  57. t.clear();  
  58.    
  59. }  
  60.    
  61. }  
  62.    
  63. long readLong(HadoopUtils::StringInStream& stream) {  
  64.    
  65. long n;  
  66.    
  67. char b;  
  68.    
  69. stream.read(&b, 1);  
  70.    
  71. n = (long)(b & 0xff) << 56 ;  
  72.    
  73. stream.read(&b, 1);  
  74.    
  75. n |= (long)(b & 0xff) << 48 ;  
  76.    
  77. stream.read(&b, 1);  
  78.    
  79. n |= (long)(b & 0xff) << 40 ;  
  80.    
  81. stream.read(&b, 1);  
  82.    
  83. n |= (long)(b & 0xff) << 32 ;  
  84.    
  85. stream.read(&b, 1);  
  86.    
  87. n |= (long)(b & 0xff) << 24 ;  
  88.    
  89. stream.read(&b, 1);  
  90.    
  91. n |= (long)(b & 0xff) << 16 ;  
  92.    
  93. stream.read(&b, 1);  
  94.    
  95. n |= (long)(b & 0xff) << 8 ;  
  96.    
  97. stream.read(&b, 1);  
  98.    
  99. n |= (long)(b & 0xff) ;  
  100.    
  101. return n;  
  102.    
  103. }  
  104.    
  105. };  
(4) Partitioner编写方法
用户自定义的Partitioner类需要继承虚基类HadoopPipes:: Partitioner,它的声明如下:
  1. class Partitioner {  
  2.    
  3. public:  
  4.    
  5. virtual int partition(const std::string& key, int numOfReduces) = 0;  
  6.    
  7. virtual ~Partitioner() {}  
  8.    
  9. };  
用户需要实现partition方法和 析构函数。
对于partition方法,框架会自动为它传入两个参数,分别为key值和reduce task的个数numOfReduces,用户只需返回一个0~ numOfReduces-1的值即可。linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款