IntelliJ IDEA运行Hadoop程序

安装JDK 和 IntelliJ IDEA

下载Hadoop

下载地址:http://mirror.metrocast.net/apache/hadoop/common/

wget http://mirror.metrocast.net/apache/hadoop/common/hadoop-2.7.3/hadoop-2.7.3.tar.gz

tar -xvf hadoop-2.7.3.tar.gz

编写Hadoop程序并运行

创建Java项目

  1. 点击 File → New → Project…
    EE75BF23-F368-4955-8997-D14F0A3A47B3.png
  2. 点击 next
  3. 点击 next
  4. 设置 Project name 和 Project location
    790D1858-011A-42B5-B4D5-9693C49ACA70.png
  5. 点击 Finish

编写程序代码

代码:单词计数

引入Hadoop相关的jar

  1. 点击 File → Project Structure…
  2. 点击左侧 Modules
  3. 点击 + → 1 JARs or directories…

![506ED49C-9B5B-4F2E-8D6E-3B636332249D.png](http://storage.blog.ikyxxs.com/828ae4d0f99f42028a6514991b060729.png)
4. 添加 Hadoop → share → hadoop 下相应的目录 ![DCACEF27-244F-4CD4-B964-2BB340AF22D4.png](http://storage.blog.ikyxxs.com/532d1e3db4944d6f95dc1993d11a1205.png) 5. 点击 OK

添加 Artifacts

  1. 点击 File → Project Structure…
  2. 点击左侧 Artifacts
  3. 点击 + → JAR → Empty
    7E35FE59-345B-4149-BE0C-FF710CB44F5F.png
  4. 修改 Name 为 HadoopDemo
  5. 点击 + → Module Output 并选中 HadoopDemo 点击 OK
    D9CED12B-769B-4E33-AE56-F3E5B3BDC266.png
  6. 勾选 Build on make
    338D3082-44B9-4203-A9BC-1ADEAD0D9F2A.png
  7. 点击 OK

添加 Configurations

  1. 点击 Edit Configurations…
    DB3EB353-610A-44D7-ADC1-D4B5242F485B.png
  2. 点击 + → Application

![F280E9EE-AE64-4ED9-BEB7-8E7D857B477C.png](http://storage.blog.ikyxxs.com/d6c66ed6dcdd4afe809ae9ec1c58a448.png)
3. 修改 Name 为 HadoopDemo 4. 修改 Main Class 为 org.apache.hadoop.util.RunJar 5. 修改 Program arguments ``` /Users/mubai/iKYXXS/HadoopDemo/out/artifacts/HadoopDemo/HadoopDemo.jar com.demo.WordCount input/ output/ ``` ![FF2578CC-18EA-4C73-A0F7-4384896B8F59.png](http://storage.blog.ikyxxs.com/0c25440da9d6409093cbe3854429be62.png) 6. 点击 OK

添加测试数据

  1. 新建目录 input
  2. 新建文件 input.txt
hello world
hello world
hello hadoop

B8D8B223-6874-4180-AEE3-9EFDD63B5D1D.png

运行

EFCD82DD-CA3D-4C3C-969D-B1D15D0EE746.png

查看运行结果

运行结果在 output 目录下
01DCA479-9F81-467F-A754-54A0A1C47274.png

validate