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);
}
}
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 0
- DesDumplicate去重代码
- cell去重 覆盖代码
- List一段代码去重
- js实现去重代码
- js 数组 去重 代码
- 一行代码实现java list去重
- 一行代码实现java list去重
- 一行代码实现java list去重
- 一行代码实现java list去重
- 去重
- 去重
- 去重
- 去重
- 去重
- 【规范代码】关于vector的去重及排序
- 常用代码备份--vector中字符串去重
- 基于Redis的Bloomfilter去重(附Python代码)
- 用最少的代码做到数组去重、排序
- 一道Integer面试题引发的对Integer的探究
- Unity 与IOS基本交互
- 浅谈nginx内存池(四)
- OSPF笔记-6
- 《python核心编程》学习笔记(二):re
- DesDumplicate去重代码
- NS3生成随机数
- 编写一个程序,它从标准输入读取C源代码,并验证所有的花括号都正确的成对出现。
- 浮点数的运算
- SSH 常用操作
- eclipse中Java及html字体颜色的修改
- Jenkins入门系列之—Jenkins安装与配置
- yuv420数据快速裁剪
- OSPF笔记-7