1. 目录

    编程实现按日期统计访问次数

    2.编程实现按访问次数排序

    获取成绩表最高分

    编译jar'包方法


  2. 编程实现按日期统计访问次数

    (1) 定义输入/输出格式

    社交网站用户的访问日期在格式上属于文本格式,访问次数为整型数值格式。其组成的键值对为<访问日期,访问次数>,因此Mapper的输出与Reducer的输出都选用Text类与IntWritable类。

    (2)Mapper 类的逻辑实现

    Mapper类中最主要的部分就是map函数。map函数的主要任务就是读取用户访问文件中的数据,输出所有访问日期与初始次数的键值对。因此访问日期是数据文件的第二列,所有先定义一个数组,再提取第二个元素,与初始次数1一起构成要输出的键值对,即<访问日期,1>。

    (3)Reducer的逻辑实现

    Reducer类中最主要的部分就是reduce函数。reduce的主要任务就是读取Mapper输出的键值对<访问日期,1>。这一部分与官网给出的WordCount中的Reducer完全相同

  3. 完整代码

    
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.Mapper;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public class MyKubi {public static class MyMapperextends Mapper<Object,Text,Text,IntWritable>{private final static IntWritable one = new IntWritable(1);public void map(Object key, Text value, Context context)throws IOException, InterruptedException{String line = value.toString();String array[] = line.split(",");String keyOutput = array[1];context.write(new Text(keyOutput),one);}}public static class MyReducerextends Reducer<Text,IntWritable,Text,IntWritable>{private IntWritable result = new IntWritable();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);context.write(key, result);}}public static void main(String[] args) throws Exception{Configuration conf = new Configuration();Job job=Job.getInstance(conf, "Daily Access Count");job.setJarByClass(MyKubi.class);job.setMapperClass(MyMapper.class);job.setReducerClass(MyReducer.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);for (int i = 0; i< args.length - 1; ++i){FileInputFormat.addInputPath(job,  new Path(args[i]));}FileOutputFormat.setOutputPath(job,new Path(args[args.length - 1]));System.exit(job.waitForCompletion(true) ? 0 : 1); }
    }
    

    将文件编译生成jar包上传到集群(生成jar包方法在后面)

  • 将数据文件上传的虚拟机在上传到hdfs
  • 在jar包的目录下执行  命令 hadoop jar (jar包名) /(数据文件目录) /(输出结果目录)、
  • 2.编程实现按访问次数排序

  • MapReduce只会对键值进行排序,所以我们在Mapper模块中对于输入的键值对,把Key与Value位置互换,在Mapper输出后,键值对经过shuffle的处理,已经变成了按照访问次数排序的数据顺序啦,输出格式为<访问次数,日期>。Reducer的处理和Mapper恰好相反,将键和值的位置互换,输出格式变为<日期,访问次数>。
  • 完整代码
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.Mapper;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    public class AccessTimesSort {// Mapper模块public static class MyMapperextends Mapper<Object, Text, IntWritable,Text>{public void map(Object key, Text value, Context context) //map函数的编写要根据读取的文件内容和业务逻辑来写throws IOException, InterruptedException {String line = value.toString();String array[] = line.split("\t");//指定,为分隔符,组成数组int keyOutput = Integer.parseInt(array[1]);//提取数组中的访问次数作为KeyString valueOutput = array[0]; //将日期作为valuecontext.write(new IntWritable(keyOutput), new  Text(valueOutput));}}// Reducer模块public static class MyReducerextends Reducer<IntWritable,Text,Text,IntWritable> {//注意与上面输出对应public void reduce(IntWritable key, Iterable<Text> values, Context  context)  throws IOException, InterruptedException {for (Text val : values) {context.write(val, key);                //进行键值位置互换}}}//Driver模块,主要是配置参数public static void main(String[] args) throws Exception {Configuration conf = new Configuration();Job job = Job.getInstance(conf, "AccessTimesSort");job.setJarByClass(AccessTimesSort.class);job.setMapperClass(MyMapper.class);job.setReducerClass(MyReducer.class);job.setMapOutputKeyClass(IntWritable.class);job.setMapOutputValueClass(Text.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);for (int i = 0; i < args.length - 1; ++i) {FileInputFormat.addInputPath(job, new Path(args[i]));}FileOutputFormat.setOutputPath(job,new Path(args[args.length - 1]));System.exit(job.waitForCompletion(true) ? 0 : 1);}
    }

    执行方法如上

  • 获取成绩表最高分

  • 在 Mapper 类中,map 函数读取成绩表 A 中的数据,直接将读取的数据以空格分隔,组成键值对<科目,成绩>,即设置输出键值对类型为<Text,IntWritable>。
    在 Reduce 中,由于 map 函数输出键值对类型是<Text,IntWritable>,所以 Reducer 接收的键值对是<Text,Iterable<IntWritable>>。针对相同的键(即科目),遍历比较它的值(即成绩),找出最高值(即最高成绩),最后输出键值对<科目,最高成绩>。
  • 完整代码
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.Mapper;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;public class FindMax {public static class FindMaxMapper extends Mapper<LongWritable, Text,Text,IntWritable>{Text course = new Text();IntWritable score = new IntWritable();@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String [] values = value.toString().trim().split(" ");course.set(values[0]);score.set(Integer.parseInt(values[1]));context.write(course,score);}}public static class FindMaxReducer extends Reducer<Text,IntWritable,Text,IntWritable>{@Overrideprotected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int maxScore = -1;Text course = new Text();for(IntWritable score:values){if (score.get()>maxScore){maxScore = score.get();course = key;}}context.write(course,new IntWritable(maxScore));}}public static void main(String [] args) throws Exception{
    //        if (args.length != 2){
    //            System.out.println("FindMax <input> <output>");
    //            System.exit(-1);
    //        }Configuration conf = new Configuration();Job job = Job.getInstance(conf,"findmax");job.setJarByClass(FindMax.class);job.setMapperClass(FindMaxMapper.class);job.setReducerClass(FindMaxReducer.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);job.setNumReduceTasks(1);FileInputFormat.addInputPath(job,new Path(args[0]));FileSystem.get(conf).delete(new Path(args[1]),true);FileOutputFormat.setOutputPath(job,new Path(args[1]));System.out.println(job.waitForCompletion(true) ? 0 : 1);}
    }

    编译jar'包方法

  • 右键点击要编译的文件选择Export
  • Java——JARfile
  • 选择classpath project——next

