package com.ccse.hadoop.outputformat;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
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.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.RecordWriter;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.mapred.lib.MultipleOutputFormat;
import org.apache.hadoop.util.Progressable;
import com.ccse.hadoop.old.WordCountApp;
/**
* 自定义输出多文件
* @author woshiccna
*
*/
public class MyMultipleOutputFormatApp {
public final static String INPUT_PATH = "hdfs://chaoren1:9000/mapinput";
public final static String OUTPUT_PATH = "hdfs://chaoren1:9000/mapoutput";
public static void main(String[] args) throws IOException, URISyntaxException {
JobConf conf = new JobConf(WordCountApp.class);
conf.setJobName("wordcount");
Configuration config = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI(OUTPUT_PATH), config);
fileSystem.delete(new Path(OUTPUT_PATH), true);
conf.setMapperClass(MyMapper.class);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(LongWritable.class);
conf.setInputFormat(TextInputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(INPUT_PATH));
conf.setReducerClass(MyReducer.class);
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(LongWritable.class);
conf.setOutputFormat(MySelfMultipleOutputFormat.class);
FileOutputFormat.setOutputPath(conf, new Path(OUTPUT_PATH));
JobClient.runJob(conf);
}
public static class MyMapper extends MapReduceBase
implements Mapper<LongWritable, Text, Text, LongWritable> {
private Text word = new Text();
private LongWritable writable = new LongWritable(1);
@Override
public void map(LongWritable key, Text value,
OutputCollector<Text, LongWritable> output, Reporter reporter)
throws IOException {
if (value != null) {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreElements()) {
word = new Text(tokenizer.nextToken());
output.collect(word, writable);
}
}
}
}
public static class MyReducer extends MapReduceBase implements
Reducer<Text, LongWritable, Text, LongWritable> {
@Override
public void reduce(Text key, Iterator<LongWritable> values,
OutputCollector<Text, LongWritable> output, Reporter reporter)
throws IOException {
long sum = 0;
while (values.hasNext()) {
LongWritable value = values.next();
sum += value.get();
}
output.collect(key, new LongWritable(sum));
}
}
public static class MySelfMultipleOutputFormat
extends MultipleOutputFormat<Text, LongWritable> {
@Override
protected RecordWriter<Text, LongWritable> getBaseRecordWriter(
FileSystem fs, JobConf job, String name, Progressable progress)
throws IOException {
final TextOutputFormat<Text, LongWritable> format
= new TextOutputFormat<Text, LongWritable>();
return format.getRecordWriter(fs, job, name, progress);
}
@Override
protected String generateFileNameForKeyValue(Text key,
LongWritable value, String name) {
//根据key和value的值来生成文件的名字
/*if (key != null) {
return key.toString();
} else {
return "hello";
}*/
if (key.toString().startsWith("hello")) {
return "hello";
} else {
return key.toString();
}
}
}
}
自定义输出多个文件
原创
©著作权归作者所有:来自51CTO博客作者ccna_zhang的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
SpringBoot自定义starter
SpringBoot自定义starter
spring maven ci -
log4j自定义输出多个文件
log4j的强大功能无可置疑,但实际应用中免不了遇到某个功能需要输出独立的日志文
log4j Apache Web C C++ -
实现mapreduce多文件自定义输出
普通maprduce中通常是有map和reduce两个阶段,在不做设置的情况下,计算结果会以part-000*输出成多个文件,并且输出的文
大数据 java hadoop apache mapreduce -
logback自定义输出至kafka logback自定义字段
logback access event 自定义字段一、logback介绍二、logback-access介绍 2.1 logback-access.xml 配置 三、使用问题 &
logback自定义输出至kafka logback-access java springBoot 字段