zoukankan      html  css  js  c++  java
  • hadoop3--编写简单的map reduce

    运行结果附图

    本节课程主要内容为MapReduce基本原理,以及在MapReduce编程环境搭建。

    实验内容为:在Eclipse中编写对文本的字母进行计数的MapReduce程序,在本地调试成功后,将java工程打成jar包放到Hadoop集群上运行。

    1. 在linux下安装eclipse,建立新的java工程,并在该工程建立 user library,将已下载的Hadoop文件夹内的所有jar包添加到该library中。

      2015-03-30 08:50:40 的屏幕截图

    2. 编写java代码,实现文本的字母计数功能
      代码的主体部分如下:

       public class LetterCount {
      //继承Mapper,设置map的输入类型为<Object,Text>,输出类型为<Text,IntWritable>
      public static class Map extends Mapper<Object,Text,Text,IntWritable> {
      	private final static IntWritable one  =  new IntWritable(1);//one 为字母计数为1
      	private Text word  = new Text();
      	public void map(Object key,Text value,Context context)throws IOException,InterruptedException{
      		//value为文本,先将文本分割成一个个字符,对每一个字符判断是否为字母,并对其计数
      		String str = value.toString();
      		char[] ch = str.toCharArray();
      		for(int i=0;i<ch.length;i++){
      			if((ch[i]<=90&&ch[i]>=65)||(ch[i]<=122&&ch[i]>=97)){
      				word.set(String.valueOf(ch[i]));
      				context.write(word, one);
      			}
      		}	
      	}
      }
      //继承Reducer,设置reduce的输入类型为<Text,IntWritable>,输出类型为<Text,IntWritable>
      public static class Reduce extends Reducer <Text,IntWritable,Text,IntWritable>{
      	private static IntWritable result = new IntWritable();//result记录单词的频数
              public void reduce(Text key, Iterable<IntWritable> values,Context context)throws IOException, InterruptedException {
          	    int sum = 0;
          	    for (IntWritable val : values) {
          		sum += val.get();
          	    }
          	result.set(sum);//将频数存到result中
              context.write(key, result);//记下此key对应的计数
          }	
      }
      public static void main(String[] args) throws Exception{
      	//GenerciOptionsParser 用来明确namenode,jobtracker和其他的配置资源
      	Configuration conf = new Configuration();
      	String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
      	if(otherArgs.length!=2){
      		System.err.println("Usage LetterCount <int> <out>");
      		System.exit(2);
      	}
      	//创建作业
      	Job job  = new Job(conf,"letterCount");
      	//配置作业各个类
      	job.setJarByClass(LetterCount.class);
      	job.setMapperClass(Map.class);
      	job.setCombinerClass(Reduce.class);
      	job.setReducerClass(Reduce.class);
      	job.setOutputKeyClass(Text.class);
      	job.setOutputValueClass(IntWritable.class);
      	
      	FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//设置输入文件路径
      	FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//设置输出文件路径
      	System.exit(job.waitForCompletion(true) ? 0 : 1);	
       }
      

    }
    ```
    3. 编译程序运行结果

    输入文本
    
    ![2015-03-29 20:15:24 的屏幕截图](http://10.106.128.234/uploads/course/course_hadoop/69a17ccb21/2015-03-29_20_15_24_%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png)
    
    输出结果
    
    ![2015-03-29 20:15:09 的屏幕截图](http://10.106.128.234/uploads/course/course_hadoop/ac551d692b/2015-03-29_20_15_09_%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE.png)
    
    1. 在本机打jar包,复制到docker中的master主机上

      使用linux下的scp命令

      2015-03-29 22:31:10 的屏幕截图

    2. hadoop对wordcount文件执行此jar,实现字母计数功能

      2015-03-29 22:17:31 的屏幕截图

      2015-03-29 22:17:50 的屏幕截图

    3. 运行结果如下

      2015-03-29 22:20:07 的屏幕截图

      2015-03-29 22:20:59 的屏幕截图

    问题、心得与体会记录

    1. 在windows下MR编程环境搭建,需要设置环境变量,复制文件到os系统目录下等一系列工作,较为繁琐,而linux下直接可以在eclipse下编程,只要确保建立好java工程下的user library引用hadoop的一系列jar即可。

    2. 将本机的jar包文件复执到docker中的主机时,可以有多种方法:

      • 使用scp 命令 scp username@ip:dir1 dir2

        dir1为复制文件的源端路径,dir为目的端路径(前提:开启ssh服务)

      • 使用HUE,因为浏览器为本机的,所以在web页面,HUE可以访问本机下的文件,从而实现将本机文件上传到HDFS上

        然后可以在docker中使用hadoop fs -get命令将文件下载到master上。

  • 相关阅读:
    SharePoint 2010 User Profile Sync Service自动停止
    如何区别多个svchost.exe?
    Log Parser分析IIS log的一个简单例子
    Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
    Windows中右键点击文件夹, 结果找不到共享选项卡, 怎么办?
    介绍SOS中的SaveModule命令
    SharePoint中Draft版本的文档不会收到document added的Alert Email
    和我一起学Windows Workflow Foundation(1)创建和调试一个WF实例
    门户网站
    C#基础—— check、lock、using语句归纳
  • 原文地址:https://www.cnblogs.com/ivywenyuan/p/4579365.html
Copyright © 2011-2022 走看看