用eclipse编写mapreduce程序

自己的博客好像要过期了,把一些还有用的东西搬过来避难~


首先,下载插件

这是另一个插件,你可以看看。

然后,放到eclipse/plugin下,我是fedora系统,我放在了/usr/lib/eclipse/plugins下。

然后把插件重命名为:hadoop-eclipse-plugin-1.0.0.jar,

我的eclipse版本:

Eclipse Platform

Version: 3.6.1

Build id: M20100909-0800

我发现不改名不行,试了很多其他插件都不行。至于为什么改成这个名称,因为我发现有一个这个名称的插件可以被我的eclipse发现,但是运行不了。

然后,我们重启eclipse,可以在window-》open perspective下看见MapReduce(如果看不见,可能就是我上面说的问题),选择后,在弹出的对话框中你需要配置Location name,如myhadoop,还有Map/Reduce Master和DFS Master。这里面的Host、Port分别为你在mapred-site.xml、core-site.xml中配置的地址及端口。

现在, 我们应该可以看见平时project列表那,也就是Project Explorer下有一个DFS Locations.

我们下面已经有一个myhadoop了,就是刚才配置的,否则,可以重新配置一个。点击它,出现两个目录,就是我们的hadoop home目录和用户目录。

现在我们先写一个wordcount。

new一个MapReduce project,然后new 一个 mapReduce Driver,你也可以把map,reduce和dirver分开,这里我不分开。

代码:

import java.io.IOException;

import java.util.*;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.conf.*;

import org.apache.hadoop.io.*;

import org.apache.hadoop.mapred.*;

import org.apache.hadoop.util.*;

public class WordCount {

    public static class Map extends MapReduceBase implements Mapper {

        private final static IntWritable one = new IntWritable(1);

        private Text word = new Text();

        public void map(LongWritable key, Text value, OutputCollector output, Reporter reporter) throws IOException {

            String line = value.toString();

            StringTokenizer tokenizer = new StringTokenizer(line);

            while (tokenizer.hasMoreTokens()) {

                word.set(tokenizer.nextToken());

                output.collect(word, one);

            }

        }

     }

    public static class Reduce extends MapReduceBase implements Reducer {

         public void reduce(Text key, Iterator values, OutputCollector output, Reporter reporter) throws IOException {

             int sum = 0;

             while (values.hasNext()) {

                 sum += values.next().get();

             }

             output.collect(key, new IntWritable(sum));

         }

    }

    public static void main(String[] args) throws Exception {

        JobConf conf = new JobConf(WordCount.class);

         conf.setJobName(“wordcount”);

         conf.setOutputKeyClass(Text.class);

         conf.setOutputValueClass(IntWritable.class);

         conf.setMapperClass(Map.class);

         conf.setCombinerClass(Reduce.class);

         conf.setReducerClass(Reduce.class);

         conf.setInputFormat(TextInputFormat.class);

         conf.setOutputFormat(TextOutputFormat.class);

         FileInputFormat.setInputPaths(conf, new Path(args[0]));

         FileOutputFormat.setOutputPath(conf, new Path(args[1]));

         JobClient.runJob(conf);

    }

}


然后,因为我们运行时要命令行参数,我们点run-》run configurations-》arguments

program argument中输入:input output

input对应hdfs中的输入目录,里面有需要统计的文件,文件里面有单词。

output对应hdfs中的输出目录,要保证你这个目录原先不存在,因为hadoop为了不覆盖之前有用的运行结果,它是不允许覆盖。

然后,我们运行,在console中输出了过程的信息:

12/03/27 02:46:13 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=

12/03/27 02:46:13 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.

12/03/27 02:46:13 INFO mapred.FileInputFormat: Total input paths to process : 3

……

最后你可以看到你的hadoop中已经有了output目录并且里面有统计结果文件了。

我们可以用hadoop fs -ls output查看。

完毕。


你可能感兴趣的:(Hadoop,BASIC)