实验材料及说明
现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,文件名为buyer_favorite。buyer_favorite包含:买家id,商品id,收藏日期这三个字段,数据以“\t”分割,样本数据及格式如下:
买家ID 商品ID 收藏日期
10181 1000481 2010-04-04 16:54:31
20001 1001597 2010-04-07 15:07:52
20001 1001560 2010-04-07 15:08:27
20042 1001368 2010-04-08 08:20:30
20067 1002061 2010-04-08 16:45:33
20056 1003289 2010-04-12 10:50:55
20056 1003290 2010-04-12 11:57:35
20056 1003292 2010-04-12 12:05:29
20054 1002420 2010-04-14 15:24:12
20055 1001679 2010-04-14 19:46:04
20054 1010675 2010-04-14 15:23:53
20054 1002429 2010-04-14 17:52:45
20076 1002427 2010-04-14 19:35:39
20054 1003326 2010-04-20 12:54:44
20056 1002420 2010-04-15 11:24:49
20064 1002422 2010-04-15 11:35:54
20056 1003066 2010-04-15 11:43:01
20056 1003055 2010-04-15 11:43:06
20056 1010183 2010-04-15 11:45:24
20056 1002422 2010-04-15 11:45:49
20056 1003100 2010-04-15 11:45:54
20056 1003094 2010-04-15 11:45:57
20056 1003064 2010-04-15 11:46:04
20056 1010178 2010-04-15 16:15:20
20076 1003101 2010-04-15 16:37:27
20076 1003103 2010-04-15 16:37:05
20076 1003100 2010-04-15 16:37:18
20076 1003066 2010-04-15 16:37:31

实验目的

掌握MapReduce的统计排序功能。即要求根据所给材料统计每个买家收藏的商品数量,并按照收藏的商品数从大到小排列。

处理算法流程图


①MapReduce实现统计

