Mapreduce《案例之数据去重复》

本文介绍了一个使用MapReduce实现的数据去重案例。通过两个文件中的示例数据,展示了如何利用MapReduce处理并去除重复记录的过程。代码示例中包含了Mapper和Reducer的具体实现。

Mapreduce《案例之数据去重复》

源数据:

a.txt内容:

2012-3-1 b

2012-3-2 a

2012-3-3 b

2012-3-4 d

2012-3-5 a

2012-3-6 c

2012-3-7 d

2012-3-3 c

 

b.txt内容:

2012-3-1 a

2012-3-2 b

2012-3-3 c

2012-3-4 d

2012-3-5 a

2012-3-6 b

2012-3-7 c

2012-3-3 c

 

输出结果:

2012-3-1 a

2012-3-1 b

2012-3-2 a

2012-3-2 b

2012-3-3 b

2012-3-3 c

2012-3-4 d

2012-3-5 a

2012-3-6 b

2012-3-6 c

2012-3-7 c

2012-3-7 d

 

 

 

 

//===================================JAVA CODE=========================

package gq;

 

import java.io.IOException;

 

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

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;

/**

 * 数据去重复

 * @author tallqi

 *

 */

public class Dereplication {

 

public static class Map extends Mapper<Object, Text, Text, Text>{

private static Text line = new Text();

public void map(Object key,Text value,Context context) throws IOException, InterruptedException{

line =value;

System.out.println("Map:"+value);

context.write(line, new Text(""));

}

 

}

public static class Reduce extends Reducer<Text, Text, Text, Text>{

public void reduce(Text key,Iterable<Text> value,Context context) throws IOException, InterruptedException{

System.out.println("Reduce:"+key);

context.write(key, new Text(""));;

}

 

}

public static void main(String[] args) throws Exception{

 

      Configuration conf = new Configuration();

 

Job job = new Job(conf,"Dereplication");

job.setJarByClass(Dereplication.class);

 

//设置Map、Combine和Reduce处理类

job.setMapperClass(Map.class);

// job.setCombinerClass(Reduce.class);

job.setReducerClass(Reduce.class);

   

 

//输出Key,value的类型

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(Text.class);

 

//数据源地址,数据输出地址

FileInputFormat.addInputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/input"));

FileOutputFormat.setOutputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/output"));

System.exit(job.waitForCompletion(true)?0:1);

 

}

 

}

 

### 使用MapReduce处理招聘数据 在实际应用场景中,MapReduce被广泛用于处理大规模数据集。一个典型的例子是招聘数据的清洗和预处理[^1]。该过程不仅能够有效去除冗余信息,还能标准化不同来源的数据格式。 #### 招聘数据分析中的具体操作 对于招聘平台而言,每天都会产生大量的职位发布记录。这些原始数据通常包含重复条目以及不一致的信息字段。采用MapReduce框架可以帮助解决这些问题: - **Mapper阶段**:读取输入文件并将每一条招聘信息转换成键值对形式;其中key可能是公司名称或职位ID,value则包含了完整的岗位描述和其他属性。 - **Reducer阶段**:接收来自mapper的结果后执行聚合逻辑,比如删除相同job ID对应的多版本记录只保留最新的一版,并且还可以在此基础上进一步做分类汇总等工作。 ```python def mapper(key, value): # 解析每一行作为单独的工作列表项 job_id = extract_job_id(value) yield (job_id, value) def reducer(job_id, values): latest_version = find_latest(values) yield (job_id, latest_version) ``` 此方法显著提高了工作效率,在面对海量简历投递情况时尤为有用。 #### Web日志分析中的去重技术 另一个常见的场景是在Web服务器产生的大量访问日志里查找唯一IP地址的数量。这同样可以通过简单的MapReduce作业实现[^2]: - Mapper函数遍历所有日志行提取出客户端IP; - Reducer部分统计各个独立访客出现次数从而得出总数。 ```bash cat access.log | hadoop jar mapreduce.jar com.example.LogAnalyzer /input/path /output/path ``` 上述命令假设有一个名为`LogAnalyzer.java`编写好的Java应用程序专门用来解析Apache HTTPD格式的日志文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值