Hadoop Lesson Fifth: Java Development map/reduce

Source: Internet
Author: User

Configure the system environment variable hadoop_home, point to the Hadoop installation directory (if you don't want to mess with unnecessary trouble, don't include spaces or Chinese characters in the directory)
Add the Hadoop_home/bin to the PATH environment variable (not necessary, just for convenience)
If you are developing under Windows, you need to add a library file for Windows
Overwrite the bin directory shared in the disk Hadoop_home/bin
If not, copy the Hadoop.dll into the C:\Windows\System32 directory and you may need to restart the machine
Create a new project to introduce the jar files required by Hadoop

Code Wordmapper:

Import Java.io.ioexception;import Org.apache.hadoop.io.intwritable;import org.apache.hadoop.io.LongWritable; Import Org.apache.hadoop.io.text;import Org.apache.hadoop.mapreduce.Mapper; public class Wordmapper extends Mapper<longwritable,text, Text, intwritable> {     @Override    protected void Map (longwritable key, text value, mapper<longwritable, text, text, Intwritable>. Context context)            throws IOException, interruptedexception {        String line = value.tostring ();        string[] Words = Line.split ("");        for (String word:words) {            context.write (new Text (word), new intwritable (1));}}}     

Code Wordreducer:

Import Java.io.ioexception;import Org.apache.hadoop.io.intwritable;import org.apache.hadoop.io.LongWritable; Import Org.apache.hadoop.io.text;import org.apache.hadoop.mapreduce.Reducer; public class Wordreducer extends Reducer<text, intwritable, Text, longwritable> {     @Override    protected void reduce (text key, iterable<intwritable> values,            reducer<text, intwritable, text, Longwritable>. Context context) throws IOException, Interruptedexception {        Long count = 0;        for (intwritable v:values) {            count + = V.get ();        }        Context.write (Key, New Longwritable (count));    }     }

Code test:

Import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.fs.path;import Org.apache.hadoop.io.intwritable;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; public class Test {public static void main (string[] args) throws Exception {Configuration conf = new Configura                                 tion ();                 Job Job = job.getinstance (conf);        Job.setmapperclass (Wordmapper.class);        Job.setreducerclass (Wordreducer.class);        Job.setmapoutputkeyclass (Text.class);        Job.setmapoutputvalueclass (Intwritable.class);        Job.setoutputkeyclass (Text.class);                 Job.setoutputvalueclass (Longwritable.class);        Fileinputformat.setinputpaths (Job, "C:/bigdata/hadoop/test/test.txt"); Fileoutputformat.setoutputpath (Job, New Path ("C:/bigdata/hadoop/test/out/"));    Job.waitforcompletion (TRUE); }}

Pull files from HDFs to run locally

Fileinputformat.setinputpaths (Job, "hdfs://master:9000/wcinput/"); Fileoutputformat.setoutputpath (Job, New Path ("hdfs://master:9000/wcoutput2/"));

Note that this is to pull the HDFs file locally to run, if you observe the output, you will see the Jobid with the local word
At the same time this operation is not required yarn (self-stop yarn service to do experiments)
At the remote server execution

Conf.set ("Fs.defaultfs", "hdfs://master:9000/"); Conf.set ("Mapreduce.job.jar", "Target/wc.jar"), Conf.set ("Mapreduce.framework.name", "yarn"), Conf.set (" Yarn.resourcemanager.hostname "," Master "); Conf.set (" Mapreduce.app-submission.cross-platform "," true "); Fileinputformat.setinputpaths (Job, "/wcinput/"); Fileoutputformat.setoutputpath (Job, New Path ("/wcoutput3/"));

If you encounter permissions issues, configure the virtual machine parameters to execute-dhadoop_user_name=root
You can also take the four configuration files of Hadoop down to the SRC root directory, do not need to be manually configured, default to the Classpath directory to find
Or put the configuration file somewhere else, add it using Conf.addresource (. class.getClassLoader.getResourceAsStream), and do not recommend using absolute paths

Hadoop Lesson Fifth: Java Development map/reduce

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.