1、下载hadoop对应hadoop-eclipse-plugin(版本一定要对应),也可以自己编译,但一般网上都有对应的版本
2、将hadoop-eclipse-plugin放到eclipse\plugins目录下,重启eclipse(这里需要注意一点:并不是所有版本eclipse都能安装成功,有些版本确实不兼容,我就受过这样的气;
推荐一个版本:indigo,下载地址:http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/indigosr2)
3、配置连接:
Location name 这里就是起个名字,随便起
Map/Reduce(V2) Master Host 这里就是虚拟机里hadoop master对应的IP地址,下面的端口对应 hdfs-site.xml里dfs.datanode.ipc.address属性所指定的端口
DFS Master Port: 这里的端口,对应core-site.xml里fs.defaultFS所指定的端口
如下图:




eclipse出现如下:

4、连接权限配置(可以通过eclipse客户端删除服务器文件)
自己测试环境无需安全连接:
(1)修改hdfs-site.xml
- <property>
- <name>dfs.permissions</name>
- <value>false</value>
- </property>
(2)运行命令彻底把hadoop的安全检测关掉
- bin/hadoop dfsadmin -safemode leave
-
- bin/hadoop fs -chmod 777 /
(3)重启hadoop
5、运行wordcount MR程序
代码直接在hadoop-2.7.1\share\hadoop\mapreduce\hadoop-mapreduce-examples-2.7.1.jar里面找
- 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 static final 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()) {
- this.word.set(itr.nextToken());
- context.write(this.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();
- }
- this.result.set(sum);
- context.write(key, this.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> [<in>...] <out>");
- System.exit(2);
- }
- Job job = Job.getInstance(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);
- for (int i = 0; i < otherArgs.length - 1; ++i) {
- FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
- }
- FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- }
- }
配置输入输出跟变量:


6、环境变量配置


7、直接运行就OK