As an important family member of the Hadoop family, MapReduce (distributed computing model) is mainly used in the field of search, massive data computing, and other issues. Internal models adopt a divide-and-conquer approach. MapReduce is divided into two parts (Map and Reduce ). Shuffler is the preprocessing of Reduce. Both map and reduce Data Processing Methods Adopt the key pair method: [k1
As an important family member of the Hadoop family, MapReduce (distributed computing model) is mainly used in the field of search, massive data computing, and other issues. Internal models adopt a divide-and-conquer approach. MapReduce is divided into two parts (Map and Reduce ). Shuffler is the preprocessing of Reduce. Both map and reduce Data Processing Methods Adopt the key pair method: [k1
As an important family member of the Hadoop family, MapReduce (distributed computing model) is mainly used in the field of search, massive data computing, and other issues.
The internal model adopts the idea of "divide and conquer. MapReduce is divided into two parts (Map and Reduce ). Shuffler is the preprocessing of Reduce.
Map and reduce data are processed in key-value pairs: [k1, v1]-> MAP-> [K2, V2]-> Reduce-> [k3, v3].
MR Execution Process
(1). The client submits an mr jar package to JobClient (submission method: hadoop jar ...)
(2) JobClient communicates with JobTracker through RPC and returns the address (HDFS) and jobId for storing the jar package.
(3). The client writes the jar package to HDFS (path = address on hdfs + jobId)
(4). Start to submit the task (the description of the task, not jar, including jobid, location where the jar is stored, configuration information, and so on)
(5). JobTracker initializes the task.
(6) read the files to be processed on HDFS and start to calculate the input parts. Each part corresponds to a MapperTask.
(7). TaskTracker receives the task through the heartbeat mechanism (task description)
(8) download the required jar and configuration files.
(9) TaskTracker starts a java child sub-process to execute specific tasks (MapperTask or ReducerTask)
(10). Write the result to HDFS.
In hadoop2.0 and later versions, JobTracker named RM (resourceManage) TastTracker named NM (nodeManage)
MapReduce operation implements the wordcount function (that is, reads content from the text and calculates the number of times each word appears)
The program is divided into three categories (custom MAP method function implementation, custom REDUCE method function implementation, and finally the class is pieced together into mapreduce mode and imported into jar package, which is implemented in HDFS Distributed Function)
1. WCMapper class (map implementation)
Import java. io. IOException;
Import org. apache. hadoop. io. LongWritable;
Import org. apache. hadoop. io. Text;
Import org. apache. hadoop. mapreduce. Mapper;
/*
* Write mapper for wordcount
* Define mapper
* KEYIN: type of k1
* VALUEIN: v1 type
*
* Override the map Method
* Hadoop does not use the jdk default serialization mechanism (long-> longwriteable String-> Text)
*/
Public class WCMapper extends Mapper {
@ Override
Protected void map (LongWritable key, Text value,
Mapper . Context context)
Throws IOException, InterruptedException {
// TODO Auto-generated method stub
// Receives information V1
String line = value. toString ();
// Split data
String [] words = line. split ("");
// Loop
For (String w: words ){
// Record 1 at a time and Output
// Create a new key and value
Context. write (new Text (w), new LongWritable (1 ));
}
}
}
2. The WCReducer class implements the reduce function.
Import java. io. IOException;
Import org. apache. hadoop. io. LongWritable;
Import org. apache. hadoop. io. Text;
Import org. apache. hadoop. mapreduce. Cer CER;
/*
* KEYIN k2 type
* VALUEIN v2 type
*
* Override the CER Method
*/
Public class WCReducer extends Reducer {
@ Override
Protected void reduce (Text k2, Iterable V2s,
Reducer . Context context)
Throws IOException, InterruptedException {
// Receive data
Text k3 = k2;
// Define a counter
Long count = (long) 0;
// Cyclic v2s
For (LongWritable I: v2s ){
Count + = I. get ();
}
// Output
Context. write (k3, new LongWritable (count ));
}
}
3. wordCount class. Splice the first two classes in the mapreduce format
Import org. apache. hadoop. conf. Configuration;
Import org. apache. hadoop. fs. Path;
Import org. apache. hadoop. io. LongWritable;
Import org. apache. hadoop. io. Text;
Import org. apache. hadoop. mapreduce. Job;
Import org. apache. hadoop. mapreduce. lib. input. FileInputFormat;
Import org. apache. hadoop. mapreduce. lib. output. FileOutputFormat;
/*
* MapReduce
*
* Assemble custom map and reduce
*/
Public class wordCount {
Public static void main (String [] args) throws Exception {
// Job job = Job. instance (new Configuration (); // version hadoop2
Job job = new Job (new Configuration (); // version hadoop1
// 4. Note --- set the class in the main method
Job. setJarByClass (wordCount. class );
// 1. Set custom Mapper
Job. setMapperClass (WCMapper. class );
Job. setMapOutputKeyClass (Text. class );
Job. setMapOutputValueClass (LongWritable. class );
// Set the path (hdfs path) read by mapper)
FileInputFormat. setInputPaths (job, new Path ("/words.txt "));
// 2. Set reduce
Job. setReducerClass (WCReducer. class );
Job. setOutputKeyClass (Text. class );
Job. setOutputValueClass (LongWritable. class );
FileOutputFormat. setOutputPath (job, new Path ("/WcountResult "));
// 3. Submit
Job. waitForCompletion (true); // print the progress and details
}
}