微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MapReduce的数据去重处理

6、让类【DeduplicationMapper】继承类Mapper同时指定需要的参数类型,根据业务逻辑修改map类的内容如下。

package com.simple.duduplication;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class DeduplicationMapper extends Mapper<LongWritable, Text, Text, Text> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
            throws IOException, InterruptedException {
        //按行读取信息并作为mapper的输出键,mapper的输出值置为空文本即可
        Text line = value;
        context.write(line, new Text(''));
    }
}

7、在项目src目录下指定的包名【com.simple.deduplication】下右键点击,新建一个类名为【DeduplicationReducer】并继承Reducer类,然后添加该类中的代码内容如下所示。

package com.simple.duduplication;
import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class DeduplicationReducer extends Reducer<Text, Text, Text, Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> value, Reducer<Text, Text, Text, Text>.Context context)
            throws IOException, InterruptedException {
        //Reducer阶段直接按键输出即可,键直接可以实现去重
        context.write(key, new Text(''));
    }
}

8、在项目src目录下指定的包名【com.simple.deduplication】下右键点击,新建一个测试主类名为【TestDeduplication】并指定main主方法,测试代码如下所示:

package com.simple.duduplication;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class TestDeduplication {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.set('fs.defaultFS', 'hdfs://localhost:9000');
        //获取作业对象
        Job job = Job.getInstance(conf);
        //设置主类
        job.setJarByClass(TestDeduplication.class);
        //设置job参数
        job.setMapperClass(DeduplicationMapper.class);
        job.setReducerClass(DeduplicationReducer.class);
        job.setoutputKeyClass(Text.class);
        job.setoutputValueClass(Text.class);
        //设置job输入输出
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setoutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