当先锋百科网

首页 1 2 3 4 5 6 7

环境需求:  系统:window 10  eclipse版本:Mars  Hadoop版本:2.6.0

资源需求:解压后的Hadoop-2.6.0,原压缩包自行下载:下载地址

丑话前头说:

以下的操作中,eclipse的启动均需要右键“管理员运行”!

在创建MapReduce的Project那块需要配置log4j(级别是debug),否则打印不出一些调试的信息,从而不好找出错的原因。配置这个log4j很简单,大家可以在网上搜索一下,应该可以找得到相关的配置。

1)首先需要利用ant编译自己的Hadoop-eclipse-plugin插件,你也可以自己网上搜索下载,我不喜欢用别人的东西,所以自己编译了一把,你们也可以参考我的另一篇博文,学着自己编译——《利用Apache Ant编译Hadoop2.6.0-eclipse-plugin》

2)把编译好的Hadoop插件放到eclipse目录下的plugins下,然后重启eclipse

3)打开window-->Preferences-->Hadoop Map/Reduce设置里面的Hadoop安装目录

a882eb8a4a945370854dd29d114597d2.png

4)打开window-->Show View找到MapReduce Tools下的Map/Reduce Location,确定

2359d8dda2be6c2b9ef31f45fd27e098.png

5)然后在eclipse的主界面就可以看到Map/Reduce Location的对话框了

112b6ba753785711fceb609fc937a974.png

6)新建一个Hadoop Location,修改HDFS和yarn的主节点和端口,finish。

ebdfb96f7a6e7374e3abc672edc1e31c.png

7)这时,在eclipse的Project Explorer中会看到HDFS的目录结构——DFS Locations

d1ae387fdb42252e5bd77ce9de24bd0e.png

注意:可能你打开这个目录结构的时候回存在权限问题(Premission),这是因为你在Hadoop的HDFS的配置文件hdfs-site.xml中没有配置权限(默认是true,意思是不能被集群外的节点访问HDFS文件目录),我们需要在这儿配置为false,重启hdfs服务,然后刷新上述dfs目录即可:

dfs.permissions.enabled

false

8)然后我们创建一个Map/Reduce Project,创建一个wordcount程序,我把Hadoop的README.txt传到/tmp/mrchor/目录下并改名为readme,输出路径为/tmp/mrchor/out。

packagecom.mrchor.HadoopDev.hadoopDev;importjava.io.IOException;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.io.LongWritable;importorg.apache.hadoop.io.Text;importorg.apache.hadoop.mapreduce.Job;importorg.apache.hadoop.mapreduce.Mapper;importorg.apache.hadoop.mapreduce.Reducer;importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public classWordCountApp {public static void main(String[] args) throwsException {

Configuration conf= newConfiguration();

Job job= Job.getInstance(conf, WordCountApp.class.getSimpleName());

job.setJarByClass(com.mrchor.HadoopDev.hadoopDev.WordCountApp.class);//TODO: specify a mapper

job.setMapperClass(MyMapper.class);//TODO: specify a reducer

job.setReducerClass(MyReducer.class);//TODO: specify output types

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(LongWritable.class);//TODO: specify input and output DIRECTORIES (not files)

FileInputFormat.setInputPaths(job, new Path("hdfs://master:8020/tmp/mrchor/readme"));

FileOutputFormat.setOutputPath(job,new Path("hdfs://master:8020/tmp/mrchor/out"));if (!job.waitForCompletion(true))return;

}public static class MyMapper extends Mapper{

Text k2= newText();

LongWritable v2= newLongWritable();

@Overrideprotected void map(LongWritable key, Text value, Mapper.Context context)throwsIOException, InterruptedException {

String[] split= value.toString().split(" ");for(String word : split) {

k2.set(word);

v2.set(1);

context.write(k2, v2);

}

}

}public static class MyReducer extends Reducer{long sum = 0;

@Overrideprotected void reduce(Text k2, Iterablev2s,

Reducer.Context context) throwsIOException, InterruptedException {for(LongWritable one : v2s) {

sum+=one.get();

}

context.write(k2,newLongWritable(sum));

}

}

}

