一个combineInputformat
mark
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
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.util.LineReader; public class MyInputFormat extends CombineFileInputFormat<InputSplitFile, Text> { @Override
public RecordReader<InputSplitFile, Text> createRecordReader(InputSplit split, TaskAttemptContext context)
throws IOException {
return new CombineFileRecordReader<InputSplitFile, Text>((CombineFileSplit)split, context, MyCombineFileRecordReader.class);
} } class MyCombineFileRecordReader extends RecordReader<InputSplitFile, Text> {
private static final Log LOG = LogFactory.getLog(MyCombineFileRecordReader.class); private CompressionCodecFactory compressionCodecs = null;
private long start;
private long pos;
private long end;
private Path path;
private LineReader in;
private int maxLineLength;
private InputSplitFile key = null;
private Text value = null; public MyCombineFileRecordReader(CombineFileSplit split, TaskAttemptContext context, Integer index) throws IOException{
Configuration job = context.getConfiguration();
this.maxLineLength = job.getInt("mapred.linerecordreader.maxlength", Integer.MAX_VALUE);
this.path = split.getPath(index);
this.start = split.getOffset(index);
this.end = start + split.getLength(index);
compressionCodecs = new CompressionCodecFactory(job);
final CompressionCodec codec = compressionCodecs.getCodec(this.path);
boolean skipFirstLine = false; FileSystem fs = path.getFileSystem(job);
FSDataInputStream fileIn = fs.open(split.getPath(index));
if (codec != null) {
in = new LineReader(codec.createInputStream(fileIn), job);
end = Long.MAX_VALUE;
} else {
if (start != 0) {
skipFirstLine = true;
--start;
fileIn.seek(start);
}
in = new LineReader(fileIn, job);
}
if (skipFirstLine) { // skip first line and re-establish "start".
start += in.readLine(new Text(), 0,
(int)Math.min((long)Integer.MAX_VALUE, end - start));
}
this.pos = start; } @Override
public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
// TODO Auto-generated method stub } @Override
public boolean nextKeyValue() throws IOException, InterruptedException {
if(key == null){
key = new InputSplitFile();
key.setFileName(path.getName());
}
key.setFileName(path.getName());
if(value == null){
value = new Text();
}
int newSize = 0;
while(pos < end){ newSize = in.readLine(value, maxLineLength, Math.max((int)Math.min(Integer.MAX_VALUE, end-pos), maxLineLength)); if(newSize == 0){
break;
} pos += newSize;
if(newSize < maxLineLength){
break;
}
// line too long. try again
LOG.info("Skipped line of size " + newSize + " at pos " +
(pos - newSize));
}
if (newSize == 0) {
key = null;
value = null;
return false;
} else {
return true;
} } @Override
public InputSplitFile getCurrentKey() throws IOException, InterruptedException { return key;
} @Override
public Text getCurrentValue() throws IOException, InterruptedException { return value;
} @Override
public float getProgress() throws IOException, InterruptedException {
if(start == end){
return 0.0f;
}else {
return Math.min(1.0f, (pos - start)/(float)(end-start));
}
} @Override
public void close() throws IOException {
if(in != null){
in.close();
} } } class InputSplitFile implements WritableComparable<InputSplitFile> {
private long offset;
private String fileName; public long getOffset(){
return offset;
} public void setOffset(long offset){
this.offset = offset;
} public String getFileName(){
return fileName;
} public void setFileName(String fileName){
this.fileName = fileName;
} public void readFields(DataInput in) throws IOException {
this.offset = in.readLong();
this.fileName = Text.readString(in);
} public void write(DataOutput out) throws IOException{
out.writeLong(offset);
Text.writeString(out, fileName);
} public int compareTo(InputSplitFile o){
InputSplitFile that = (InputSplitFile) o; int f = this.fileName.compareTo(that.fileName);
if(f == 0){
return (int)Math.signum((double)(this.offset - that.offset));
}
return f;
} public boolean equals(InputSplitFile obj){
if(obj instanceof InputSplitFile){
return this.compareTo(obj) == 0;
}
return false;
} public int hashCode(){
assert false : "hashCode not designed";
return 42;//an arbitrary constant
} }
一个combineInputformat的更多相关文章
- 为什么很多人坚信“富贵险中求”?
之家哥 2017-11-15 09:12:31 微信QQ微博 下载APP 摘要 网贷之家小编根据舆情频道的相关数据,精心整理的关于<为什么很多人坚信"富贵险中求"?>的 ...
- python基础全部知识点整理,超级全(20万字+)
目录 Python编程语言简介 https://www.cnblogs.com/hany-postq473111315/p/12256134.html Python环境搭建及中文编码 https:// ...
- Tomcat一个BUG造成CLOSE_WAIT
之前应该提过,我们线上架构整体重新架设了,应用层面使用的是Spring Boot,前段日子因为一些第三方的原因,略有些匆忙的提前开始线上的内测了.然后运维发现了个问题,服务器的HTTPS端口有大量的C ...
- 如何一步一步用DDD设计一个电商网站(九)—— 小心陷入值对象持久化的坑
阅读目录 前言 场景1的思考 场景2的思考 避坑方式 实践 结语 一.前言 在上一篇中(如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成),有一行注释的代码: public interfa ...
- 如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成
阅读目录 前言 建模 实现 结语 一.前言 前面几篇已经实现了一个基本的购买+售价计算的过程,这次再让售价丰满一些,增加一个会员价的概念.会员价在现在的主流电商中,是一个不大常见的模式,其带来的问题是 ...
- SQLSERVER将一个文件组的数据移动到另一个文件组
SQLSERVER将一个文件组的数据移动到另一个文件组 有经验的大侠可以直接忽视这篇文章~ 这个问题有经验的人都知道怎麽做,因为我们公司的数据量不大没有这个需求,也不知道怎麽做实验 今天求助了QQ群里 ...
- 构建一个基本的前端自动化开发环境 —— 基于 Gulp 的前端集成解决方案(四)
通过前面几节的准备工作,对于 npm / node / gulp 应该已经有了基本的认识,本节主要介绍如何构建一个基本的前端自动化开发环境. 下面将逐步构建一个可以自动编译 sass 文件.压缩 ja ...
- 【造轮子】打造一个简单的万能Excel读写工具
大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...
- 如何一步一步用DDD设计一个电商网站(十)—— 一个完整的购物车
阅读目录 前言 回顾 梳理 实现 结语 一.前言 之前的文章中已经涉及到了购买商品加入购物车,购物车内购物项的金额计算等功能.本篇准备把剩下的购物车的基本概念一次处理完. 二.回顾 在动手之前我对之 ...
随机推荐
- JavaScript高级程序设计:第十二章
DOM1级主要定义的是HTML和XML文档的底层结构.DOM2和DOM3级则在这个结构的基础上引入了更多的交互能力,也支持了更高级的XML特性.为此DOM2和DOM3级分为许多模块,这些模块如下: D ...
- Ansible11:变量详解【转】
一.在Inventory中定义变量 详见<Ansible2:主机清单> 二.在Playbook中定义变量 1.通过vars关键字定义: vars: http_port: 80 server ...
- .net简单的静态页生成
1.得到实体对象model,读取模板 string htmlMaster = File.ReadAllText(HttpContext.Current.Server.MapPath("/ma ...
- 暴力+树状数组维护 Codeforces Round #378 (Div. 2) C
题目大意:给你一个长度为n的数组a,然后数值大的可以合并数值小的,且合并了以后该数组的长度-1.给你一个长度为k目标数组b,问,是否可以从a数组变到b数组,是就yes并且输出步骤.否就输出no 思路: ...
- Django CRM学员系统项目
项目需求: 1.分讲师\学员\课程顾问角色,2.学员可以属于多个班级,学员成绩按课程分别统计3.每个班级至少包含一个或多个讲师4.一个学员要有状态转化的过程 ,比如未报名前,报名后,毕业老学员5.客户 ...
- win8.1下安装ubuntu 14.0 4LTS
1.前奏 电脑上已经安装了win8.1系统 2.准备工作 关闭win8.1的快速启动 步骤: 控制面板->电源选项->选择电源按钮的功能->更改不可用的设置,然后把"启用快 ...
- Too Much Money
Too Much Money time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Goods transportation
Goods transportation time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Struts2 请求数据的自动封装 及 自定义转换器类
请求数据自动封装: 实现原理:使用了参数拦截器.struts-default.xml中 <interceptor name="params" class="com. ...
- Objective-C相关Category的收集
Objective-C相关Category的收集 Categories是给你得不到源码的classes增加功能的一种方法.这个页面收集一些相关的Category,并且持续更新,你可以订阅关注.作者是F ...