mapreduce之于eclipse环境搭建好了之后,就可以尝试运行一个例子了,这里我还是从入门及的WordCount跑跑看看。WordCount的例子位于Hadoop的安装目录这里我简化为${HADOOP_HOME}下src/examples/org/apache/hadoop/examples的WordCount.java文件。
首先在eclipse中new 一个mapreduce project,取名为hadoop-test,一次新建一个package和class,class名字就是WordCount.然后我们仔细看看这个java代码,发现main方法需要接受一个用于输入文件参数的path和一个用于输出文件参数的path。
package com.francis.hadoop; 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, IntWritable> { 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, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context 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); } }
于是我们可以需要创建这两个文件,并且put到我们的hdfs分布式文件系统上去。
在${HADOOP_HOME}下面创建一个input目录,然后创建两个文件file01.txt和file02.txt文件
file01.txt文件内容为hello world
file02.txt文件内容为hello hadoop
然后我们将input文件夹put到hdfs上去,input01作为显示文件夹名字(也作为运行WordCount接受输入文件的path)
${HADOOP_HOME}下bin/hadoop fs -put input input01
然后我们就可以在hdfs中查看input01文件夹 bin/hadoop fs -ls 或者去你的eclipse中user(1)下刷新查看.
这个时候就可以运行WordCount了,点击Run as -》 Run Configurations.在Arguments写入输入文件和输出文件的path参数
hdfs://localhost:9000/user/hadoop/input01 hdfs://localhost:9000/user/hadoop/output01(注意:这个参数文件的位置一定要在eclipse DFS Location中为准确。)
当程序运行完成之后,就可以在Eclipse中看到User下面hadoop下面不仅有一个input01文件夹和output01文件夹了,
而已用命令来查看output01文件夹的内容了 bin/hadoop fs -cat output01/* 就会显示运行的结果。当然运行的时候也可以在eclipse的console中查看结果。