在之前的Blog [http://flyfoxs.iteye.com/blog/2110463] 中讨论了, hadoop在文件切割时,可能会把一个行数据切割成无意义的2块. 如果不做特别处理,这会造成数据的失真及处理错误. 经人指点,发现这个BUG不存在.
Hadoop在分割文件后,后期读取中会通过一些规则来保证不会出现把一行数据分割成2行. 下面对这个后期处理机制(LineRecordReader)做一个分析:
1)数据分割是由JobClient完成,不是在hadoop集群完成.(并且这个是一个粗分,具体精确的还是依赖Mapper依赖如下规则)
2)数据的分割是由JobClient完成,但是Mapper在处理的时候,不是严格按照这个来处理,
除了第一个Split,其他的Split都是从第一个换行符开始读取
Split的结束是下一个Split的换行符,(太霸道了,除了最后一个,几乎每一都要跨越Split)
3)针对超长行,有一个理论上的Bug,就是如果有行超过了你限制的长度,那么这一行会有部分数据会被抛弃. 但是这个Bug是理论上的,因为默认值为 Integer.MAX_VALUE .
this.maxLineLength = job.getInt("mapred.linerecordreader.maxlength", Integer.MAX_VALUE);
下面的代码可以看出LineRecordReader读取最后一行的时候,并不是严格按照Split的结束而结束. 而是必须要读取到下一个Split的换行符.
代码比较复杂已经添加了注释,如果有不明白的欢迎提问.
public int readLine(Text str, int maxLineLength, int maxBytesToConsume) throws IOException { /* We're reading data from in, but the head of the stream may be * already buffered in buffer, so we have several cases: * 1. No newline characters are in the buffer, so we need to copy * everything and read another buffer from the stream. * 2. An unambiguously terminated line is in buffer, so we just * copy to str. * 3. Ambiguously terminated line is in buffer, i.e. buffer ends * in CR. In this case we copy everything up to CR to str, but * we also need to see what follows CR: if it's LF, then we * need consume LF as well, so next call to readLine will read * from after that. * We use a flag prevCharCR to signal if previous character was CR * and, if it happens to be at the end of the buffer, delay * consuming it until we have a chance to look at the char that * follows. */ str.clear(); int txtLength = 0; //tracks str.getLength(), as an optimization int newlineLength = 0; //length of terminating newline boolean prevCharCR = false; //true of prev char was CR long bytesConsumed = 0; do { //bufferPosn记录了当前Buffer读取到哪个位置,这样当下一次循环时 int startPosn = bufferPosn; //starting from where we left off the last time //如果Buffer里面的数据已经处理完毕,则对Buffer清空,重新再从IO流读取数据 if (bufferPosn >= bufferLength) { startPosn = bufferPosn = 0; if (prevCharCR) ++bytesConsumed; //account for CR from previous read //从IO中读取数据处理,只有处理完毕(bufferPosn >= bufferLength)才会再次读取 //bufferLength记录了从IO中读取了多少个字节的数据 bufferLength = in.read(buffer); if (bufferLength <= 0) break; // EOF } //在For循环总寻找断行符, 兼容MAC, Windows, Linux 多种平台的换行符 for (; bufferPosn < bufferLength; ++bufferPosn) { //search for newline //判断当前字符是否是'\n' if (buffer[bufferPosn] == LF) { //如果是'\r\n'来区分一行, newlineLength=2, 如果是'\n'则newlineLength=1 newlineLength = (prevCharCR) ? 2 : 1; ++bufferPosn; // at next invocation proceed from following byte break; } //如果是\r来区分一行,则newlineLength=1 if (prevCharCR) { //CR + notLF, we are at notLF newlineLength = 1; break; } //判断当前字符是否是'\r', 等待下一个循环来组合判断真正的换行符 prevCharCR = (buffer[bufferPosn] == CR); } int readLength = bufferPosn - startPosn; //Buffer最后一个字节就是'\r' if (prevCharCR && newlineLength == 0) --readLength; //CR at the end of the buffer bytesConsumed += readLength; //appendLength:在本轮循环中从Buffer中读取的负载长度,去除了换行符 int appendLength = readLength - newlineLength; //txtLength:记录了最终返回的str的长度 if (appendLength > maxLineLength - txtLength) { //如果添加后,字符串长度超过了一行长度的上限,那么超过的将不会被添加到str appendLength = maxLineLength - txtLength; } //将当前Buffer中,指定区间的字符添加到返回值(str) if (appendLength > 0) { str.append(buffer, startPosn, appendLength); txtLength += appendLength; } //如果在buffer里面没有读取到换行符,并且已经读取的字节数没有超过预定大小,则继续从IO流读取下一批数据 } while (newlineLength == 0 && bytesConsumed < maxBytesToConsume); if (bytesConsumed > (long)Integer.MAX_VALUE) throw new IOException("Too many bytes before newline: " + bytesConsumed); return (int)bytesConsumed; }
下面的代码可以看出LineRecordReader是如何来判读是否需要忽略第一行
public void initialize(InputSplit genericSplit, TaskAttemptContext context) throws IOException { FileSplit split = (FileSplit) genericSplit; Configuration job = context.getConfiguration(); this.maxLineLength = job.getInt("mapred.linerecordreader.maxlength", Integer.MAX_VALUE); start = split.getStart(); end = start + split.getLength(); final Path file = split.getPath(); compressionCodecs = new CompressionCodecFactory(job); final CompressionCodec codec = compressionCodecs.getCodec(file); // open the file and seek to the start of the split FileSystem fs = file.getFileSystem(job); FSDataInputStream fileIn = fs.open(split.getPath()); boolean skipFirstLine = false; 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; }
参考文献:
http://blog.csdn.net/bluishglc/article/details/9380087
http://blog.csdn.net/wanghai__/article/details/6583364
相关推荐
6.1 Hadoop概述 6.2 HDFS 6.2.1 HDFS文件系统的原型GFS 6.2.2 HDFS文件的基本结构 6.2.3 HDFS的存储过程 6.3 MapReduce编程框架 6.3.1 MapReduce的发展历史 ...6.5.7 在Hadoop系统上运行测试程序WordCount
7.1 概述 7.2 MapReduce体系结构 7.3 MapReduce工作流程 7.4 实例分析:WordCount 7.5 MapReduce的具体应用 7.6 MapReduce编程实践
主要介绍了大数据HelloWorld-Flink实现WordCount的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
storm-wordcount例子 storm-wordcount例子 storm-wordcount例子 storm-wordcount例子
1. HSDF获取文件:需要计算的源文本存在于HDFS系统上 2. Input(文件输入):HDFS中的文件都是以块(Block)为单位存储 3. Split
亲自测试的
【大数据入门笔记系列】第五节 SpringBoot集成hadoop开发环境(复杂版的WordCount)前言环境清单创建SpringBoot项目创建包创建yml添加集群主机名映射hadoop配置文件环境变量HADOOP_HOME编写代码添加hadoop依赖jar包...
大数据实验报告Hadoop编程实现wordcount单词统计程序附源码.doc
cs-1660-gcp-wordcount-hw
1.每个实验单元在 50 页的篇幅内完成一份报告 3.实验报告要求:书写工整规范,语言表达清楚,数据和程序真 4.参加实验的每位同学应独立完成实验报告的撰写,其
wc-mode:Emacs的Wordcount次要模式
云计算与大数据 MapReduce实验 Wordcount实验中所需数据包 WordCount.jar 不需要封装,centos7 linux hadoop实验上传所需
CKEditor-WordCount-插件 CKEditor v4(或更高版本)的WordCount插件可对单词/字符进行计数,并在编辑器的页脚中显示单词计数和/或字符计数。 演示版 免责声明:这是一个分叉的版本,如果有人知道原始作者,我将...
项目:maven-hadoop-java-wordcount-template 这是一个 Maven Hadoop Java 项目模板。 这个样板框架代码包含一个 Driver、一个 Mapper 和一个 Reducer,可以用您的代码修改(它们包含经典的 wordcount 示例)。 在您...
用java的MapReduce写了个demo,用于计算文档单词出现个数
wordcount-mapreduce Hadoop MapReduce WordCount 示例应用程序
实验2-在Hadoop平台上部署WordCount程序该项任务请同学作为作业自行完成,并提交实验报告。
005 - Spark框架 - 快速上手 - WordCount - 案例分析.avi 009 - Spark框架 - 快速上手 - WordCount - Spark的实现.avi 010 - Spark框架 - 快速上手 - WordCount - 日志和错误.avi 011 - Spark框架 - 运行环境 - 本地...
四川大学IT企业实训,拓思爱诺大数据第二次作业,MapReduce编程,包括Hadoop wordcount程序,及flowcount流量统计程序,包括重写排序及分区函数
3.2.2.在spark shell中编写WordCount程序 1.首先启动hdfs 2.向hdfs上传一个文件到hdfs://hdp-01:9000/wordcount/input/a.txt 3.在spark shell中用scala语言编写spark程序 scala> sc.textFile("hdfs://hdp-01:...