package  shiyan;
import java.io.IOException;
import java.util.StringTokenizer;
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.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class shangpin {public static class doMapper extends Mapper<Object, Text, Text, IntWritable> {//第一个Object表示输入key的类型;第二个Text表示输入value的类型;第三个Text表示表示输出键的类型;第四个IntWritable表示输出值的类型  public static final IntWritable one = new IntWritable(1);public static Text word = new Text();@Overrideprotected void map(Object key, Text value, Context context) throws IOException, InterruptedException {//抛出异常 StringTokenizer tokenizer = new StringTokenizer(value.toString(), " ");//以空格分割//StringTokenizer是Java工具包中的一个类,用于将字符串进行拆分 word.set(tokenizer.nextToken());//返回当前位置到下一个分隔符之间的字符串context.write(word, one);//将word存到容器中,记一个数}}public static class doReducer extends Reducer<Text, IntWritable, Text, IntWritable> {private IntWritable result = new IntWritable();protected void reduce(Text key, Iterable<IntWritable> values, Context context)throws IOException, InterruptedException {int sum = 0;for (IntWritable value : values) {sum += value.get();}result.set(sum);context.write(key, result);}}public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {Job job = Job.getInstance();job.setJobName("shangpin");job.setJarByClass(shangpin.class);job.setMapperClass(doMapper.class);job.setReducerClass(doReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);Path in=new Path("hdfs://localhost:9000/shiyan/in/shangpin11.txt");Path out=new Path("hdfs://localhost:9000/shiyan/out");FileInputFormat.addInputPath(job, in);FileOutputFormat.setOutputPath(job, out);boolean flag = job.waitForCompletion(true);System.out.println(flag);System.exit(flag? 0 : 1);}}

②MapReduce实现降序排列

package shiyan;
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.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class paixu {public static class Map extends Mapper<Object , Text , IntWritable,Text >{private static Text goods=new Text();private static IntWritable num=new IntWritable();public void map(Object key,Text value,Context context)throws IOException,InterruptedException{String line=value.toString();String arr[]=line.split("\t");num.set(Integer.parseInt(arr[1]));goods.set(arr[0]);context.write(num, goods);}}public static class Reduce extends Reducer< IntWritable, Text, IntWritable, Text>//input is temperature (IntWritable) and data (Text){//private static IntWritable result= new IntWritable();public void reduce(IntWritable key,Iterable<Text> values,Context context) throws IOException, InterruptedException{//reduce端接收到<key,value-list>之后,//将输入的key直接复制给输出的key,用for循环遍历value-list并将里面的元素设置为输出的value,//然后将<key,value>逐一输出,根据value-list中元素的个数决定输出的次数。for(Text val:values){System.out.println("-------"+val);context.write(key,val);  //output data of the same temperature}}}//使Sort阶段的Key降序排列的比较器public static class IntWritableDecreasingComparator extendsIntWritable.Comparator {public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {return -super.compare(b1, s1, l1, b2, s2, l2);}}public static void main(String[] args) throws IOException,ClassNotFoundException,InterruptedException{Configuration conf=new Configuration();Job job =new Job(conf,"sort");job.setJarByClass(paixu.class);job.setMapperClass(Map.class);job.setReducerClass(Reduce.class);job.setOutputKeyClass(IntWritable.class);job.setOutputValueClass(Text.class);job.setInputFormatClass(TextInputFormat.class);job.setOutputFormatClass(TextOutputFormat.class);Path in=new Path("hdfs://localhost:9000/shiyan/out/part-r-00000");Path out=new Path("hdfs://localhost:9000/shiyan/out2");FileInputFormat.addInputPath(job,in);FileOutputFormat.setOutputPath(job,out);job.setSortComparatorClass(IntWritableDecreasingComparator.class);System.exit(job.waitForCompletion(true)?0:1);}
}

MapReduce的统计和排序功能相关推荐

  1. Hadoop实战——MapReduce对英文单词文本进行统计和排序(超详细教学,算法分析)

    B站视频操作过程 Hadoop实战--对单词文本进行统计和排序_哔哩哔哩_bilibili 更多MapReduce设计案例地址 https://github.com/yuanprogrammer/Ma ...

  2. 编写MapReduce程序,统计每个买家收藏商品数量,实现统计排序功能

    实验材料及说明 在Ubuntu系统的/学号(每个人用自己的学号)/salesInfo目录下,有买家的购买记录文件Sales,该文件记录了买家的id,购买商品的id以及购买日期,文件为名为Sales.S ...

  3. MapReduce基础开发之一词汇统计和排序(wordcount)

    统计/var/log/boot.log中含k的字符的数量,并对含k的字符按照数量排序.需分两个job完成,一个用来统计,一个用来排序. 一.统计 1.上传文件到hadoop:    1)新建文件夹:h ...

  4. MapReduce的数据去重功能

    实验材料及说明 现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品id以及收藏日期,文件名为buyer_favorite.buyer_favorite包含:买家id,商品id,收藏日期这三个字段 ...

  5. 降序排序_新手需要掌握的Excel排序功能都在这儿了!

    排序,真的是一个很基础的功能. 如果还不知道排序的话,可能仅仅是用Excel来做数据录入了. 但是排序真的又不是一个简单的功能,能全部了解其中细节的人真的不多,今天分享关于排序的内容,不讲太深,仅仅是 ...

  6. html表格筛选排序规则,excel表的排序功能你真的会吗?带你重新认识真正的排序功能...

    Excel的排序功能是很有用处的,但是很多朋友不会用.这里就举例一个例子介绍一下这个功能的使用方法.如一张期末成绩分析表,它是随机排列的,我们想把这些学生按英语成绩大小排列,应该怎么做呢? 1.使用E ...

  7. 详细讲解MapReduce二次排序过程

    2019独角兽企业重金招聘Python工程师标准>>> 我在15年处理大数据的时候还都是使用MapReduce, 随着时间的推移, 计算工具的发展, 内存越来越便宜, 计算方式也有了 ...

  8. AngularJS table 按照表头字段排序功能(升序和降序)

    一.表格按照表头排序 1 <!doctype html> 2 <html ng-app="a3_4"> 3 <head> 4 <title ...

  9. Hadoop学习笔记—11.MapReduce中的排序和分组

    Hadoop学习笔记-11.MapReduce中的排序和分组 一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出 ...

最新文章

  1. bzoj 2724[Violet 6]蒲公英
  2. 论文解读丨无参数的注意力模块SimAm
  3. linux内存管理(十四)-内存OOM触发分析
  4. element下拉框回显问题
  5. Python 学习笔记 - 不断更新!
  6. 意图识别 聊天机器人_如何解决聊天机器人中的意图冲突
  7. [iOS]让你的应用支持新iPad的Retina显示屏
  8. C# Serialization(序列化)
  9. java计算机毕业设计小区物业管理系统录像展示.mp4源程序+mysql+系统+lw文档+远程调试
  10. Python利用bs4批量抓取网页图片并下载保存至本地
  11. 计算机辅助教育课件有哪些类型,多媒体计算机辅助教学 (2).ppt
  12. 2020年7月各大城市与程序员平均工资排行榜
  13. leaflet加载接入腾讯矢量、腾讯影像地图(leaflet篇.4)
  14. leetcode881.救生艇(中等)
  15. 强制隐藏windows任务栏(使用快捷键才弹出)(hide the taskbar in win10)
  16. tp6+layui后台管理系统
  17. 《张艺谋这个人》较真
  18. 2022,2021英语六级全套资料自提,阿里云网盘链接,不限速度
  19. 食物链 (POJ-1182)
  20. [附源码]JAVA+ssm网上游戏商店设计(程序+Lw)

热门文章

  1. svn 目录结构 trunk java_如何彻底删除SVN中的文件和文件夹(附恢复方法)
  2. 如何组织成功的bug bash--摘录
  3. LeetCode 7. 整数反转 Reverse Integer 官网答案的条件判定的一点思考
  4. 调用个别f5 负载端口为80的vs时,返回值为空的问题
  5. 使用python操作redis及简单应用
  6. [转] React Hot Loader 3 beta 升级指南
  7. Mysql中分页查询两个方法比较
  8. 大数相乘、大数相加、大数相减Java版本
  9. “630”后逆变器售价下滑 企业如何应战?
  10. 中国LINUX内核开发大会 ppt演讲资料 与 会议视频