下载插件hadoop-eclipse-plugin-1.2.1.jar(需自行编译,见附件)
放到eclipse/plugins目录下[ /usr/lib64/eclipse/plugins ]
重启eclipse
选择"Window"菜单下的"Preference",在窗体的左侧菜单栏 里面会多出"Hadoop Map/Reduce"选项,点击此选项,选择Hadoop的安装目录(如我的Hadoop目录:E:\HadoopWorkPlat\hadoop-1.2.1)。
在eclipse的右上角出现蓝色大象logo,点击。之后,在正下方的区域将会多出一项Map/Reduce Locations的选项卡,点击该选项卡,右键新建New Hadoop Location。
设置如下
参数名 |
配置参数 |
说明 |
Location name |
hadoop |
当前链接的名字,可任意指定 |
MapReduce Master |
Host: 192.168.3.80 |
NameNode的IP地址,不能写主机名 |
MapReduce Master |
Port: 9001 |
MapReduce Port,参考配置的mapred-site.xml |
DFS Master |
Port: 9000 |
DFS Port,参考配置的core-site.xml |
User name |
grid |
Hadoop用户 |
之后,切换到Advanced parameters,需要修改以下参数
参数名 |
配置参数 |
说明 |
fs.default.name |
hdfs://192.168.3.80:9000/ |
参考core-site.xml |
hadoop.tmp.dir |
/home/grid/hadoop/tmp |
参考core-site.xml |
mapred.job.tracker |
192.168.3.80:9001 |
参考mapred-site.xml |
之后确认,这样便在eclipse左边出现了HDFS的文件结构。但是现在你只能查看,而不能添加修改文件。因此你还需要手工登录到HDFS上,并使用命令修改权限。
./bin/hadoop fs -chmod -R 777 /
如果是在Windows电脑上使用eclipse远程登录集群,且windows用户与Hadoop用户不一致时则需要在conf/hdfs-site.xm中添加以下配置,并重启Hadoop集群:
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
运行程序WordCount.java 在\src\examples\org\apache\hadoop\examples目录下
package org.apache.hadoop.examples; 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) { Configuration conf = new Configuration(); String[] otherArgs; try { 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); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
右键run configurations里添加以下两个参数,注意先将文本words里面包含单词Hello word Hello Hadoop
上传到Data目录下 bin/hadoop fs -put /home/grid/Date Data
执行,右键Run As -> Run on Hadoop
错误总结:
Invalid byte 1 of 1-byte UTF-8 sequence
1、查看conf/ 目录下的配置文件是否有中文字符
2、在配置“Hadoop Map/Reduce”选项时,是否路径中含有中文字符
运行程序时有以下异常
Exception in thread "main" java.io.IOException: Failed to set permissions of path: \tmp\hadoop-Administrator\mapred\staging\Administrator-519341271\.staging to 0700
Windows下文件权限问题,在Linux下可以正常运行,不存在这样的问题。
解决方法是,修改hadoop-1.2.1/src/core/org/apache/hadoop/fs/FileUtil.java里面的checkReturnValue,注释掉即可。
// if (!rv) {
// throw new IOException("Failed to set permissions of path: " + p +
// " to " +
// String.format("%04o", permission.toShort()));
// }
重新编译打包hadoop-core-1.2.1.jar,替换掉hadoop-1.2.1根目录下的jar即可。已编译jar包参看附件。