Control the number of maps by Inputsplit shard size

Source: Internet
Author: User

Preface: In the specific implementation of the Hadoop program, we have to set the number of maps according to different circumstances. In addition to setting the maximum number of maps that can be run on each node, we also need to control the number of tasks that actually perform the map operation.
1. How to control the number of map tasks actually running
We know that when the file is uploaded to the HDFs file system, it is cut into different block blocks (the default size is 64MB). However, each map processing block is sometimes not the physical block size of the system. The actual processing of the input block size is based on Inputsplit to set, then inputsplit how to get it?

Inputsplit=math.max (MinSize, Math.min (MaxSize, blockSize) Where: Minsize=mapred.min.split.size maxsize= Mapred.max.split.size

We control how many maps are actually used by changing the number of shards in the InputFormat, while controlling how many shards in the InputFormat need to control the size of each Inputsplit shard
2. How to control the size of each split shard
The default input format for Hadoop is Textinputformat, which defines how files are read and how they are fragmented. We open his source file (in the Org.apache.hadoop.mapreduce.lib.input package):

package org.apache.hadoop.mapreduce.lib.input;import org.apache.hadoop.fs.path;import  org.apache.hadoop.io.longwritable;import org.apache.hadoop.io.text;import  org.apache.hadoop.io.compress.compressioncodec;import  org.apache.hadoop.io.compress.compressioncodecfactory;import  org.apache.hadoop.io.compress.splittablecompressioncodec;import org.apache.hadoop.mapreduce.inputformat; Import org.apache.hadoop.mapreduce.inputsplit;import org.apache.hadoop.mapreduce.jobcontext;import  org.apache.hadoop.mapreduce.RecordReader;import org.apache.hadoop.mapreduce.TaskAttemptContext; public class textinputformat extends fileinputformat<longwritable, text> {     @Override    public RecordReader<LongWritable, Text>       createrecordreader (inputsplit split,              &nBsp;         taskattemptcontext context)  {      return new linerecordreader ();   }    @Override     protected boolean issplitable (Jobcontext context, path file)  {      compressioncodec codec =        new  compressioncodecfactory (Context.getconfiguration ()). Getcodec (file);      if   (NULL&NBSP;==&NBSP;CODEC)  {       return true;      }     return codec instanceof  splittablecompressioncodec;   }}

Through the source code, we found that Textinputformat inherited the Fileinputformat, and in Textinputformat, we did not find a specific section for file segmentation, The Textinputformat should be using the Fileinputformat default Inputsplit method. So, we open the source code for Fileinputformat, where we found:
 

 public static void  Setmininputsplitsize (job job,long size)  {     job.getconfiguration (). Setlong ("Mapred.min.split.size",  size);    }   public static long  getminsplitsize (jobcontext job)  {     return  Job.getconfiguration (). Getlong ("Mapred.min.split.size",  1l);   }     Public static void setmaxinputsplitsize (job job,long size)  {      job.getconfiguration (). Setlong ("Mapred.max.split.size",  size);   }    public static long getmaxsplitsize (Jobcontext context)  {      return context.getconfiguration (). Getlong ("Mapred.max.split.size", Long.max_value);    } 

As we can see, Hadoop implements the definition of mapred.min.split.size and mapred.max.split.size here, with the default values of 1 and the largest of long. Therefore, we can control the size of the Inputsplit shard by simply re-assigning the values to these two values in the program.
3. If we want to set the Shard size to 10MB
Then we can add the following code to the driver section of the MapReduce program:

Textinputformat.setmininputsplitsize (job,1024l);//Set minimum shard size textinputformat.setmaxinputsplitsize (job,1024x1024x 10L);//Set maximum shard size

Control the number of maps by Inputsplit shard size

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.