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 的屏幕截图

    输出结果

    2015-03-29 20:15:09 的屏幕截图

  4. 在本机打jar包,复制到docker中的master主机上

    使用linux下的scp命令

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

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

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

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

  6. 运行结果如下

    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上。

转载于:https://www.cnblogs.com/ivywenyuan/p/4579365.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值