How does Hadoop split files? - hadoop

I would like to know how the files are split in Hadoop. I mean, i know they are split by some size (e.g. 64MB), but does the break occur, at the end of a line or at some character etc?
Also how does the name node keep track of the sequence in which the files are split, like how to assemble them in which order after they are collected from the data nodes.

LineRecordReader reads every line and sends key/value pairs to mapper instance.
If EOL appears before defined block size(in this case 64MB), reader continues to next line.
Now, If reader reaches block size and not EOL, then it continues to read until EOL and set as a block.
Now, next block starts from where reader stopped(i.e., after EOL).
Reference

Related

Very long lines of input in Ruby

I have a Ruby script that performs some substitutions on the output of mysqldump.
The input can have very long lines (hundreds of MB), because a single line can represent a multi-row INSERT statement for all the data in a table. The mysqldump utility can be coerced to produce one INSERT statement per row, but I don't have control of every client.
My script naively expects IO#each_line to control memory usage:
$stdin.each_line do |line|
next if options[:entity_excludes].any? { |entity| line =~ /^(DROP TABLE IF EXISTS|INSERT INTO) `custom_#{entity}(s|_meta)`/ }
line.gsub!(/^CREATE TABLE `/, "CREATE TABLE IF NOT EXISTS `")
line.gsub!('{{__OPF_SITEURL__}}', siteurl) if siteurl
$stdout.write(line)
end
I've already seen input with maximum line length over 400MB, and this translates directly into process resident memory.
Are there libraries for Ruby that allow text transforms on an input stream using buffers instead of relying on line-delimited input?
This was marked as a duplicate of a simpler question. But there's quite a bit more to this. You need to keep track of multiple buffers and test for application of transforms even when they apply across a buffer boundary. It's easy to get wrong, which is why I'm hoping a library already exists.

why is the length of block after the block

I'm extracting data from a binary file and see that the length of the binary data block comes after the block itself (the character chunks within the block have length first then 00 and then the information)
what is the purpose of the the block? is it for error checking?
Couple of examples:
The length of block was unknown when write operation began. Consider audio stream from microphone which we want to write as single block. It is not feasible to buffer it in RAM because it may be huge. That's why after we received EOF, we append effective size of block to the file. (Alternative way would be to reserve couple of bytes for length field in the beginning of block and then, after EOF, to write length there. But this requires more IO.)
Database WALs (write-ahead logs) may use such scheme. Consider that user starts transaction and makes lots of changes. Every change is appended as single record (block) to WAL. If user decides to rollback transaction, it is easy now to go backwards and then to chop off all records which were added as part of transaction user wants to rollback.
It is common for binary files to carry two blocks of metainformation: one block in the beginning (e.g. creation date, hostname) and another one in the end (e.g. statistics and checksum). When application opens existing binary file, it first wants to load these two blocks to make decisions about memory allocation and the like. This is much easier to load last block if its length is stored in the very end of file rather then scanning file from the beginning.

Reading of the broken line by Record Reader

I went through the cloudera blog and I got an article(Link below).Refer to the third point.
http://blog.cloudera.com/blog/2011/01/lessons-learned-from-clouderas-hadoop-developer-training-course/
As per my understanding, if there are 2 input splits, then the broken line will be read by the record reader of the first input split.
If I am getting it correct, can you tell me how it does that i.e how the record reader of the first split reads the broken line past the input split ?
As per my understanding, if there are 2 input splits, then the broken line will be read by the record reader of the first input split.
Yes, this is correct.
can you tell me how it does that i.e how the record reader of the first split reads the broken line past the input split
An InputSplit doesn't contain the raw data, but rather the information needed to extract the data. A FileInputSplit (which is what you're referring to) contains a path to the file as well as the byte offsets to read in the file. It is then up to the RecordReader to go out and read that data. This means that it can read past the end byte offset defined by the split.

Is it possible to know the serial number of the block of input data on which map function is currently working?

I am a novice in Hadoop and here I have the following questions:
(1) As I can understand, the original input file is split into several blocks and distributed over the network. Does a map function always execute on a block in its entirety? Could there be more than one map functions executing on data in a single block?
(2) Is there any way that it can be learned, from within the map function, which section of the original input text the mapper is currently working on? I would like to get something like a serial number, for instance, for each block starting from the first block of the input text.
(3) Is it possible to make the splits of the input text in such a way that each block has a predefined word count? If possible then how?
Any help would be appreciated.
As I can understand, the original input file is split into several blocks and distributed over the network. Does a map function always execute on a block in its entirety? Could there be more than one map functions executing on data in a single block?
No. A block(split to be precise) gets processed by only one mapper.
Is there any way that it can be learned, from within the map function, which section of the original input text the mapper is currently working on? I would like to get something like a serial number, for instance, for each block starting from the first block of the input text.
You can get some valuable info, like the file containing split's data, the position of the first byte in the file to process. etc, with the help of FileSplit class. You might find it helpful.
Is it possible to make the splits of the input text in such a way that each block has a predefined word count? If possible then how?
You can do that by extending FileInputFormat class. To begin with you could do this :
In your getSplits() method maintain a counter. Now, as you read the file line by line keep on tokenizing them. Collect each token and increase the counter by 1. Once the counter reaches the desired value, emit the data read upto this point as one split. Reset the counter and start with the second split.
HTH
If you define a small max split size you can actually have multiple mappers processing a single HDFS block (say 32mb max split for a 128 MB block size - you'll get 4 mappers working on the same HDFS block). With the standard input formats, you'll typically never see two or more mappers processing the same part of the block (the same records).
MapContext.getInputSplit() can usually be cast to a FileSplit and then you have the Path, offset and length of the file being / block being processed).
If your input files are true text flies, then you can use the method suggested by Tariq, but note this is highly inefficient for larger data sources as the Job Client has to process each input file to discover the split locations (so you end up reading each file twice). If you really only want each mapper to process a set number of words, you could run a job to re-format the text files into sequence files (or another format), and write the records down to disk with a fixed number of words per file (using Multiple outputs to get a file per number of words, but this again is inefficient). Maybe if you shared the use case as for why you want a fixed number of words, we can better understand your needs and come up with alternatives

Loading file split input of hadoop map function into a data structure

I have designed a system where each map function is suppose to load its input (file split containing multiple CSV records) into a data structure and process them rather than processing line by line. There will be multiple Mapppers since I will be processing millions of records hence one mapper is totally inefficient.
I see from the example in WordCount, that the map function is reading line by line. Almost as of the map function is invoked for each line from the split it receives. I believe the input to this map should be the complete lines itself instead of sending it one line at a time.
Reduce function has other tasks at hand. So I guess, the map function could be tweaked to do the task its assigned. Is there a workaround?
From what you explain I understand that your map input is not single line but some structure built from the several lines. In this case - you should create your own InputFormat which will convert the input stream (from the split) to the sequence of objects of YourDataStructure cluss. And Your mapper will accept these YourDataStructure objects.
If the whole script is actually structure you want to process - I would suggest to do all the logic in mapper in one trick - you should know when there is last line in split. It can be done by inheriting TextInputFormat and tweak it to indicate you that there is last line. Then you build your structure in mapper, line by line, and when last line is indicated - do the job.

Resources