版权声明:个人原创,转载请标注! https://blog.csdn.net/Z_Date/article/details/83861759
将MR处理后的结果数据输出到多个文件中
import java.io.IOException;
import java.util.StringTokenizer;
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.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
/**
*
* @author lyd
*
*输入数据
*hello world
*hi qianfeng
*Hi qianfeng
*Hello Qianfeng
*QQ
*163.com
*1603
*@qq.com
**123
**123
*(123)
*
*单词首字母为a-z的单词放到一个输出文件,并统计
*单词首字母为A-Z的单词放到一个输出文件,并统计
*单词首字母为0-9的单词放到一个输出文件,并统计
*单词首字母为其它的单词放到一个输出文件,并统计
*
关键代码:
MyReducer类中:
mos = new MultipleOutputs<Text, Text>(context);
mos.write("az", key, new Text(counter+""));
main函数中:
MultipleOutputs.addNamedOutput(job, "az", TextOutputFormat.class, Text.class, Text.class);
*/
public class MultipleDemo {
//自定义myMapper
public static class MyMapper extends Mapper<LongWritable, Text, Text, Text>{
//只在map方法运行之前执行一次。(仅执行一次)
@Override
protected void setup(Context context)
throws IOException, InterruptedException {
}
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
context.write(new Text(st.nextToken()), new Text("1"));
}
}
//map方法运行完后执行一次(仅执行一次)
@Override
protected void cleanup(Context context)
throws IOException, InterruptedException {
}
}
//自定义myReducer
public static class MyReducer extends Reducer<Text, Text, Text, Text>{
//在reduce方法执行之前执行一次。(仅一次)
MultipleOutputs<Text, Text > mos = null;
@Override
protected void setup(Context context)
throws IOException, InterruptedException {
//获取mos多文件输出对象
mos = new MultipleOutputs<Text, Text>(context);
}
@Override
protected void reduce(Text key, Iterable<Text> value,Context context)
throws IOException, InterruptedException {
/*
* qianfeng list(1,1)
*/
int counter = 0;
for (Text t : value) {
counter += Integer.parseInt(t.toString());
}
String word = key.toString();
//判断单词首字母
String firstChar = word.substring(0,1);
if(firstChar.matches("[a-z]")){
mos.write("az", key, new Text(counter+""));
} else if(firstChar.matches("[A-Z]")){
mos.write("AZ", key, new Text(counter+""));
} else if(firstChar.matches("[0-9]")){
mos.write("09", key, new Text(counter+""));
} else {
mos.write("others", key, new Text(counter+""));
}
}
//在reduce方法执行之后执行一次。(仅一次)
@Override
protected void cleanup(Context context)
throws IOException, InterruptedException {
mos.close();
}
}
/**
* job的驱动方法
* @param args
*/
public static void main(String[] args) {
try {
//1、获取Conf
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://hadoop01:9000");
//2、创建job
Job job = Job.getInstance(conf, "model01");
//3、设置运行job的class
job.setJarByClass(MultipleDemo.class);
//4、设置map相关属性
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
//设置多文件输出
MultipleOutputs.addNamedOutput(job, "az", TextOutputFormat.class, Text.class, Text.class);
MultipleOutputs.addNamedOutput(job, "AZ", TextOutputFormat.class, Text.class, Text.class);
MultipleOutputs.addNamedOutput(job, "09", TextOutputFormat.class, Text.class, Text.class);
MultipleOutputs.addNamedOutput(job, "others", TextOutputFormat.class, Text.class, Text.class);
//5、设置reduce相关属性
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
//判断输出目录是否存在,若存在则删除
FileSystem fs = FileSystem.get(conf);
if(fs.exists(new Path(args[1]))){
fs.delete(new Path(args[1]), true);
}
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//6、提交运行job
int isok = job.waitForCompletion(true) ? 0 : 1;
//退出
System.exit(isok);
} catch (IOException | ClassNotFoundException | InterruptedException e) {
e.printStackTrace();
}
}
}