思路
去除重复用什么?
reduce阶段本身就会去除重复
package ip;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class IpMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
context.write(value,new IntWritable(1));
}
}
当不需要某个value时,可以使用NullWritable类型
map的输出value值其实是可以不要的
package ip;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class IpMapper extends Mapper<LongWritable, Text,Text, NullWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
context.write(value,NullWritable.get());
}
}
package ip;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class IpReducer extends Reducer<Text, IntWritable, Text,IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
//测试会不会提前归于一组
context.write(key,new IntWritable(1));
}
}
"reduce也不用输出值"
package ip;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class IpReducer extends Reducer<Text, NullWritable, Text,NullWritable> {
@Override
protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
context.write(key,NullWritable.get());
}
}
package ip;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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;
import java.io.IOException;
public class IpDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
//配置类,可以通过这个类提交作业参数
Configuration conf = new Configuration();
//封装作业
Job job = Job.getInstance(conf);
// 设置入口类
job.setJarByClass(IpDriver.class);
// 设置Mapper类
job.setMapperClass(IpMapper.class);
// 设置Reducer类
job.setReducerClass(IpReducer.class);
//设置mapper的输出类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NulllWritable.class);
//设置reducer的输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//文件输入路径
FileInputFormat.addInputPath(job,new Path("hdfs://hadoop01:9000/txt/ip.txt"));
// 设置输出路径
// 要求输出路径在HDFS上不存在
FileOutputFormat.setOutputPath(job, new Path("hdfs://hadoop01:9000/result/ipcount"));
// 提交任务
job.waitForCompletion(true);
}
}