hadoop中的输入拆分和块

whlutmcx  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(467)

我有一个100 mb的文件大小,并说默认块大小是64 mb。如果我没有设置输入分割大小,默认分割大小将是块大小。现在分割大小也是64MB。
当我将这个100MB的文件加载到hdfs中时,这个100MB的文件将被分成两个块。i、 e、64 mb和36 mb。例如,下面是一首100 mb大小的诗歌歌词。如果我将这些数据加载到hdfs中,比如说从第1行到第16行的一半,正好是64mb作为一个分割/块(直到“它产生了”),而从第16行的剩余一半(孩子们欢笑和玩耍)到文件的结尾,正好是第二个块(36mb)。将有两个Map绘制工作。
我的问题是,第一个绘图员将如何考虑第16条线(即区块1的第16条线),因为区块只有一半的线,或者第二个绘图员将如何考虑区块2的第一条线,因为它也有一半的线。

Mary had a little lamb
Little lamb, little lamb
Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
Mary went, Mary went
Everywhere that Mary went
The lamb was sure to go

He followed her to school one day
School one day, school one day
He followed her to school one day
Which was against the rule
It made the children laugh and play
Laugh and play, laugh and play
It made the children laugh and play
To see a lamb at school

And so the teacher turned him out
Turned him out, turned him out
And so the teacher turned him out
But still he lingered near
And waited patiently
Patiently, patiently
And wai-aited patiently
Til Mary did appear

或者在拆分64MB时,hadoop会考虑整行16而不是拆分单行吗?

t3irkdon

t3irkdon1#

在hadoop中,数据是基于输入拆分大小和块大小读取的。
文件根据大小分为多个文件片段。每个输入分割都使用与输入中的偏移量相对应的start参数进行初始化。
初始化linerecordreader时,它会尝试示例化一个linereader,该linereader开始读取行。
如果定义了compressioncodec,它将处理边界。因此,如果inputsplit的开头不是0,则回溯1个字符,然后跳过第一行(遇到\n或\r\n)。回溯可确保不跳过有效行。
代码如下:

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;

由于分割是在客户机中计算的,Map器不需要按顺序运行,每个Map器都已经知道是否需要丢弃第一行。
所以在你的情况下,第一块b1,将读取的数据从偏移量0到“它使孩子们欢笑和玩耍”行
块b2将读取从“在学校看羔羊”行到最后一行偏移量的数据。
您可以参考以下内容:
https://hadoopd.wordpress.com/2015/03/10/hdfs-file-block-and-input-split/
hadoop进程记录是如何跨越块边界分割的?

4smxwvx5

4smxwvx52#

第一个Map器将读取整个第16行(它将继续读取,直到找到和结束行字符)。
如果您还记得的话,为了应用mapreduce,您的输入必须组织在键值对中。对于textinputformat,这恰好是hadoop中的默认值,它们是:(offset\ from\ file\ beging,line\ of\ text)。正在根据“\n”字符将文本拆分为这些键值对。因此,如果一行文本超出了输入拆分的大小,Map程序将继续读取,直到找到“\n”。

相关问题