深入解析MapReduce架构设计与实现原理–读书笔记(4)MR及Partitio
MR解析 Mapper/Reducer封装了应用程序的数据处理逻辑。 所有存储在底层分布式文件系统上的数据均要解释成key/value的形式。并交给MR中的map/reduce函数处理,产生另外一些key/value。 Mapper 1)初始化 Mapper继承了JobConfigurable接口。该config方法允许通
MR解析
Mapper/Reducer封装了应用程序的数据处理逻辑。
所有存储在底层分布式文件系统上的数据均要解释成key/value的形式。并交给MR中的map/reduce函数处理,产生另外一些key/value。
Mapper
1)初始化
Mapper继承了JobConfigurable接口。该config方法允许通过JobConf参数对Mapper进行初始化。
2)Map操作
MapReduce会通过InputFormat中RecordReader从InputSplit获取一个key/value对,并交给map()函数处理:
void map(K1 key,V2 value,OutputCollector
3)清理
Mapper通过继承Colseable获得close方法,用户可通过实现该方法对Mapper进行清理。
Mapper类型
ChainMapper 链式作业;IdentityMapper对于输入不进行任何处理,直接输出;InvertMapper 交换key/value位置;
RegexMapper 正则表达式字符串分割;TokenMapper 将字符串分割成若干个token,可用作wordCount的Mapper;
LongSumReducer:以key为组,对long类型的value求累加和。
新的Mapper由接口变为抽象类;不再继承JobConfigurable和Closeable,而是直接在类中添加了setup和cleanup两个方法进行初始化和清理工作。
将参数封装到Context对象中,接口具有良好扩展性。
去掉MapRunnable接口,在Mapper中添加run方法,以方便用户定制map()函数的调用方法。
新API中,Reducer遍历value的迭代器类型变为Iterable
void reduce(KEYIN key,Iteratable values,Context context) throws IOException,InterrupteException{for(VALUEIN value:values){ context.write((KEYOUT) key,(VALUEOUT) value);}}
Partitioner接口的设计与实现
Partitioner的作用是对Mapper产生的中间结果进行分片,以便将同一分组的数据交给同一个Reducer处理,它直接影响Reduce阶段的负载均衡。
只包含一个待实现的方法getPartition。该方法包含3个参数,均由框架自传入,前面2个参数是key/value,第三个参数numPartitions表示每个Mapper的分片数,
也就是Reducer的个数。
HashPartitioner和TotalOrderPartitioner。其中HashPartitioner是默认实现:public int getPartition(K2 key,V2 value,int numReduceTasks){return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks ;}
TotalOrderPartitioner提供了一种基于区间的分片方法,通常用在数据全排序中,归并排序。
在Map阶段,每个MapTask进行局部排序;在Reduce阶段,启动一个ReduceTask进行全局排序。由于作业只能有一个ReduceTask,因此会产生瓶颈。
TotalOrderPartitioner按照大小将数据分成若干个区间,并保证后一个区间的所有数据均大于前一个区间数据。
步骤1:数据采样。
在client端通过采样获取分片的分割点。
采样数据:b,abc,abd,bcd,abcd,efg,hii,afd,rrr,mnk
排序后:abc,abcd,abd,afd,b,bcd,efg,hii,mnk,rrr
如果有4个Reduce Task,则采样数据的四等分点为abd,bcd,mnk
步骤2:Map阶段。
Mapper可采用IdentityMapper直接将输入数据输出,TotalOrderPartitioner将步骤1中获取的分割点保存到trie树中以便快速定位任意一个记录所在的区间,这样每个
Map Task产生R个区间,且区间中间有序。
步骤3:Reduce阶段。
每个Reducer对分配到的区间数据进行局部排序,最终得到全排序数据。
TotalOrderPartitioner有2个典型应用实例;TeraSort和HBase。
HBase内部数据有序,Region之间也有序。
原文地址:深入解析MapReduce架构设计与实现原理–读书笔记(4)MR及Partitioner, 感谢原作者分享。

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
