一、安装Eclipse
1、下载-官方网址
https://www.eclipse.org/downloads
2、解压
tar -zxvf eclipse-committers-oxygen-3a-linux-gtk-x86_64.tar.gz
3、启动 (创建桌面快捷方式)
我们可以直接启动,进入eclipse解压目录使用./eclipse可以直接启动,但是为了后期方便启动,可以在桌面创建快捷方式,创建快捷方式步骤如下:
进入/usr/share/applications文件夹,在此文件夹下创建一个eclipse的快捷方式。
vim eclipse.desktop
在新建文件中添加如下内容
[Desktop Entry]
Encoding=UTF-8
Name=Eclipse
Comment=Eclipse IDE
Exec=/usr/local/eclipse/eclipse(eclipse存放路径)
Icon=/usr/local/eclipse/icon.xpm
Terminal=false
Type=Application
Categories=GNOME;Application;Development;
StartupNotify=true
这时,我们可以看到在/usr/share/applacations/文件夹下有了eclipse的快捷方式,最后把eclipse快捷方式复制到桌面就可以启动了。
二、在Eclipse中安装Hadoop插件
1.复制jar包
将hadoop-eclipse-plugin-2.7.1.jar文件复制到“/usr/local/eclipse/plugins”文件夹下。
2.在Eclipse中设置Hadoop的安装目录
重新启动Eclipse,在主菜单中选择“Window->Preferences”,在弹出的对话框中选择左边的Hadoop Map/Reduce,然后在右边的“Hadoop installation directory”中填入Hadoop的安装目录,如下图所示:
图1 Eclipse中设置Hadoop的安装目录
3.创建并配置Map/Reduce Locations
在主菜单中选择“Window->Show View->Other”,在弹出的对话框中找到并展开“MapReduce Tools”,然后选择“Map/Reduce Locations”,如下图所示:
图2 在Eclipse中选择Map/Reduce Locations
返回后出现Map/Reduce Locations子窗口,如下图所示:
图3 Map/Reduce Locations子窗口
在“Map/Reduce Locations”子窗口中右键单击,选择“New Hadoop Location”,创建一个新的Hadoop Location。
图4 在Eclipse中设置Hadoop Location
说明:Map/Reduce (V2) Master的Host和port是Yarn的主机和端口号,默认端口号是50020。DFS Master的Host和Port需要根据core-site.xml文件配置。默认端口号是9000。
配置正确后返回主窗口会看到新增的Hadoop Location,如下图所示:
图5 成功创建的Hadoop Location
三、开发第一个MapReduce程序
我们以典型的WordCount为例,在Eclipse中编写第一个MapReduce程序。1.数据文件
本次我们继续使用之前运行系统自带案例中创建的wordtest.txt文件,文件内容如下:
图6 wordtest.txt文件内容
2.创建Map/Reduce项目
在Eclipse主菜单中,选择“File->New->Other”,在弹出的对话框中选择“Map/Reduce Project”,如下图所示:
图6 创建Map/Reduce项目
图7 输入项目名称
3.编写代码
TokenizerMapper.java
package com.hellohadoop;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class TokenizerMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable 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);
}
}
}
IntSumReducer.java
package com.hellohadoop;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public 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);
}
}
WordCount.java
package com.hellohadoop;
import java.io.IOException;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
Job job = new Job(conf,"word count");
job.setJarByClass(WordCount.class);
//指定Mapper类
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
//指定Reducer类
job.setReducerClass(IntSumReducer.class);
//设置Reduce函数输出key的类型
job.setOutputKeyClass(Text.class);
//设置Reduce函数输出value的类型
job.setOutputValueClass(IntWritable.class);
//指定输入路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
//指定输出路径
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.out.println("OK");
//提交任务
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
4.运行程序
首先,要设置程序的输入参数,右键WordCount.java选择Run As->Run Configurations,如下图所示:
图8 为Java Application创建一个新的配置
接着打开"Arguments",输入参数,如下图所示:
图9 输入运行参数
配置完成后,返回主界面,点击Run As->Run on Hadoop,运行结果如下图所示:
图10 MapReduce程序运行结果