[关闭]
@boothsun 2017-10-22T02:31:15.000000Z 字数 3948 阅读 1603

Windows + IDEA 手动开发MapReduce程序

大数据


  1. 参见马士兵老师的博文:map_reduce

环境配置

Windows本地解压Hadoop压缩包,然后像配置JDK环境变量一样在系统环境变量里配置HADOOP_HOME和path环境变量。注意:hadoop安装目录尽量不要包含空格或者中文字符。

形如:

添加windows环境下依赖的库文件

  1. 把盘中(盘地址 提取码:s6uv)共享的bin目录覆盖HADOOP_HOME/bin目录下的文件。
  2. 如果还是不行,把其中hadoop.dll复制到C:\windows\system32目录下,可能需要重启机器。
  3. 注意:配置好之后不需要启动Windows上的Hadoop

pom.xml

  1. <!-- hadoop start -->
  2. <dependency>
  3. <groupId>org.apache.hadoop</groupId>
  4. <artifactId>hadoop-minicluster</artifactId>
  5. <version>2.7.4</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.hadoop</groupId>
  9. <artifactId>hadoop-client</artifactId>
  10. <version>2.7.4</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.hadoop</groupId>
  14. <artifactId>hadoop-assemblies</artifactId>
  15. <version>2.7.4</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.hadoop</groupId>
  19. <artifactId>hadoop-maven-plugins</artifactId>
  20. <version>2.7.4</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.hadoop</groupId>
  24. <artifactId>hadoop-common</artifactId>
  25. <version>2.7.4</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.apache.hadoop</groupId>
  29. <artifactId>hadoop-hdfs</artifactId>
  30. <version>2.7.4</version>
  31. </dependency>
  32. <!-- hadoop end -->

代码

WordMapper:

  1. public class WordMapper extends Mapper<Object,Text,Text,IntWritable> {
  2. private final static IntWritable one = new IntWritable(1);
  3. private Text word = new Text();
  4. @Override
  5. public void map(Object key , Text value , Context context) throws IOException, InterruptedException{
  6. StringTokenizer itr = new StringTokenizer(value.toString()) ;
  7. while (itr.hasMoreTokens()) {
  8. word.set(itr.nextToken());
  9. context.write(word,one);
  10. }
  11. }
  12. }

WordReducer:

  1. public class WordReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
  2. private IntWritable result = new IntWritable() ;
  3. public void reduce(Text key , Iterable<IntWritable> values, Context context) throws IOException , InterruptedException {
  4. int sum = 0 ;
  5. for (IntWritable val : values) {
  6. sum += val.get();
  7. }
  8. result.set(sum);
  9. context.write(key,result);
  10. }
  11. }

本地计算 + 本地HDFS文件

  1. public static void main(String[] args) throws Exception{
  2. //如果配置好环境变量,没有重启机器,然后报错找不到hadoop.home 可以手动指定
  3. // System.setProperty("hadoop.home.dir","E:\\hadoop\\hadoop-2.7.4");
  4. List<String> lists = Arrays.asList("E:\\input","E:\\output");
  5. Configuration configuration = new Configuration();
  6. Job job = new Job(configuration,"word count") ;
  7. job.setJarByClass(WordMain.class); // 主类
  8. job.setMapperClass(WordMapper.class); // Mapper
  9. job.setCombinerClass(WordReducer.class); //作业合成类
  10. job.setReducerClass(WordReducer.class); // reducer
  11. job.setOutputKeyClass(Text.class); // 设置作业输出数据的关键类
  12. job.setOutputValueClass(IntWritable.class); // 设置作业输出值类
  13. FileInputFormat.addInputPath(job,new Path(lists.get(0))); //文件输入
  14. FileOutputFormat.setOutputPath(job,new Path(lists.get(1))); // 文件输出
  15. System.exit(job.waitForCompletion(true) ? 0 : 1); //等待完成退出
  16. }

本地计算 + 远程HDFS文件

把远程HDFS文件系统中的文件拉到本地来运行。

相比上面的改动点:

  1. FileInputFormat.setInputPaths(job, "hdfs://master:9000/wcinput/");
  2. FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/wcoutput2/"));

注意这里是把HDFS文件拉到本地来运行,如果观察输出的话会观察到jobID带有local字样,同时这样的运行方式是不需要yarn的(自己停掉jarn服务做实验)。

远程计算 + 远程HDFS文件

这个方式是将文件打成一个jar文件,通过Hadoop Client自动上传到Hadoop集群,然后使用远程HDFS文件进行计算。

java代码:

  1. public static void main(String[] args) throws Exception{
  2. Configuration configuration = new Configuration();
  3. configuration.set("fs.defaultFS", "hdfs://master:9000/");
  4. configuration.set("mapreduce.job.jar", "target/wc.jar");
  5. configuration.set("mapreduce.framework.name", "yarn");
  6. configuration.set("yarn.resourcemanager.hostname", "master");
  7. configuration.set("mapreduce.app-submission.cross-platform", "true");
  8. Job job = new Job(configuration,"word count") ;
  9. job.setJarByClass(WordMain2.class); // 主类
  10. job.setMapperClass(WordMapper.class); // Mapper
  11. job.setCombinerClass(WordReducer.class); //作业合成类
  12. job.setReducerClass(WordReducer.class); // reducer
  13. job.setCombinerClass(WordReducer.class); //作业合成类
  14. job.setOutputKeyClass(Text.class); // 设置作业输出数据的关键类
  15. job.setOutputValueClass(IntWritable.class); // 设置作业输出值类
  16. FileInputFormat.setInputPaths(job, "/opt/learning/hadoop/wordcount/*.txt");
  17. FileOutputFormat.setOutputPath(job, new Path("/opt/learning/output7/"));
  18. System.exit(job.waitForCompletion(true) ? 0 : 1); //等待完成退出
  19. }

如果运行过程中遇到权限问题,配置执行时的虚拟机参数 -DHADOOP_USER_NAME=root 。

形如下图:

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注