介绍如何在Intellij Idea中通过创建maven工程配置MapReduce的编程环境。
一、软件环境
我使用的软件版本如下:
- Intellij Idea 2017
- Maven 3.3.9
- Hadoop
因为我们保证maven的纯净性,所以,这里我并没有整两套settings,而是重新开辟了一个maven环境,maven的前提是得有java环境。
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fgwb2errvsj30gu08n0ts.jpg)
然后我们在maven的settings里面配置路径和中央仓库,我用的是阿里的。
<localRepository>F:\MapReduce\apache-maven\apache-maven-3.3.9\repository</localRepository>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
到这里我们可以了,打开idea,选择 file->new->Project 左侧选择maven。
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fgwbhzxwnxj30of0fi0v8.jpg)
设置GroupId和ArtifactId,下一步。
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fgwbiqpdquj30of0fiab2.jpg)
设置工程存储路径,下一步。
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fgwbja6votj30of0fiwfh.jpg)
然后我们更改maven目录,依次更改maven目录、settings目录、本地仓库,其实使用idea的默认的maven也是可以的,这个随意吧,idea集成的maven有2有3版本的。
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fgwbq6z8nnj30zb0lljtx.jpg)
依照这样创建目录:
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fgwc2edj06j30da0dygmd.jpg)
- hadoop-common
- hadoop-hdfs
- hadoop-mapreduce-client-core
- hadoop-mapreduce-client-jobclient
log4j( 打印日志)
接下来,我们启动集群hadoop
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fgwcr1s39bj311l097jso.jpg)
查看192.168.181.100:50070查看是否启动成功。
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fgwd9fbbjdj312n0ontbw.jpg)
然后我们配置pom,
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fgwd6tyy0jj30h40oujtd.jpg)
在src/main/resources目录下新增log4j的配置文件log4j.properties,内容如下:
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fgwddse58xj310b0auwfv.jpg)
在工程根目录下新建input文件夹,input文件夹下新增dream.txt,随便写入一些单词:
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fgwdd31zchj30wj07jgmn.jpg)
在src/main/java目录下新建包,新增FileUtil.java,创建一个删除output文件的函数,以后就不用手动删除了。内容如下:
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fgwdfs2wu3j30cd0g03zb.jpg)
此时我们需要创建mr类了,正如我们之前所说的,我们需要Mapper类,并且重写map函数,
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fgwdjn7r7sj30k10f7403.jpg)
Reduce类的reduce函数:
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fgwdk4f2gmj30ix0b9gmd.jpg)
main方法,启动job:
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fgwdklanlpj30lz0ibgnu.jpg)
然后我们运行主类,出现:
[ERROR] 2017-06-24 16:37:25,627 method:org.apache.hadoop.util.Shell.getWinUtilsPath(Shell.java:373)%java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fgwdq6jyu7j30yk03z0ta.jpg)
原因是缺少winutils.exe文件,在网上下载一个,这个问题就OK了去网上差了,一般都会windows开发都会报错:
(1)缺少winutils.exe回报如下错误:
Could not locate executable null \bin\winutils.exe in the hadoop binaries
(2)缺少hadoop.dll
错误如下:
Unable to load native-hadoop library for your platform… using builtin-Java classes where applicable
所以呀,懂了,现在就要去服务器上面拉hadoop配置。
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fgwe2hqds4j30nq0b776e.jpg)