Six: Inputsplit Shard size Control Map number

Source: Internet
Author: User

In the implementation of the Hadoop program, we have to set the number of maps according to different situations. 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, and controlling how many shards in the InputFormat needs to control the size of each Inputsplit shard.


2. How to control the size of each split shard

 hadoop the default input format 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 find Textinputformat inherits Fileinputformat, and in Textinputformat, We did not find a specific section for file segmentation, Textinputformat should be using the Fileinputformat default Inputsplit method. So, we open the source code for Fileinputformat, where we found it:

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", &NBSP;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

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/77/77/wKioL1ZoXBiDWTdLAAF0cdtYI1o731.png "title=" captures 32a. PNG "alt=" Wkiol1zoxbidwtdlaaf0cdtyi1o731.png "/>

This article from "in order to finger that direction" blog, declined reprint!

Six: Inputsplit Shard size Control Map number

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.