hadoop之eclipse插件

本文介绍了如何在Eclipse中安装hadoop-eclipse-plugin,配置连接到Hadoop集群,以及运行wordcount MR程序的步骤。首先,需要下载对应版本的插件并放入Eclipse的plugins目录,接着配置Hadoop的Master IP和端口。然后,通过修改hdfs-site.xml禁用权限检查以简化测试环境。最后,提供了wordcount示例代码,并指导设置输入输出路径,完成环境变量配置后即可运行程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1下载hadoop对应hadoop-eclipse-plugin(版本一定要对应),也可以自己编译,但一般网上都有对应的版本

2hadoop-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

[html]  view plain  copy
  1. <property>  
  2.     <name>dfs.permissions</name>  
  3.     <value>false</value>  
  4. </property>  
(2)运行命令彻底把hadoop的安全检测关掉

[html]  view plain  copy
  1. bin/hadoop dfsadmin -safemode leave  
  2.   
  3. bin/hadoop fs -chmod 777 /  
(3)重启hadoop

5、运行wordcount MR程序

代码直接在hadoop-2.7.1\share\hadoop\mapreduce\hadoop-mapreduce-examples-2.7.1.jar里面找

[java]  view plain  copy
  1. import java.io.IOException;  
  2. import java.util.StringTokenizer;  
  3.   
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.IntWritable;  
  7. import org.apache.hadoop.io.Text;  
  8. import org.apache.hadoop.mapreduce.Job;  
  9. import org.apache.hadoop.mapreduce.Mapper;  
  10. import org.apache.hadoop.mapreduce.Reducer;  
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  13. import org.apache.hadoop.util.GenericOptionsParser;  
  14.   
  15. public class WordCount {  
  16.   
  17.     public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {  
  18.         private static final IntWritable one = new IntWritable(1);  
  19.         private Text word = new Text();  
  20.   
  21.         public void map(Object key, Text value, Context context) throws IOException, InterruptedException {  
  22.             StringTokenizer itr = new StringTokenizer(value.toString());  
  23.             while (itr.hasMoreTokens()) {  
  24.                 this.word.set(itr.nextToken());  
  25.                 context.write(this.word, one);  
  26.             }  
  27.         }  
  28.     }  
  29.   
  30.     public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {  
  31.         private IntWritable result = new IntWritable();  
  32.   
  33.         public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {  
  34.             int sum = 0;  
  35.             for (IntWritable val : values) {  
  36.                 sum += val.get();  
  37.             }  
  38.             this.result.set(sum);  
  39.             context.write(key, this.result);  
  40.         }  
  41.     }  
  42.   
  43.     public static void main(String[] args) throws Exception {  
  44.         Configuration conf = new Configuration();  
  45.         String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  46.         if (otherArgs.length < 2) {  
  47.             System.err.println("Usage: wordcount <in> [<in>...] <out>");  
  48.             System.exit(2);  
  49.         }  
  50.         Job job = Job.getInstance(conf, "word count");  
  51.         job.setJarByClass(WordCount.class);  
  52.         job.setMapperClass(TokenizerMapper.class);  
  53.         job.setCombinerClass(IntSumReducer.class);  
  54.         job.setReducerClass(IntSumReducer.class);  
  55.         job.setOutputKeyClass(Text.class);  
  56.         job.setOutputValueClass(IntWritable.class);  
  57.         for (int i = 0; i < otherArgs.length - 1; ++i) {  
  58.             FileInputFormat.addInputPath(job, new Path(otherArgs[i]));  
  59.         }  
  60.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));  
  61.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  62.     }  
  63. }  


配置输入输出跟变量:


6、环境变量配置



7、直接运行就OK


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值