Hadoop之——MapReduce job的几种运行模式 问题

Hadoop之——MapReduce job的几种运行模式

需要的jar包 \share\hadoop\common下的jar和其子目录下lib中的jar

\share\hadoop\hdfs下的jar和其子目录下lib中的jar

\share\hadoop\mapreduce下的jar和其子目录下lib中的jar

\share\hadoop\yarn下的jar和其子目录下lib中的jar


WordCountMapper.java

[html]  view plain  copy
  1. package com.test.hadoop.mr.wordcount;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.commons.lang.StringUtils;  
  6. import org.apache.hadoop.io.LongWritable;  
  7. import org.apache.hadoop.io.Text;  
  8. import org.apache.hadoop.mapreduce.Mapper;  
  9.   
  10. public class WordCountMapper extends Mapper<LongWritable, Text, Text, LongWritable> {  
  11.     @Override  
  12.     protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, LongWritable>.Context context)  
  13.             throws IOException, InterruptedException {  
  14.         // 获取到一行文件的内容  
  15.         String line = value.toString();  
  16.         // 切分这一行的内容为一个单词数据  
  17.         String[] words = StringUtils.split(line, " ");  
  18.         // 遍历输出 <word,1>  
  19.         for (String word : words) {  
  20.             context.write(new Text(word), new LongWritable(1));  
  21.         }  
  22.     }  
  23. }  
WordCountReducer.java

[html]  view plain  copy
  1. package com.test.hadoop.mr.wordcount;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.io.LongWritable;  
  6. import org.apache.hadoop.io.Text;  
  7. import org.apache.hadoop.mapreduce.Reducer;  
  8.   
  9. public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {  
  10.   
  11.     // key:hello ,values:{1,1,1,1,....}  
  12.     @Override  
  13.     protected void reduce(Text key, Iterable<LongWritable> values,  
  14.             Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {  
  15.         // 定义一个累加计数器  
  16.         long count = 0;  
  17.         for (LongWritable value : values) {  
  18.             count += value.get();  
  19.         }  
  20.         // 输出<单词:count>键值对  
  21.         context.write(key, new LongWritable(count));  
  22.     }  
  23. }  
WordCountRunner.java
[html]  view plain  copy
  1. package com.test.hadoop.mr.wordcount;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.Path;  
  7. import org.apache.hadoop.io.LongWritable;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapreduce.Job;  
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  12.   
  13. /**  
  14.  * 用来描述一个作业job(使用哪个mapper类,哪个reducer类,输入文件在哪,输出结果放哪。。。。) 然后提交这个job给hadoop集群  
  15.  *   
  16.  * @author Administrator com.test.hadoop.mr.wordcount.WordCountRunner  
  17.  */  
  18. public class WordCountRunner {  
  19.     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
  20.   
  21.         Configuration conf = new Configuration();  
  22.   
  23.         Job wcjob = Job.getInstance(conf);  
  24.   
  25.         // 设置wcjob中的资源所在的jar包  
  26.         wcjob.setJarByClass(WordCountRunner.class);  
  27.   
  28.         // wcjob 要使用哪个mapper类  
  29.         wcjob.setMapperClass(WordCountMapper.class);  
  30.   
  31.         // wcjob要使用哪个reducer类  
  32.         wcjob.setReducerClass(WordCountReducer.class);  
  33.   
  34.         // wcjob的mapper类输出的kv数据类型  
  35.         wcjob.setMapOutputKeyClass(Text.class);  
  36.         wcjob.setMapOutputValueClass(LongWritable.class);  
  37.   
  38.         // wcjob的reducer类输出的kv数据类型  
  39.         wcjob.setOutputKeyClass(Text.class);  
  40.         wcjob.setOutputValueClass(LongWritable.class);  
  41.   
  42.         // 指定要处理的原始数据存放的路径  
  43.         // FileInputFormat.setInputPaths(wcjob, "D:/wc/words.txt");  
  44.         FileInputFormat.setInputPaths(wcjob, "hdfs://192.168.169.128:9000/wc/words.txt");  
  45.   
  46.         // 指定处理之后的结果输出到哪个路径  
  47.         FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://192.168.169.128:9000/wc/output"));  
  48.   
  49.         boolean res = wcjob.waitForCompletion(true);  
  50.   
  51.         System.exit(res ? 0 : 1);  
  52.     }  
  53. }  
1、在eclipse中开发好mr程序(windows或linux下都可以),然后打成jar包(hadoop-mapreduce.jar),上传到服务器       执行命令    hadoop jar hadoop-mapreduce.jar com.test.hadoop.mr.wordcount.WordCountRunner

      这种方式会将这个job提交到yarn集群上去运行


