MapReduce API (1)

Source: Internet
Author: User

First, we will introduce the usage of some built-in APIs:

Configuration conf = new Configuration (); // read hadoop Configuration
Job job = new Job (conf, "Job name"); // instantiate a job
Job. setOutputKeyClass (type of output Key );
Job. setOutputValueClass (type of output Value );
FileInputFormat. addInputPath (job, new Path (input hdfs Path ));
FileOutputFormat. setOutputPath (job, new Path (output hdfs Path ));
Job. setMapperClass (Mapper type );
Job. setCombinerClass (Combiner type );
Job. setReducerClass (CER type );

Next, we will introduce the development process in combination with eclipse:

1. Create a project and write the relevant code. Do not forget to specifyPackage hadoop. test;

Package hadoop. test;

Import java. io .*;

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. Mapper;
Import org. apache. hadoop. mapreduce. Cer CER;
Import org. apache. hadoop. mapreduce. lib. input. FileInputFormat;
Import org. apache. hadoop. mapreduce. lib. output. FileOutputFormat;

Public class MaxTemperature {

Static class MaxTemperatureMapper extends
Mapper <LongWritable, Text, Text, IntWritable> {
Private static final int MISSING = 9999;

Public void map (LongWritable key, Text value, Context conext)
Throws IOException, InterruptedException {
String line = value. toString ();
String year = line. substring (5, 9); // self-prepared data, simplified version of weather forecast data
Int airTemperature = Integer. parseInt (line. substring (15, 19); // self-prepared data, a simplified version of weather forecast data

If (airTemperature! = MISSING ){
Conext. write (new Text (year), new IntWritable (airTemperature ));
}

}

}

Static class MaxTemperatureReducer extends
CER <Text, IntWritable, Text, IntWritable> {
Public void reduce (Text key, Iterable <IntWritable> values,
Context context) throws IOException, InterruptedException {
Int maxValue = Integer. MIN_VALUE;
For (IntWritable value: values ){
MaxValue = Math. max (maxValue, value. get ());
}
Context. write (key, new IntWritable (maxValue ));
}
}

Public static void main (String [] args ){

If (args. length! = 2 ){
System. err
. Println ("Usage: MaxTemperature <input path> <output path> ");
System. exit (-1 );
}

Try {
Job job = new Job ();
Job. setJarByClass (MaxTemperature. class );

FileInputFormat. addInputPath (job, new Path (args [0]);
FileOutputFormat. setOutputPath (job, new Path (args [1]);

Job. setMapperClass (MaxTemperatureMapper. class );
Job. setReducerClass (MaxTemperatureReducer. class );

Job. setOutputKeyClass (Text. class );
Job. setOutputValueClass (IntWritable. class );

System. exit (job. waitForCompletion (true )? 0: 1 );

} Catch (IOException e ){
E. printStackTrace ();
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}

2. Introduce hadoop-related jar through eclipse
Right-click the root directory of the Project, right-click Properties, and an attribute panel will pop up. There is a column of attribute settings on the left. There is an entry named Java build path above. Click, then there will be a white box and a column of buttons on the right. There are four tabs on the white box. Click the libraries tag. The button on the right of the white box contains a button named "add external jar". Click, select hadoop-core-0.20.203.0.jar.
In this case, all errors prompted by the eclipse code detection will disappear.

3. right-click the exported jar package and select export from the Java file. Then, select JAR file and specify the path and name, for example, E: \ hadooptest. jar, next, and select main class, such as hadoop. test. maxtemperature4. Upload the JAR file to the hadoop cluster or pseudo-distributed cluster for testing. data transmission: bbbbb1991bbbbbb0040a ccccc1992cccccc0040c has been released. run the hadoop command:/root/hadoop-0.20.203.0/bin/hadoop JAR/root/java_hadoop/hadooptest. jar/root/log // root/out/Note the path for specific execution7. view results CAT/root/out/part-r-00000

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.