Hadoop mapreduce custom partition HashPartitioner, hadoopmapreduce
This article is published on my blog.
In the previous article, I wrote a simple WordCount program, and I also got a general idea about mapreduce running. In this article, I also talked about customizing partitions, sorting, and grouping, today, I will connect the code to continue to improve the implementation of custom partitions.
First, let's clarify how this partition works? To answer this question, first look at the results of the last code execution, we know that there is a file (part-r-00000) in the results, this file is the number of all words in the record, at this time, I have no idea. For example, if I want to put some special words in a separate file, and other words that I don't care about in another file, it would be much easier for me to view them, for example, if I want to count some people's hobbies, can I put my childhood in a file, an adult in a file, and so on. Yes, this is very useful. The output result is the most direct. Now let's analyze how to implement it:
We know that this output file is output by the reduce end, and the data on the reduce end is processed by the map function through shufflecopy to the reduce end. However, the output quantity on the map end is the number of reduce inputs, the map end is responsible for data division. There is a step in the shuffle process to partition. Let's take a look at the partition class HashPartitioner used in the previous Code and see the code:
public class HashPartitioner<K, V> extends Partitioner<K, V> { /** Use {@link Object#hashCode()} to partition. */ public int getPartition(K key, V value, int numReduceTasks) { return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks; }}
Here is a numReduceTasks variable. Where did it come from? It depends on who called this method. See MapTask. java can see that its write method is called. However, the partitions parameter of this method is:
jobContext.getNumReduceTasks();
We can continue to find this variable. The default value is 1, which is determined by the mapred. reduce. tasks configuration node. Now we do not know (key. hashCode () & Integer. MAX_VALUE) number of values but % 1 we can know that the result is 0; now I will inherit this class (or inherit its parent class Partitioner <K, V>) rewrite its getPartition method to implement partitions. See the following custom partition MyPartition code:
Import org. apache. hadoop. io. longWritable; import org. apache. hadoop. io. text; import org. apache. hadoop. mapreduce. lib. partition. hashPartitioner;/*** custom partition class * @ author Liang **/public class MyPartition extends HashPartitioner <Text, LongWritable> {@ Override public int getPartition (Text key, LongWritable value, int numReduceTasks) {return key. toString (). contains ("luoliang ")? 0: 1 ;}}
The getPartition function is rewritten above. If the key contains the key value of the string "luoliang", 0 is returned; otherwise, 1 is returned. After execution (hdfs://hadoop-master:9000/mapreduce/output/) There will be 2 files, one is the part-r-00000, the other is the part-r-00001. The part-r-00000 corresponds to the condition key. toString (). contains ("luoliang") for true!
Note that you must first add the following to the mian function:
job.setJarByClass(Test.class);
Then, package the program into a jar. jar file and upload it to the server. Run the following command:
hadoop jar jar.jar
If an error is reported during local debugging or running, it must be packaged on the server for running. The following two files are generated:
part-r-00000part-r-00001
First come here this time. Keep recording!