2、在Linux的eclipse中直接启动Runner类的main方法,这种方式可以使job运行在本地,也可以运行在yarn集群
      ----究竟运行在本地还是在集群,取决于一个配置参数
             mapreduce.framework.name == yarn (local)

      ----如果确实需要在eclipse中提交到yarn执行,必须做好以下两个设置
            a/将mr工程打成jar包(wc.jar),放在工程目录下,  把/opt/soft/hadoop-2.7.3/etc/hadoop/目录中的core-site.xml,hdfs-site.xml,mapred-site.xml,yarn-site.xml拷贝到src下
            b/在工程的main方法中,加入一个配置参数   conf.set("mapreduce.job.jar","hadoop-mapreduce.jar");

3、在windows的eclipse中运行本地模式,步骤为:
     ----a、在windows中找一个地方放一份hadoop的安装包,并且将其bin目录配到环境变量中 
     ----b、根据windows平台的版本(32?64?win7?win8?),替换掉hadoop安装包中的本地库(bin,lib)
     ----c、mr程序的工程中不要有参数mapreduce.framework.name的设置

[html]  view plain  copy
  1. package com.test.hadoop.mr.wordcount;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.Path;  
  7. import org.apache.hadoop.io.LongWritable;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapreduce.Job;  
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  12.   
  13. /**  
  14.  * 用来描述一个作业job(使用哪个mapper类,哪个reducer类,输入文件在哪,输出结果放哪。。。。) 然后提交这个job给hadoop集群  
  15.  *   
  16.  * @author Administrator com.test.hadoop.mr.wordcount.WordCountRunner  
  17.  */  
  18. public class WordCountRunner {  
  19.     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {  
  20.             //System.setProperty("hadoop.home.dir", "D:/BaiduYunDownload/hadoop-2.7.3");  
  21.         Configuration conf = new Configuration();  
  22.   
  23.         Job wcjob = Job.getInstance(conf);  
  24.   
  25.         // 设置wcjob中的资源所在的jar包  
  26.         wcjob.setJarByClass(WordCountRunner.class);  
  27.   
  28.         // wcjob 要使用哪个mapper类  
  29.         wcjob.setMapperClass(WordCountMapper.class);  
  30.   
  31.         // wcjob要使用哪个reducer类  
  32.         wcjob.setReducerClass(WordCountReducer.class);  
  33.   
  34.         // wcjob的mapper类输出的kv数据类型  
  35.         wcjob.setMapOutputKeyClass(Text.class);  
  36.         wcjob.setMapOutputValueClass(LongWritable.class);  
  37.   
  38.         // wcjob的reducer类输出的kv数据类型  
  39.         wcjob.setOutputKeyClass(Text.class);  
  40.         wcjob.setOutputValueClass(LongWritable.class);  
  41.   
  42.         // 指定要处理的原始数据存放的路径  
  43.         // FileInputFormat.setInputPaths(wcjob, "D:/wc/words.txt");  
  44.         FileInputFormat.setInputPaths(wcjob, "hdfs://192.168.169.128:9000/wc/words.txt");  
  45.   
  46.         // 指定处理之后的结果输出到哪个路径  
  47.         // FileOutputFormat.setOutputPath(wcjob, new Path("D:/wc/output"));  
  48.         FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://192.168.169.128:9000/wc/output"));  
  49.   
  50.         boolean res = wcjob.waitForCompletion(true);  
  51.   
  52.         System.exit(res ? 0 : 1);  
  53.     }  
  54. }  
在windows的eclipse中运行本地模式
出现的错误:

1.“Could not locate executable null\bin\winutils.exe in the Hadoop binaries”

“Could not locate executable null\bin\winutils.exe in the Hadoop binaries”
1.1 缺少winutils.exe
Could not locate executable null \bin\winutils.exe in the hadoop binaries
1.2 缺少hadoop.dll
Unable to load native-hadoop library for your platform… using builtin-Java classes where applicable

解决办法
下载资源
http://download.csdn.NET/detail/lizhiguo18/8764975。首先将hadoop.dll和winutils.exe放到hadoop的bin目录下,
还是不可以,重启电脑或者在WordCountRunner加代码System.setProperty("hadoop.home.dir", "D:/BaiduYunDownload/hadoop-2.7.3")(本地的hadoop所存的目录。);


2.org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z

 分析:
    C:\Windows\System32下缺少hadoop.dll,把这个文件拷贝到C:\Windows\System32下面即可。
 解决:
    hadoop-common-2.2.0-bin-master下的bin的hadoop.dll放到C:\Windows\System32下,然后重启电脑,也许还没那么简单,还是出现这样的问题。
    
 
3. org.apache.hadoop.security.AccessControlException: Permission denied: user=Administrator, access=WRITE, inode="/wc/output2/_temporary/0":root:supergroup:drwxr-xr-x

解决办法:WordCountRunner中加

用来指明登陆hadoop的用户
System.setProperty("HADOOP_USER_NAME", "hadoop上的用户名");

或者

右键->Run Configurations->Arguments->VM arguments 加入:-DHADOOP_USER_NAME=root


4、在windows的eclipse中运行main方法来提交job到集群执行,比较麻烦
      ----a、类似于方式3中所描述的对本地库兼容性进行改造
      ----b、修改YarnRunner这个类  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值