DesDumplicate去重代码

来源:互联网 发布:贝叶斯算法 spark 编辑:程序博客网 时间:2024/06/04 19:30
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);
}
}





























原创粉丝点击