Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3008055
  • 博文数量: 167
  • 博客积分: 613
  • 博客等级: 中士
  • 技术积分: 5473
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-13 21:35
个人简介

人, 既无虎狼之爪牙,亦无狮象之力量,却能擒狼缚虎,驯狮猎象,无他,唯智慧耳。

文章分类
文章存档

2015年(19)

2014年(70)

2013年(54)

2012年(14)

2011年(10)

分类: HADOOP

2013-07-18 13:53:26

    
   Hadoop用于架设分布式系统。一般来说,实现多个系统并行计算需要考虑许多细节,比如说各个系统节点间的任务分配、跟踪与意外发生后的恢复等。Hadoop可以使我们这些“!分布式系统Expert”轻松地架设自己的分布式系统,写出分布式应用程序。Hadoop的一个关键实现利用了Map-Reduce的编程模型。Map阶段是Reduce阶段的一个准备,将Hadoop提供的分片进行处理,然后提交给Reduce程序处理提供结果。基本的数据流向如图:

   数据流首先进行了分片(每片64M,与HDFS的分块大小一致),然后每个分片会分配给一个map进行处理,之后针对reduce的数量产生对应的输出分片,这里原先的分片顺序会打乱,类似于洗牌,之后分别交给reduce处理后输出结果。在Hadoop框架下编写程序时可以使用多种语言,通常可以使用python或C++。直接调用Hadoop提供的输入输出结构/Pipes就可以实现程序。下面是一个Tom White提供的关于天气数据处理的一个C++实现例子,由此可见一窥:

点击(此处)折叠或打开

  1. //Hadoop MapReduce Example by C++
  2. #include <algorithm>
  3. #include <limits>
  4. #include <stdlib>
  5. #include <string>

  6. #include "~\hadoop\Pipes.hh"
  7. #include "~\hadoop\TemplateFactory.hh"
  8. #include "~\hadoop\StringUtils.hh"

  9. class MaxTemperatureMapper:public HadoopPipes::Mapper
  10. {
  11.       public :
  12.              MaxTemperatureMapper(HadoopPipes::TaskContext& context)
  13.              {
  14.                  }
  15.              void map(HadoopPipes::MapContext& context) //重写map函数用以实现自身操作
  16.              {
  17.                   std::string line = context.getInputValue();
  18.                   std::string year = line.substr(15,4);
  19.                   std::string airTemperature = line.substr(87,5);
  20.                   std::string q = line.substr(92,1);
  21.                   if (airTemperature != "+9999" && (q == '0' || q == '1' || q == '4'||q == '5'|| q == '9'))
  22.                   {
  23.                       context.emit(year, airTemperature);
  24.                   }
  25.               }
  26.       };

  27. class MapTemperatureReducer:public HadoopPipes::Reducer
  28. {
  29.       public : MapTemperatureReducer(HadoopPipes::TaskContext& context)
  30.       {
  31.              }
  32.       void reduce(hadoopPipes::ReduceContext& context) //重写了reduce函数
  33.       {
  34.            int maxValue = 0; //初始化一个最小值
  35.            while (context.nextValue())
  36.            {
  37.                  maxValue = std::(maxValue, HadoopUtils::toInt(context.getInputValue));
  38.                  }
  39.            context.emit(context.getInputKey(), HadoopUtils::toString(maxValue));
  40.        }
  41.       }
  42.       
  43. int main(int argc, char *argv[])
  44. {
  45.     return HadoopPipes::runTask(HadoopPipes::TemplateFactory<MaxTemperatureMapper, MapTemperatureReducer()>);
  46. }
    第一次看原版书,第一次接触Hadoop,还是有些迷糊,有些概念还是没有搞懂,先搁置在一边,继续向下看,慢慢就会懂了吧!
PS:学习资料:>, Tom White, O'REILLY
阅读(5844) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~