一个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设计一个电商网站(十)—— 一个完整的购物车
阅读目录 前言 回顾 梳理 实现 结语 一.前言 之前的文章中已经涉及到了购买商品加入购物车,购物车内购物项的金额计算等功能.本篇准备把剩下的购物车的基本概念一次处理完. 二.回顾 在动手之前我对之 ...
随机推荐
- java fork-join框架应用和分析
http://shmilyaw-hotmail-com.iteye.com/blog/1897636 java fork-join框架应用和分析 博客分类: concurrency multithre ...
- lldpcli 常用命令
1.查看周围的邻居列表 root@hbg:/# lldpcli show neighbors------------------------------------------------------ ...
- DB层级
最上层: 业务层 负载均衡: LVS 代理层: DB-PROXY DB层: DB主库 DB从库 随着DB出 ...
- TOMCAT 优化设置
增加JVM堆内存大小修复JRE内存泄漏线程池设置压缩数据库性能调优Tomcat本地库 第1步 – 提高JVM栈内存Increase JVM heap memory 你使用过tomcat的话,简单的说就 ...
- size_t, ptrdiff_t, size_type, difference_type
size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以负数,std:: ...
- zend笔记
ZEND_STRL(str) 等价于 (str), (sizeof(str)-1) ZEND_STRS(str)等价于 (str), (sizeof(str))
- namenode ha
http://blog.csdn.net/totxian/article/details/45248399 http://www.aboutyun.com/thread-13679-1-1.html ...
- Java学习笔记之接口和抽象类
接口(interface)1.interface创建一个接口,implements实现接口 interface jiekou{} class lie implements jiekou{}2.接口可以 ...
- 【转】How to build and install PHP 5.6.9 from source on Ubuntu 14.04 VPS
原文 https://vpsineu.com/blog/how-to-build-and-install-php-5-6-9-from-source-on-ubuntu-14-04-vps/ In t ...
- idea控制台输出乱码
找到安装目录bin下面的idea64.exe.vmoptions,打开后在最后一行增加 -Xms128m -Xmx750m -XX:MaxPermSize=350m -XX:ReservedCodeC ...