Hadoop实战-初级部分 之 Hadoop集群的安装

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

java代码:
  1. Mapper   
  2.   
  3. public class WordMapper extends MapReduceBase implements  
  4.         Mapper<LongWritable, Text, Text, IntWritable> {   
  5.     private final static IntWritable one = new IntWritable(1);   
  6.     private Text word = new Text();   
  7.     @Override  
  8.     public void map(LongWritable key, Text value,OutputCollector<Text, IntWritable> output, Reporter reporter)   
  9.             throws IOException {   
  10.         String line = value.toString();   
  11.          for(String word : s.split("\\W+")){   
  12.                             if(word.length()>0){   
  13.                 output.collect(new Text(word),new IntWritable(1));   
  14.                      }   
  15.                         }      
  16.             }   
  17. }  
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代码:
  1. Reducer   
  2.   
  3. public class WordReducer extends MapReduceBase implements  
  4. Reducer<Text, IntWritable, Text, IntWritable>{   
  5.        
  6.     @Override  
  7.     public void reduce(Text key, Iterator<IntWritable> values,OutputCollector<Text, IntWritable> output, Reporter reporter)   
  8.             throws IOException {   
  9.         Int sum = 0;   
  10.         while (values.hasNext()) {   
  11.             sum += values.next().get()+sum;   
  12.         }   
  13.         output.collect(key, new IntWritable(sum));   
  14.     }   
  15.   
  16. }  
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来运行
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值