Hadoop实战-初级部分 之 Hadoop MapReduce JAVA API

来源:互联网 发布:十三姨 知乎 斗鱼 编辑:程序博客网 时间:2024/04/28 12:58

第一部分:Word Count 程序讲解
 
•编写一个MapReduce 程序的步骤
–编写一个Mapper类
–编写一个Reducer类
–编写一个Driver类(即Job),来将Mapper与Reducer类来进行组合。
 
 

java代码Mapper
public class WordMapper extends MapReduceBase implements          Mapper<LongWritable, Text, Text, IntWritable> {      private final static IntWritable one = new IntWritable(1);      private Text word = new Text();      @Override      public void map(LongWritable key, Text value,OutputCollector<Text, IntWritable> output, Reporter reporter)              throws IOException {          String line = value.toString();           for(String word : s.split("\\W+")){                              if(word.length()>0){                  output.collect(new Text(word),new IntWritable(1));                       }                          }                 }  }  



java代码Reducer
public class WordReducer extends MapReduceBase implements 
Reducer<Text, IntWritable, Text, IntWritable>{            @Override      public void reduce(Text key, Iterator<IntWritable> values,OutputCollector<Text, IntWritable> output, Reporter reporter)              throws IOException {          Int sum = 0;          while (values.hasNext()) {              sum += values.next().get()+sum;          }          output.collect(key, new IntWritable(sum));      }    }

  
第二部分:Mapper API 介绍
•老版Mapper API
– org.apache.hadoop.mapred Interface Mapper<K1,V1,K2,V2>
 
•新版Mapper  API
– org.apache.hadoop.mapreduce Class Mapper<KEYIN,VALUEIN,KEYOUT,VALUEOUT>
 
第三部分:Reducer API 介绍
•老版 Reducer API
– org.apache.hadoop.mapred Interface Reducer<K2,V2,K3,V3>
 
•新版 Reducer API
– org.apache.hadoop.mapreduce Class Reducer<KEYIN,VALUEIN,KEYOUT,VALUEOUT>
 
第四部分:Job运行模式
•MapReduce程序可以以以下三种模式运行
–Local(Standalone) Mode:只有一个 Java 虚拟机在跑,完全没有分布式的成分。且不使用HDFS文件系统,而是使用本机的Linux文件系统。
–Pseudo-distributed Mode:在同一台机器上启动独立数个 JVM 进程,每一个hadoop daemon运行在一个单独的JVM进程中,进行“伪分布式”操作。
–Fully-distributed Mode:真正的可以运行于多台机器上的分布式模式。其中, Standalone mode 使用local filesystem 以及 local MapReducer job runner, Distributed mode 使用HDFS 以及 MapReduce daemons
 
•对应的配置文件 conf/core-site.xml:
    为Hadoop设定默认的文件系统
 
 <configuration>
  <property>
         <name> fs.default.name </name>
         <value>       VALUE      </value>
</property>
</configuration>
 
Standalone mode:    VALUE=file:///
Pseudo-distributed mode: VALUE=hdfs://localhost:9000
Fully-Distributed mode:   VALUE=hdfs://namenode
•对应的配置文件 conf/mapred-site.xml
<configuration>
 <property>
     <name> mapred.job.tracker </name>
      <value>            VALUE         </value>
 </property>
</configuration>
 
Standalone mode:          VALUE=local
Pseudo-distributed mode: VALUE=localhost:9001
Fully-Distributed mode:   VALUE=jobtracker:9001
HDFS client使用这个属性来决定NameNode的位置,这样HDFS client就可以连接到该NameNode.
第五部分:集群上运行Word Count
•打包
•启动
•MapReduce网络用户界面
•获取结果
•调试作业
 
 
•打包
步骤
在项目上,选择[File]=>Export,导出项目为一个jar包
•启动
– 步骤
hadoop jar yourjar.jar mainClassName  -conf inputfolder outputfolder
 
•MapReduce网络用户界面
– url
        http://localhost:50030/
•获取结果
–Hadoop fs –ls outputfolder
•调试作业
–加入传统的Log输出
–使用Reporter 来做错误源的输出比对
第六部分:Mapreduce 工作流
•如何将问题分解成MapReduce作业 
 –复杂的需求
  •在Word Count 程序中,求出单词出现频率的总和
  •单词中包含大写字母H的则转换为小写
  •在Word Count 程序中,求出单词出现频率的总和与单词的个数
•运行独立的作业
  –假设有Job1,Job2,需要运行
   •如果作业是线性的
      JobClinet.runjob(conf1)
      JobClinet.runjob(conf2)
   •如果更负责的是环形的
  –
  –可以通过Hadoop自带的JobControl来运行
 
转载请注明出处【 http://sishuok.com/forum/blogPost/list/0/5461.html 
资源附件

Hadoop MapReduce JAVA API.pdf





原文地址:点击打开链接

0 0