Hadoop Learning Note (v): Java development MapReduce

Source: Internet
Author: User

1. The MapReduce flowchart (excerpted from the Horse Soldier Teacher Video), which we developed is one of these two (red box) processes. To summarize this diagram, input is the file we need to process (a chunk of the file on the Datanode); split is to split the file, the default is to split the line, the result of the split is a key-value pair, the key is the starting position of this line, Value is the content of this line; Map is what we need to develop, that is, the processing of this line of data, the result is a key-value pair; Shuffle is a summary of the data from the previous step, merging the same key together, Put all of the value in a container; reduce reduces the value of the previous step in the container to sum, is also a key-value;

2. If you are developing on a Windows machine, you need to configure the environment:

A). First add the environment variable for Hadoop hadoop_home to the installation directory for Hadoop:

b). Add the Hadoop_home/bin to the PATH environment variable (not necessary)

c). Overwrite hadoop_home/bin (https://github.com/srccodes/hadoop-common-2.2.0-bin download bin file here)

d). Copy the Hadoop.dll to the C:\Windows\System32 directory (restart the computer)

3. Create a new Java project, introduce the corresponding jar package, the jar package is located in the Share/hadoop in the Hadoop_home directory, the following is the jar manifest:

a). Common under Hadoop-common-2.7.3.jar, all jar packages have been common/lib.

b). All jar packages under HDFs, and all jar packages under Hdfs/lib.

c). All jar packages under MapReduce, and all jar packages under Mapreduce/lib.

d). Yarn under all jar packages, and all jar packages under Yarn/lib.

4. Write the map layer code and create a new Wordmapper.java class:

1 Importjava.io.IOException;2 3 Importorg.apache.hadoop.io.IntWritable;4 Importorg.apache.hadoop.io.LongWritable;5 ImportOrg.apache.hadoop.io.Text;6 ImportOrg.apache.hadoop.mapreduce.Mapper;7 8  Public classWordmapperextendsmapper<longwritable, text, text, intwritable> {9 @OverrideTen     protected voidMap (longwritable key, text value, mapper<longwritable, text, text, Intwritable>. Context context)throwsIOException, interruptedexception { One         FinalIntwritable one =NewIntwritable (1); AString s =value.tostring (); -string[] Words = S.split (""); -          for(String word:words) { theContext.write (NewText (Word), one); -         } -     } -}

5. Write the reduce layer code and create a new Wordreduce.java file:

1 Importjava.io.IOException;2 3 Importorg.apache.hadoop.io.IntWritable;4 Importorg.apache.hadoop.io.LongWritable;5 ImportOrg.apache.hadoop.io.Text;6 ImportOrg.apache.hadoop.mapreduce.Reducer;7 8  Public classWordreduceextendsReducer<text, Intwritable, Text, longwritable> {9 @OverrideTen     protected voidReduce (text key, iterable<intwritable> values, Reducer<text, intwritable, text, Longwritable>. Context content)throwsIOException, interruptedexception { One         LongCount = 0; A          for(intwritable v:values) { -Count + =v.get (); -         } theContent.write (Key,Newlongwritable (count)); -     } -}

6. Programming test layer Code, new Test.java (program can run independently in Windows without starting Hadoop service)

1 Importorg.apache.hadoop.conf.Configuration;2 ImportOrg.apache.hadoop.fs.Path;3 Importorg.apache.hadoop.io.IntWritable;4 Importorg.apache.hadoop.io.LongWritable;5 ImportOrg.apache.hadoop.io.Text;6 ImportOrg.apache.hadoop.mapreduce.Job;7 ImportOrg.apache.hadoop.mapreduce.lib.input.FileInputFormat;8 ImportOrg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;9 Ten  Public classTest { One      Public Static voidMain (string[] args)throwsException { AConfiguration conf =NewConfiguration (); -  -Job Job =job.getinstance (conf); the  -Job.setmapperclass (Wordmapper.class); -Job.setreducerclass (wordreduce.class); -  +Job.setmapoutputkeyclass (Text.class); -Job.setmapoutputvalueclass (intwritable.class); +  AJob.setoutputkeyclass (Text.class); atJob.setoutputvalueclass (longwritable.class); -  -Fileinputformat.setinputpaths (Job, "E:/input.txt"); -Fileoutputformat.setoutputpath (Job,NewPath ("e:/out/")); -  -Job.waitforcompletion (true); in     } -}

7. Run the test code and go to the output directory to view:

8. Open the file to view the results of the operation:

9. Throw this program into Hadoop and run it.

10. First prepare a file to be processed on Hadoop

11. Modify the test code, just modify the two lines, and then run (remember to start Hadoop and yarn):

Fileinputformat. setinputpaths (Job, "Hdfs://192.168.74.100:9000/input");

Fileoutputformat. Setoutputpath (Job, new Path ("Hdfs://192.168.74.100:9000/output"));

12. View the results of a run in Hadoop

13. Note that (watch Eclipse's console), this task is still executed locally, that is, the program needs to be processed to download the file locally, and then processed, it is obvious that if the file is large, this is very inappropriate.

14. What we want to do is to execute the program to Hadoop instead of downloading the Datanode file locally and then executing it with the local program. After modifying the test code as follows, note that two configurations are required here to add the master IP in the Windows host file:

Then, put the project into a jar package and place it under the project root directory:

When running, right-click the test file, select Run configurations, enter-dhadoop_user_name=root in arguments VM arguments, then click Run

1 Importorg.apache.hadoop.conf.Configuration;2 ImportOrg.apache.hadoop.fs.Path;3 Importorg.apache.hadoop.io.IntWritable;4 Importorg.apache.hadoop.io.LongWritable;5 ImportOrg.apache.hadoop.io.Text;6 ImportOrg.apache.hadoop.mapreduce.Job;7 ImportOrg.apache.hadoop.mapreduce.lib.input.FileInputFormat;8 ImportOrg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;9 Ten  Public classTest { One      Public Static voidMain (string[] args)throwsException { AConfiguration conf =NewConfiguration (); -  -Conf.set ("Fs.defaultfs", "Hdfs://192.168.74.100:9000/"); theConf.set ("Mapreduce.job.jar", "Mr.jar"); -Conf.set ("Mapreduce.framework.name", "yarn"); -Conf.set ("Yarn.resourcemanager.hostname", "Master"); -Conf.set ("Mapreduce.app-submission.cross-platform", "true"); +  -Job Job =job.getinstance (conf); +  AJob.setmapperclass (Wordmapper.class); atJob.setreducerclass (wordreduce.class); -  -Job.setmapoutputkeyclass (Text.class); -Job.setmapoutputvalueclass (intwritable.class); -  -Job.setoutputkeyclass (Text.class); inJob.setoutputvalueclass (longwritable.class); -  toFileinputformat.setinputpaths (Job, "/input/"); +Fileoutputformat.setoutputpath (Job,NewPath ("/output2/")); -  theJob.waitforcompletion (true); *     } $}

15. View the task ID

16. Run through the above code, view the test results (self-viewing), the host browser to see the task just now, found that the task was performed on Slave3.

Hadoop Learning Note (v): Java development MapReduce

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.