MR中简单实现自定义的输入输出格式
import java.io.DataOutput;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.CombineFileRecordReader;
import org.apache.hadoop.mapreduce.lib.input.CombineFileSplit;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.LineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileRecordReader;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.ReflectionUtils;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class TestCombine extends Configured implements Tool {
private static class ProvinceMapper extends
Mapper<Object, Text, Text, Text> {
@Override
protected void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
System.out.println("value : " + value + " Context " + context);
context.write(value, value);
}
} private static class ProvinceReducer extends
Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
for (Text va : values) {
System.out.println("reduce " + key);
context.write(key, key);
}
}
} // 输入格式
static class CombineSequenceFileInputFormat<K, V> extends CombineFileInputFormat<K, V> {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public RecordReader<K, V> createRecordReader(InputSplit split, TaskAttemptContext context) throws IOException {
return new CombineFileRecordReader((CombineFileSplit)split, context, CombineLineRecordReader.class);
}
} static class CombineLineRecordReader<K, V> extends RecordReader<K, V> {
private CombineFileSplit split;
private TaskAttemptContext context;
private int index;
private RecordReader<K, V> rr; @SuppressWarnings("unchecked")
public CombineLineRecordReader(CombineFileSplit split, TaskAttemptContext context, Integer index) throws IOException, InterruptedException {
this.index = index;
this.split = (CombineFileSplit) split;
this.context = context; this.rr = (RecordReader<K, V>) ReflectionUtils.newInstance(LineRecordReader.class, context.getConfiguration());
} @SuppressWarnings("unchecked")
@Override
public void initialize(InputSplit curSplit, TaskAttemptContext curContext) throws IOException, InterruptedException {
this.split = (CombineFileSplit) curSplit;
this.context = curContext; if (null == rr) {
rr = ReflectionUtils.newInstance(SequenceFileRecordReader.class, context.getConfiguration());
} FileSplit fileSplit = new FileSplit(this.split.getPath(index),
this.split.getOffset(index), this.split.getLength(index),
this.split.getLocations()); this.rr.initialize(fileSplit, this.context);
} @Override
public float getProgress() throws IOException, InterruptedException {
return rr.getProgress();
} @Override
public void close() throws IOException {
if (null != rr) {
rr.close();
rr = null;
}
} @Override
public K getCurrentKey()
throws IOException, InterruptedException {
return rr.getCurrentKey();
} @Override
public V getCurrentValue()
throws IOException, InterruptedException {
return rr.getCurrentValue();
} @Override
public boolean nextKeyValue() throws IOException, InterruptedException {
return rr.nextKeyValue();
}
} // 输出格式
static class MyOutputFormat extends FileOutputFormat<Text, Text>{
@Override
public RecordWriter<Text, Text> getRecordWriter(
TaskAttemptContext job) throws IOException, InterruptedException {
return new MyRecordWriter(job);
}
} public static class MyRecordWriter extends RecordWriter<Text, Text> {
private Map<String, FSDataOutputStream> outputMap = null;
private static final String LINESEPARATOR = "\n";
private FileSystem fs;
private JobContext job; public MyRecordWriter(JobContext job) throws IOException {
this.outputMap = new HashMap<String, FSDataOutputStream>();
this.job = job;
this.fs = FileSystem.get(job.getConfiguration());
} // 参考 MultipleOutputs
public void write(Text key, Text value) throws IOException {
String k = key.toString();
if(k.isEmpty())
return;
FSDataOutputStream out = outputMap.get(k);
if(out==null) {
if(k.isEmpty())
System.out.println(value.toString());
Path outputPath = new Path(FileOutputFormat.getOutputPath(job), k);
if(!fs.exists(outputPath))
out = fs.create(outputPath);
else
return;
outputMap.put(k, out);
}
out.write(value.getBytes());
out.write(LINESEPARATOR.getBytes());
} @Override
public void close(TaskAttemptContext context) throws IOException,
InterruptedException {
for(FSDataOutputStream out : outputMap.values()) {
out.close();
}
}
} public int run(String[] args) throws Exception {
Configuration conf = new Configuration(); Job job = new Job(conf);
job.setJobName("TestCombine");
job.setJarByClass(TestCombine.class); job.setMapperClass(ProvinceMapper.class);
job.setReducerClass(ProvinceReducer.class); //job.setInputFormatClass(CombineSequenceFileInputFormat.class);
job.setOutputFormatClass(MyOutputFormat.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); String inpath = "/home/hadoop/tmp/combine";
String outpath = "/home/hadoop/tmp/combineout";
Path p = new Path(outpath); FileSystem fs = FileSystem.get(conf);
if (fs.exists(p)){
fs.delete(p);
}
FileInputFormat.addInputPaths(job, inpath);
FileOutputFormat.setOutputPath(job, p); return job.waitForCompletion(true) ? 0 : 1;
} public static void main(String[] args) throws Exception {
int ret = ToolRunner.run(new TestCombine(), args);
System.exit(ret);
}
}
MR中简单实现自定义的输入输出格式的更多相关文章
- Hadoop(七):自定义输入输出格式
MR输入格式概述 数据输入格式 InputFormat. 用于描述MR作业的数据输入规范. 输入格式在MR框架中的作用: 文件进行分块(split),1个块就是1个Mapper任务. 从输入分块中将数 ...
- [ACM训练] ACM中巧用文件的输入输出来改写acm程序的输入输出 + ACM中八大输入输出格式
ACM中巧用文件的输入输出来改写acm程序的输入输出 经常有见大神们使用文件来代替ACM程序中的IO,尤其是当程序IO比较复杂时,可以使自己能够更专注于代码的测试,而不是怎样敲输入. C/C++代码中 ...
- 教你一招:在PowerPoint中自定义可输入文本的占位符
日常生活中,当我们设计多媒体课件时,默认的版式其实已经够用了.但是,很多时候,我们需要更加个性一点,所以,我们需要自定义很多东西.本文介绍在PowerPoint中自定义可输入文本的占位符. 一.占位符 ...
- 在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程)
在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程) 原文链接:http://www.360doc.com/content/14/1117/10/16948208_42571794 ...
- Settings > Editor > Live Templates 中自定义快速输入
Settings > Editor > Live Templates 中自定义快速输入
- Hadoop MapReduce编程 API入门系列之自定义多种输入格式数据类型和排序多种输出格式(十一)
推荐 MapReduce分析明星微博数据 http://git.oschina.net/ljc520313/codeexample/tree/master/bigdata/hadoop/mapredu ...
- MapReduce的输入输出格式
默认的mapper是IdentityMapper,默认的reducer是IdentityReducer,它们将输入的键和值原封不动地写到输出中. 默认的partitioner是HashPartitin ...
- C语言第一次作业——输入输出格式
题目1温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代码 #include& ...
- CString中Format函数与格式输入与输出
CString中Format函数与格式输入与输出 Format是一个非经常常使用.却又似乎非常烦的方法,下面是它的完整概貌.以供大家查询之用: 格式化字符串forma("%d" ...
随机推荐
- [转]ASP.NET Core Exception Filters and Resource Filters
本文转自:https://damienbod.com/2015/09/30/asp-net-5-exception-filters-and-resource-filters/ This article ...
- winfrom 树勾选
树勾选 /// <summary> /// 树勾选 /// </summary> /// <param name="sender"></p ...
- CssClass初步语法了解
首先 创建Css有三种方法 这里面就不一一介绍了,主要说第二种 创建第二种Css样式表 要在标签<title><title>标签下面写 如: <style type= ...
- 文档生成工具Sandcastle Help File Builder
Sandcastle Help File Builder http://shfb.codeplex.com/
- 在Android源码中如何吧so库打包编译进入apk, 集成第三方库(jar和so库)
集成第三方so和jar包 include $(CLEAR_VARS) #jar包编译 LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES :=securit ...
- .net比较两个时间的大小,不含日期,精确到分
解决方案: 1.将要比较的两个时间转成DateTime类型: DateTime date1 = DateTime.Parse("8:00"); DateTime date2 = D ...
- 继承Application管理生命周期
继承Application实现Android数据共享 http://www.jianshu.com/p/75a5c24174b2 jessyan提出一个思路,用Application + 接口来管理扩 ...
- C# 矩阵运算和一些基本的几何运算
以前工作中写的,这里备个份,有可能用到 基本的矩阵运算类,测试20阶以内应该没啥问题,超过20阶不好使... /// <summary> /// 矩阵 异常 512索引 1024无解 20 ...
- CSS深入理解学习笔记之border
1.border-width border-width为何不支持百分比:语义和使用场景决定的,现实中各种边框本身的概念就不存在百分比的使用方法. border-width支持关键字:thin.medi ...
- 隐藏 google 地图 Logo 隐藏 百度 地图 Logo
Google 地图 var isFirstLoad=true; //地图瓦片加载完成之后的回调 google.maps.event.addListener(map, 'tilesloaded', fu ...