DesDumplicate去重代码

package com.zhiyou.bd17.mr;


import java.io.IOException;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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;


//计算出访问过该系统的每一个用户的用户名
//去重
public class DesDumplicate {

//从文件中抽取用户名,并将其作为key发送到reducer节点,value 与计算无关
// 设置成NullWritable类型
public static class DesDumplicateMap extends Mapper<LongWritable, Text, Text, NullWritable>{


private String[] infos;
private NullWritable oValue = NullWritable.get();
private Text oKey = new Text();
@Override
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context)
throws IOException, InterruptedException {

infos = value.toString().split("\\s");
oKey.set(infos[0]);
context.write(oKey, oValue);
}
}

//把每个key作为一条记录输出出去,每个key 都不同,结果就是排过重了
public static class DesDumplicateReduce extends Reducer<Text, NullWritable, Text, NullWritable>{
private final NullWritable oValue = NullWritable.get();


@Override
protected void reduce(Text key, Iterable<NullWritable> values,
Reducer<Text, NullWritable, Text, NullWritable>.Context context) throws IOException, InterruptedException {
context.write(key,oValue);
}

}

//构建和启动job
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{
Configuration configuration = new Configuration();

Job job = Job.getInstance(configuration);
job.setJarByClass(DesDumplicate.class);
job.setJobName("计算访问过系统的用户名");

job.setMapperClass(DesDumplicateMap.class);
job.setReducerClass(DesDumplicateReduce.class);

//设置map的输出kv类型和整个mrjob的map输出kv类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);

//设置输入数据
Path inputPath = new Path("/bd17/user-logs-large.txt");
FileInputFormat.addInputPath(job, inputPath);

//设置输出数据
Path outputPath = new Path("/bd17/output/desdump");
outputPath.getFileSystem(configuration).delete(outputPath, true);
FileOutputFormat.setOutputPath(job, outputPath);

//启动job
System.exit(job.waitForCompletion(true)?0:1);
}
}





























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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值