9)右键Run As-->Run on Hadoop:

A)注意:这边可能报错:

java.io.IOException: HADOOP_HOME or hadoop.home.dir are not set.

这是因为你在安装eclipse的这台机子上没有配置Hadoop的环境变量,需要配置一下:

一)右键“我的电脑”或者“此电脑”选择属性:进入到高级系统设置-->高级-->环境变量配置-->系统变量

新建一个HADOOP_HOME,配置解压后的Hadoop-2.6.0的目录

5514b3692fc4d264042eab543fd656d8.png

二)重启eclipse(管理员运行)

10)继续运行wordcount程序,Run on Hadoop,可能会报如下错:

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z

at org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Native Method)

at org.apache.hadoop.io.nativeio.NativeIO$Windows.access(NativeIO.java:557)

at org.apache.hadoop.fs.FileUtil.canRead(FileUtil.java:977)

at org.apache.hadoop.util.DiskChecker.checkAccessByFileMethods(DiskChecker.java:187)

at org.apache.hadoop.util.DiskChecker.checkDirAccess(DiskChecker.java:174)

at org.apache.hadoop.util.DiskChecker.checkDir(DiskChecker.java:108)

at org.apache.hadoop.fs.LocalDirAllocator$AllocatorPerContext.confChanged(LocalDirAllocator.java:285)

at org.apache.hadoop.fs.LocalDirAllocator$AllocatorPerContext.getLocalPathForWrite(LocalDirAllocator.java:344)

at org.apache.hadoop.fs.LocalDirAllocator.getLocalPathForWrite(LocalDirAllocator.java:150)

at org.apache.hadoop.fs.LocalDirAllocator.getLocalPathForWrite(LocalDirAllocator.java:131)

at org.apache.hadoop.fs.LocalDirAllocator.getLocalPathForWrite(LocalDirAllocator.java:115)

at org.apache.hadoop.mapred.LocalDistributedCacheManager.setup(LocalDistributedCacheManager.java:131)

at org.apache.hadoop.mapred.LocalJobRunner$Job.(LocalJobRunner.java:163)

at org.apache.hadoop.mapred.LocalJobRunner.submitJob(LocalJobRunner.java:731)

at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:536)

at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1296)

at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1293)

at java.security.AccessController.doPrivileged(Native Method)

at javax.security.auth.Subject.doAs(Subject.java:422)

at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1628)

at org.apache.hadoop.mapreduce.Job.submit(Job.java:1293)

at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1314)

at com.mrchor.HadoopDev.hadoopDev.WordCountApp.main(WordCountApp.java:34)

通过源码查看,发现在NativeIO.java有说明——还是权限问题,可能是需要将当前电脑加入到HDFS授权的用户组:

但是,我们这边有一个更加巧妙的办法解决这个问题——将源码中的这个文件复制到你的MapReduce的Project中,这个意思是程序在执行的时候回优先找你Project下的class作为程序的引用,而不会去引入的外部jar包中找:

2b91cf732e36eaaffa72374e3e2b862d.png

11)继续运行wordcount程序,这次应该程序可以执行了,结果为:

dcfc99aa45b80786ec3e2ddd0c90d615.png

如果得到上面这个结果,说明程序运行正确,打印出来的是MapReduce程序运行结果。我们再刷新目录,可以看到/tmp/mrchor/out目录下有两个文件——_SUCCESS和part-r-00000:

649061723a384e45d67c01f68cf803dd.png

48d7488ca33cc022fb09c425be06ac8d.png

说明程序运行结果正确,此时,我们的eclipse远程调试Hadoop宣告成功!!!大家鼓掌O(∩_∩)O