Wordcount.java
VI Wordcount.java
Import java.io.IOException;
Import Java.util.StringTokenizer;
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.fs.Path;
Import org.apache.hadoop.io.IntWritable;
Import Org.apache.hadoop.io.Text;
Import Org.apache.hadoop.mapreduce.Job;
Import Org.apache.hadoop.mapreduce.Mapper;
Import Org.apache.hadoop.mapreduce.Reducer;
Import Org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
Import Org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
Import Org.apache.hadoop.util.GenericOptionsParser; public class WordCount {public static class Tokenizermapper extends Mapper<object, text, text, in
twritable>{private final static intwritable one = new intwritable (1);
Private Text Word = new text ();
public void Map (Object key, Text value, Context context) throws IOException, Interruptedexception { StringTokenizer ITR = new StringtokenizER (value.tostring ());
while (Itr.hasmoretokens ()) {Word.set (Itr.nexttoken ());
Context.write (Word, one); }} public static class Intsumreducer extends Reducer<text,intwritable,text,intwrita
ble> {Private Intwritable result = new intwritable ();
public void reduce (Text key, iterable<intwritable> values, context
) throws IOException, interruptedexception {int sum = 0;
for (intwritable val:values) {sum + = Val.get ();
} result.set (sum);
Context.write (key, result); } public static void Main (string[] args) throws Exception {Configuration conf = new Configuration (
);
string[] Otherargs = new Genericoptionsparser (conf, args). Getremainingargs (); if (otherargs.length!= 2) {System.err.println ("Usage:wordcount <in> <out>");
System.exit (2);
Job Job = new Job (conf, word count);
Job.setjarbyclass (Wordcount.class);
Job.setmapperclass (Tokenizermapper.class);
Job.setcombinerclass (Intsumreducer.class);
Job.setreducerclass (Intsumreducer.class);
Job.setoutputkeyclass (Text.class);
Job.setoutputvalueclass (Intwritable.class);
Fileinputformat.addinputpath (Job, New Path (otherargs[0));
Fileoutputformat.setoutputpath (Job, New Path (otherargs[1));
System.exit (Job.waitforcompletion (true)? 0:1); }
}
Compile
Create a folder to save the generated compiled class file wordcount_classes
mkdir ~/wordcount_classes
You need to specify a build dependent jar package, separated by a colon
Javac-classpath/usr/lib/hadoop-0.20/hadoop-core-0.20.2-cdh3u6.jar:/usr/lib/hadoop-0.20/lib/commons-cli-1.2.jar -D wordcount_classes Wordcount.java
Packaged
JAR-CVF wordcount.jar-c wordcount_classes/.
Run
Hadoop jar ~/wordcount.jar WordCount Input Output
Input corresponds to Hdfs://user/root/input folder, output is the result of the folder, must not exist, otherwise it will not run successfully, output will be generated in/user/root/ouput position.
Results
root@bjidss46:~# Hadoop jar Wordcount.jar WordCount Input Output 13/11/20 16:10:07 INFO input. Fileinputformat:total input paths to process:1 13/11/20 16:10:07 WARN. Loadsnappy:snappy Native Library is available 13/11/20 16:10:07 INFO util. nativecodeloader:loaded the Native-hadoop library 13/11/20 16:10:07 INFO snappy. Loadsnappy:snappy Native Library loaded 13/11/20 16:10:07 INFO mapred. Jobclient:running job:job_201311201528_0008 13/11/20 16:10:08 INFO mapred. Jobclient:map 0% reduce 0% 13/11/20 16:10:12 INFO mapred. Jobclient:map 100% reduce 0% 13/11/20 16:10:16 INFO mapred. Jobclient:map 100% reduce 100% 13/11/20 16:10:16 INFO mapred. Jobclient:job complete:job_201311201528_0008 13/11/20 16:10:16 INFO mapred. jobclient:counters:26 13/11/20 16:10:16 INFO mapred. Jobclient:job counters 13/11/20 16:10:16 INFO mapred. jobclient:launched reduce Tasks=1 13/11/20 16:10:16 INFO mapred. jobclient:slots_millis_maps=4473 13/11/20 16:10:16 INFO mapred. Jobclient:total time spent by all reduces waiting after reserving slots (ms) =0 13/11/20 INFO 16:10:16. Jobclient:total time spent by all maps waiting after reserving slots (ms) =0 13/11/20 INFO 16:10:16. jobclient:launched map Tasks=1 13/11/20 16:10:16 INFO mapred. Jobclient:data-local map Tasks=1 13/11/20 16:10:16 INFO mapred. jobclient:slots_millis_reduces=3523 13/11/20 16:10:16 INFO mapred. Jobclient:filesystemcounters 13/11/20 16:10:16 INFO mapred. jobclient:file_bytes_read=57 13/11/20 16:10:16 INFO mapred. jobclient:hdfs_bytes_read=138 13/11/20 16:10:16 INFO mapred. jobclient:file_bytes_written=105460 13/11/20 16:10:16 INFO mapred. jobclient:hdfs_bytes_written=35 13/11/20 16:10:16 INFO mapred. Jobclient:map-reduce Framework 13/11/20 16:10:16 INFO mapred. Jobclient:map input Records=1 13/11/20 16:10:16 INFO mapred. Jobclient:reduce Shuffle bytes=57 13/11/20 16:10:16 INFO mapred. Jobclient:spilled records=8 13/11/20 16:10:16 INFO mapred. Jobclient:map output bytes=43 13/11/20 16:10:16 INFO mapred. Jobclient:cpu Time Spent (ms) =1530 13/11/20 16:10:16 INFO mapred. Jobclient:total committed heap usage (bytes) =504758272 13/11/20 INFO 16:10:16. Jobclient:combine input records=4 13/11/20 16:10:16 INFO mapred. jobclient:split_raw_bytes=111 13/11/20 16:10:16 INFO mapred. Jobclient:reduce input records=4 13/11/20 16:10:16 INFO mapred. Jobclient:reduce input groups=4 13/11/20 16:10:16 INFO mapred. Jobclient:combine output records=4 13/11/20 16:10:16 INFO mapred. Jobclient:physical memory (bytes) snapshot=334163968 13/11/20 16:10:16 INFO mapred. Jobclient:reduce output records=4 13/11/20 16:10:16 INFO mapred. Jobclient:virtual memory (bytes) snapshot=2914021376 13/11/20 16:10:16 INFO mapred. Jobclient:map Output records=4
Summarize
The reason for compiling execution using the original Javac method is to better understand the mapreduce process, export the jar when using eclipse do not package all the dependent jar packages, just hadoop-core-0.20.2-cdh3u6.jar and/usr /lib/hadoop-0.20/lib/commons-cli-1.2.jar can be.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/tools/