自定义输出多个文件

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();
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波哥的技术积累

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值