在hadoop下如何进行c++开发【入门版】

来源:互联网 发布:好听的淘宝名字 编辑:程序博客网 时间:2024/05/13 02:49

http://www.linuxidc.com/Linux/2012-07/65969.htm

如果遇到authenticate问题


请参考http://www.myexception.cn/ai/921448.html

然后再执行下列步骤

同时注意参考http://www.happylivelife.com/view/?hd=map%20reduce&o=http://www.happylivelife.com/r/?id=66

经过一上午的努力,终于以伪分布式模式运行了C++版的MapReduce.下面逐一介绍这一过程

一、先决条件

    1. 在Linux系统上已安装好Hadoop 1.0.X版本(本人系统为CentOS5.5(64位系统),hadoop版本为1.0.3,其它系统有可能不同)

    2. 了解hadoop基本概念

二、步骤(如果已具备该步骤下的条件可以跳过该步骤)

    1.修改$HADOOP_INSTALL/conf目录下的三个文件core-site.xml, hdfs-site.xml, mapred-site.xml分别如下:

  1. <?xml version="1.0"?>  
  • <!-- core-site.xml -->  
  • <configuration>  
  • <property>  
  • <name>fs.default.name</name>  
  • <value>hdfs://localhost/</value>  
  • </property>  
  • </configuration>  
  • <?xml version="1.0"?>  
  • <!-- hdfs-site.xml -->  
  • <configuration>  
  • <property>  
  • <name>dfs.replication</name>  
  • <value>1</value><!--only one copy-->  
  • </property>  
  • </configuration>  
  • <?xml version="1.0"?>  
  • <!-- mapred-site.xml -->  
  • <configuration>  
  • <property>  
  • <name>mapred.job.tracker</name>  
  • <value>localhost:8021</value>  
  • </property>  
  • </configuration>  

    2.配置SSH(即无密码访问本机,在CentOS5下比较麻烦)【遇到的困难1】

    1). 确认系统已经安装好OpenSSH的server 和client
      安装步骤这里不再讲述,不是本文的重点。
    2). 确认本机sshd的配置文件(需要root权限)
      $ vi /etc/ssh/sshd_config
      找到以下内容,并去掉注释符”#“
      RSAAuthentication yes
      PubkeyAuthentication yes
      AuthorizedKeysFile      .ssh/authorized_keys
    3).  如果修改了配置文件需要重启sshd服务 (需要root权限)
      $ /sbin/service sshd restart
    4). ssh登陆系统 后执行测试命令:
      $ ssh localhost
      回车会提示你输入密码,因为此时还没有生成证书
    5).生成证书公私钥的步骤:
      $ ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa
      $ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys
    6).测试登陆 ssh localhost:
      $ ssh localhost
      正常情况下会登陆成功,显示一些成功登陆信息,如果失败请看下面的 一般调试步骤
    7).若登录失败,很有可能是authorized_keys的权限问题,添加权限
     $ chmod 600 ~/.ssh/authorized_keys
    8).测试登陆 ssh localhost:(一般来说会成功!)


    3.格式化HDFS文件系统

    1)命令: Hadoop namenode -format

    4.启动守护进程(dfs和mapred,需要正确设置好前面三个文件,否则易出错)

    $ start-dfs.sh

    $ start-mapred.sh

    5.编写CPP文件,源码如下:

    1. #include <algorithm>  
    2. #include <limits>  
    3. #include <stdint.h>  
    4. #include <string>  
    5.   
    6. #include "hadoop/Pipes.hh"  
    7. #include "hadoop/TemplateFactory.hh"  
    8. #include "hadoop/StringUtils.hh"  
    9.   
    10. class MaxTemperatureMapper : public HadoopPipes::Mapper {  
    11. public:  
    12.   MaxTemperatureMapper(HadoopPipes::TaskContext& context) {  
    13.   }  
    14.   void map(HadoopPipes::MapContext& context) {  
    15.     std::string line = context.getInputValue();  
    16.     std::string year = line.substr(15, 4);  
    17.     std::string airTemperature = line.substr(87, 5);  
    18.     std::string q = line.substr(92, 1);  
    19.     if (airTemperature != "+9999" &&  
    20.         (q == "0" || q == "1" || q == "4" || q == "5" || q == "9")) {  
    21.       context.emit(year, airTemperature);  
    22.     }  
    23.   }  
    24. };  
    25.   
    26. class MapTemperatureReducer : public HadoopPipes::Reducer {  
    27. public:  
    28.   MapTemperatureReducer(HadoopPipes::TaskContext& context) {  
    29.   }  
    30.   void reduce(HadoopPipes::ReduceContext& context) {  
    31.     int maxValue = INT_MIN;  
    32.     while (context.nextValue()) {  
    33.       maxValue = std::max(maxValue, HadoopUtils::toInt(context.getInputValue()));  
    34.     }  
    35.     context.emit(context.getInputKey(), HadoopUtils::toString(maxValue));  
    36.   }  
    37. };  
    38.   
    39. int main(int argc, char *argv[]) {  
    40.   return HadoopPipes::runTask(HadoopPipes::TemplateFactory<MaxTemperatureMapper,   
    41.                               MapTemperatureReducer>());  
    42. }  

    6.编写Makefile文件,源码如下(注意:该文件和参考书《Hadoop权威指南中文第二版》有一点区别,添加了 "-lcrypto",将"-m32"改为"-m64")

    1. CC = g++  
    2. CPPFLAGS = -m64 -I$(HADOOP_INSTALL)/c++/$(PLATFORM)/include  
    3.   
    4. max_temperature: max_temperature.cpp   
    5.     $(CC) $(CPPFLAGS) $< -Wall -L$(HADOOP_INSTALL)/c++/$(PLATFORM)/lib -lhadooppipes -lcrypto -lhadooputils -lpthread -g -O2 -o $@  

    7.安装gcc, g++, 标准C库

    安装gcc,g++步骤
    1).添加软件更新源
    更新方法如下:
    先进入yum源配置目录
    cd /etc/yum.repos.d

    备份系统自带的yum源
    mv CentOS-Base.repo CentOS-Base.repo.save

    下载其他更快的yum源
    sudo wget http://centos.ustc.edu.cn/CentOS-Base.repo
    sudo wget http://mirrors.163.com/.help/CentOS-Base-163.repo
    sudo wget http://mirrors.sohu.com/help/CentOS-Base-sohu.repo

    更新完yum源后,建议更新一下,使操作立即生效
    yum makecache

    2).安装gcc
    sudo yum install gcc -y

    3).安装g++(sudo yum install g++ 报告无法找到g++包,原来这个包的名字叫做gcc-c++。完整的应该是sudo yum install gcc-c++)
    sudo yum install gcc-c++ -y

    4).安装标准C库

    sudo yum install glibc-devel -y

    8.安装openssl,命令如下

    cd /usr/local/src 
    sudo tar zxvf openssl-0.9.8l.tar.gz
    sudo cd openssl-0.9.8l
    sudo ./config 
    sudo make 
    make install 
    sudo cp libcrypto.a /usr/local/lib
    sudo cp libssl.a /usr/local/lib

        9.编译源文件,即max_temperature.cpp

         1)设置环境变量: exoprt PLATFORM=Linux-amd64-64

         2)在max_temperature.cpp所在目录下执行make命令

       10.上传文件到HDFS

         1)上传本地可执行文件max_temperature到HDFS的bin/max_temperature目录下: Hadoop fs -put max_temperature bin/max_temperature

         2)上传数据到HDFS: hadoop fs -put sample.txt sample.txt

         3)sample.txt内容如下:

    1. 0067011990999991950051507004+68750+023550FM-12+038299999V0203301N00671220001CN9999999N9+00001+99999999999  
    2. 0043011990999991950051512004+68750+023550FM-12+038299999V0203201N00671220001CN9999999N9+00221+99999999999  
    3. 0043011990999991950051518004+68750+023550FM-12+038299999V0203201N00261220001CN9999999N9-00111+99999999999  
    4. 0043012650999991949032412004+62300+010750FM-12+048599999V0202701N00461220001CN0500001N9+01111+99999999999  
    5. 0043012650999991949032418004+62300+010750FM-12+048599999V0202701N00461220001CN0500001N9+00781+99999999999  

    11.使用hadoop pipes命令运行作业,命令如下:

         hadoop pipes -D hadoop.pipes.java.recordreader=true -D hadoop.pipes.java.recordwriter=true -input sample.txt -output output -program bin/max_temperature

        12.查看作业执行结果:

         hadoop fs -cat output/*

         若结果为

    1949    111
    1950    22

        表明上述11项设置正确。(庆祝一下,...~O(∩_∩)O~...)



  • 原创粉丝点击