大数据---16.MapReduce的数据去重复操作实例

MapReduce的数据去重复操作实例

1.原始数据:phone .txt

134 1341307 广东 惠州 移动 516000 0752 441300
134 1341308 广东 惠州 移动 516000 0752 441300
134 1341309 广东 惠州 移动 516000 0752 441300
134 1341310 广东 惠州 移动 516000 0752 441300
134 1341311 广东 惠州 移动 516000 0752 441300
134 1341312 广东 惠州 移动 516000 0752 441300
134 1341313 广东 惠州 移动 516000 0752 441300
需求:
求取出以上数据手机号前三位以及对应的省;市;运营商

例子:134 广东 惠州 移动

2.具体代码:

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
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.File;
import java.io.IOException;

public class DistinctDemo {
//map端
public static class MapTask extends Mapper<LongWritable, Text,Text,Text>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] splits = value.toString().split(“\t”);
//1341312 广东 惠州 移动 516000 0752 441300 ===>134 广东 惠州 移动
if (splits.length >= 7) {
String phone = splits[0].substring(0, 3);
String province = splits[1];
String city = splits[2];
String operator = splits[3];
//写出去
context.write(new Text(phone + “\t” + province + “\t” + city), new Text(operator));
}
}
}

//reduce端
public static class ReduceTask extends Reducer<Text,Text,Text,Text>{
    @Override            //134  广东  惠州        (移动,移动,移动,移动。。。。。。。)
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        for (Text value : values) {
            context.write(key,new Text(value));
            break;
        }
    }
}


//main
public static void main(String[] args) throws Exception {
    //我们需要一盒hadoop的对象去提交这俩个内部类  Job    本地运行
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf);

    //提交那俩个内部类
    job.setMapperClass(DistinctDemo.MapTask.class);
    job.setReducerClass(DistinctDemo.ReduceTask.class);
    job.setJarByClass(DistinctDemo.class);

    //设置四个输出参数的类型
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(Text.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);

    //如果输出文件  存在 就删除
    String output="E:\\BigData\\output\\distinct";
    File file = new File(output);
    if(file.exists()){
        FileUtils.deleteDirectory(file);
    }

    //设置输入  输出路径
    FileInputFormat.addInputPath(job,new Path("E:\\BigData\\input\\Phone.txt"));
    FileOutputFormat.setOutputPath(job,new Path(output));

    //温馨提示
    boolean b = job.waitForCompletion(true);
    System.out.println(b?"数据成功!!!":"数据,出BUG了,赶快去调一下!!!");
}

}

3.运行结果

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值