1、有words.txt文件内容如下,其中以制表符分割,需求:利用mapreduce按照性别分区
Smith male
Alice female
Tony male
Doris female
2、分析
map阶段:
(1)、mapreduce逐行读取文件,得到每行的值
(2)、以制表符分割后,姓名为key,性别为value输出
reduce阶段:
(1)、直接输出即可
自定义分区类:
package com.qujiuge.partition;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
/**
* 这里的两个Text对应的是map输出的类型
*/
public class Partition extends Partitioner<Text, Text> {
@Override
public int getPartition(Text key, Text value, int i) {
if ("female".equals(value.toString())) return 0;
return 1;
}
}
3、创建maven工程后,添加如下依赖
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
4、编写mapreduce程序
package com.qujiuge.partition;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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;
import java.io.IOException;
public class Driver {
static class PartitionMapper extends Mapper<LongWritable, Text, Text, Text> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] fields = line.split("\t");
if (fields.length == 2) {
context.write(new Text(fields[0]), new Text(fields[1]));
}
}
}
static class PartitionReducer extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
super.reduce(key, values, context);
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration config = new Configuration();
Job job = Job.getInstance(config);
job.setJarByClass(com.qujiuge.sort_.Driver.class);
job.setMapperClass(PartitionMapper.class);
job.setReducerClass(PartitionReducer.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setPartitionerClass(Partition.class); // 指定自定义分区类
job.setNumReduceTasks(2); // 同时指定相应数量的reduce
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileSystem fs = FileSystem.get(config);
Path outPath = new Path(args[1]);
if (fs.exists(outPath)) {
fs.delete(outPath, true);
}
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, outPath);
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
项目结构如下:
5、将words.txt文件上传到hdfs中
6、将项目打包成jar文件后,用hadoop jar命令执行
运行结果: