MapReduce的数据去重功能

实验材料及说明
现有某电商网站用户对商品的收藏数据,记录了用户收藏的商品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的数据去重功能。即,要求根据所给材料,根据商品ID进行去重,统计有哪些商品被用户收藏了。
二、实验环境
(1)Linux Ubuntu 18.XX
(2)jdk-8u162-linux-x64
(3)hadoop-2.7.1
(4)hadoop-2.7.1-eclipse.jar
三、实验原理或流程
实验原理或流程需要包括以下内容:
(1)清晰描述要解决的问题以及其背后的原理
数据去重的目的是让原始数据中出现次数超过一次的数据在输出文件中只出现一次。我们自然想到将相同key值的所有value记录交到一台reduce机器,让其无论这个数据出现多少次,最终结果只输出一次。具体就是reduce的输出应该以数据作为key,而对value-list没有要求,当reduce接收到一个时,就直接将key复制到输出的key中,将value设置为空。
map阶段采用Hadoop的默认的作业输入方式,把输入的value用split()方法截取,截取出的商品id字段设置为key,设置value为空,然后直接输出
map输出的键值对经过shuffle过程,聚成后,会交给reduce函数。reduce函数,不管每个key 有多少个value,它直接将输入的赋值给输出的key,将输出的value设置为空,然后输出就可以了。
(2)用流程图表示算法的实现过程
MapReduce的数据去重功能_第1张图片
四、实验内容
实验内容需要包括以下内容:
(1)问题分析
本实验要求根据商品ID进行去重,统计有哪些商品被用户收藏了。具体就是reduce的输入应该以数据作为key,在这里先将把输入的value用split()方法截取,截取出的商品id字段设置为key,而对value-list则没有要求(可以设置为空)。当reduce接收到一个时就直接将输入的key复制到输出的key中,并将value设置成空值,然后输出
(2)重要的实验过程截图
1.切换到/usr/local/hadoop/目录下,启动hadoop。
./sbin/start-dfs.sh
MapReduce的数据去重功能_第2张图片
2.本实验上传的数据与MapReduce统计排序相同,故此次试验不再上传。有需要可以查阅上一篇文章。
3.编写代码,运行出结果
MapReduce的数据去重功能_第3张图片
MapReduce的数据去重功能_第4张图片
(3)代码调试说明
1、将输入的value用split()方法截取,将截取出的商品ID字段设置为key
MapReduce的数据去重功能_第5张图片
2、shuffle自动根据key值进行排序
MapReduce的数据去重功能_第6张图片

附完整源代码:

package yang;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.mapreduce.Mapper.Context;
import java.io.IOException;
public class quchong {
	public static class Map extends Mapper {
        private static Text newKey=new Text();//从输入中得到的每行的数据的类型
        private static Text newValue=new Text();
        public void map(Object key,Text value,Context context) throws IOException, InterruptedException {
        	  String line=value.toString();
              if (line!=null) {
               String arr[]=line.split("   ");//把输入的value用split()方法截取
               newKey.set(arr[1]);//截取出的商品id字段设置为key
              newValue.set(arr[0]);
            context.write(newKey, newValue);
              }
        }
	}
	
        public static class Reduce extends Reducer {
            public static Text myValue = new Text();
            public void reduce(Text key,Iterable values,Context context) throws IOException, InterruptedException{
                String myList = new String();
                for (Text value : values)
                    myList += value.toString() + ";";//输出所有收藏了该商品的用户id
                myValue.set(myList);
                context.write(key,myValue);
            }
        }
     
        public static void main(String[] args) throws Exception{
            Configuration conf=new Configuration();
            Job job =new Job(conf,"quchong");
            job.setJarByClass(quchong.class);
     
            job.setMapperClass(Map.class);
            job.setReducerClass(Reduce.class);
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(Text.class);
     
            Path in=new Path("hdfs://localhost:9000/shiyan/in/shangpin11.txt");
            Path out=new Path("hdfs://localhost:9000/shiyan/out3");
            FileInputFormat.addInputPath(job,in);
            FileOutputFormat.setOutputPath(job,out);
            System.exit(job.waitForCompletion(true) ? 0 : 1);
        }
	}

结果截图:
MapReduce的数据去重功能_第7张图片

你可能感兴趣的:(大数据,mapreduce,hadoop,大数据)