点击两次next,选择存放路径,选择文件

********在执行命令是不要忘记将数据文件上传到hdfs************

MapReduce编程练习相关推荐

  1. Mapreduce编程1之WordCount

    Mapreduce是hadoop的计算框架,对数据的处理操作都要在这里编程来实现功能. 这是我学习的第一个程序,也算是入门程序,相当于其他语言的helloworld,虽然还有很多不懂的地方,但相信通过 ...

  2. java mapreduce编程_Hadoop实验——MapReduce编程(1)

    实验目的 通过实验掌握基本的MapReduce编程方法. 掌握用MapReduce解决一些常见的数据处理问题,包括数据去重.数据排序和数据挖掘等. 通过操作MapReduce的实验,模仿实验内容,深入 ...

  3. 大数据之hadoop伪集群搭建与MapReduce编程入门

    一.理论知识预热 一句话介绍hadoop: Hadoop的核心由分布式文件系统HDFS与Map/Reduce计算模型组成. (1)HDFS分布式文件系统 HDFS由三个角色构成: 1)NameNode ...

  4. mapreduce编程实例(1)-统计词频

    今天开始把MapReduce Design Patterns这本书上的mapreduce例子过一遍,我觉得这本书对学mapreduce编程非常好,把这本书看完了,基本上能遇到的mapreduce问题也 ...

  5. MapReduce编程实战之“调试”

    本篇内容 在上一篇的"初识"环节,我们已经在本地和Hadoop集群中,成功的运行了几个MapReduce程序,对MapReduce编程,已经有了最初的理解. 在本篇文章中,我们对M ...

  6. MapReduce编程实战之“初识”

    MapReduce是什么 MapReduce是Hadoop(这种大数据处理生态环境)的编程模型. 既然称为模型,则意味着它有固定的形式. MapReduce编程模型,就是Hadoop生态环境进行数据分 ...

  7. MapReduce编程基础

    MapReduce编程基础 1. WordCount示例及MapReduce程序框架 2.  MapReduce程序执行流程 3.  深入学习MapReduce编程(1) 4. 参考资料及代码下载 & ...

  8. Hadoop大数据--Mapreduce编程规范及入门示例

    Mapreduce是一个分布式的运算编程框架,核心功能是将用户编写的核心逻辑代码分布式地运行在一个集群的很多服务器上. Mapreduce的存在价值 (1)海量数据在单机上处理因为硬件资源限制,无法胜 ...

  9. [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想

    Ubuntu系统 (我用到版本号是140.4) ubuntu系统是一个以桌面应用为主的Linux操作系统,Ubuntu基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一 ...

  10. MapReduce编程实践

    一.MapReduce编程思想 学些MapRedcue主要是学习它的编程思想,在MR的编程模型中,主要思想是把对数据的运算流程分成map和reduce两个阶段: Map阶段:读取原始数据,形成key- ...

最新文章

  1. Http环境下的保持连接方式
  2. linux 权限 x 表示,在Linux系统中,用户对目录拥有“x”权限,表示可以执行下列哪种操作?...
  3. android 加载进度,Android实现图片加载进度提示
  4. linux网络编程、socket编程
  5. 本地 Windows 如何将 Web 工程部署到远程 Windows 主机上
  6. GARFIELD@03-26-2005
  7. 关于SubSonic3.0插件使用SubSonic.Query.Select查询时,字段类型为tinyint时列丢失问题的Bug修复...
  8. SCI期刊分区/期刊名词,看完秒懂~
  9. 常见搜索引擎蜘蛛大全
  10. php oop思想
  11. 2018美团实习笔试
  12. N63043-郝子轩-第二周
  13. Java+MySQL实现网络爬虫程序
  14. OMML2MML.XSL 微软数学标记语言源码
  15. python123第四周_百度杯十月第四周WriteUp
  16. 关于注册时验证邮箱,并实现类似安卓的吐丝效果
  17. webstorm 一次Git使用很卡的处理记录
  18. IEEE ICIP 2019 | 更快更好的联邦学习:一种特征融合方法
  19. teradata数据库分析函数_TERADATA中函数的使用
  20. spark:Action算子:show()

热门文章

  1. 有道云笔记导出为pdf
  2. 什么是多线程?如何实现多线程?
  3. 海盗分金币问题 【转载】
  4. 转载航模无人机的电池一款充电器ISDT 备忘
  5. batchnorm原理及代码详解(笔记2)
  6. linux实操篇之-----vi和vim编辑器
  7. Java学习之八皇后
  8. 无法播放Blob视频文件问题
  9. MacBook Pro Late 2013 在2020年通过APPLE官方更换电池
  10. LiveGBS国标流媒体平台-海康NVR摄像机自带物联网卡摄像头注册GB/T28181国标平台看不到设备的时候如何抓包及排查