一、获取相应版本的hadoop-eclipse-plugin
本人用的是2.2.0版本的插件,大家可以根据自己需求下载不同版本的插件,这里给出下载地址:
https://pan.baidu.com/s/1bBvVl2Wc-dQJzBujB_XAKA
提取码:0cn9
二、解压并配置相应环境
1.将hadoop-eclipse-plugin-2.6.0.jar,复制到eclipse安装目录下的plugins下。
2.启动eclipse,点击Window中的Preferences
进入后在Data Management那一栏找到Hadoop Map/Reduce,设置获取Hadoop路径(hadoop安装路径)。如果没找到则说明插件未安装成功,可能没刷新或者是版本不对。
3.添加Map/Redure视图:
4.设置New Hadoop location
右键新创建一个New hadoop location,我这里已经创建过了:
连接之后刷新列表:
出现小蓝像则说明连接成功。
三、编写测试类
1.创建一个Map/Redure Project,右键 –> New –> Other –> Map/Redure Project 。
package test1; 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.LongWritable; 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.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; /** * MapReduce_WordCount * @author muster_hunter * */ public class WordCount { public static class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> { private final IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer token = new StringTokenizer(line); while (token.hasMoreTokens()) { word.set(token.nextToken()); context.write(word, one); } } } public static class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = new Job(conf); job.setJarByClass(WordCount.class); job.setJobName("wordcount"); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(WordCountMap.class); job.setReducerClass(WordCountReduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); } }
2.先运行一波该程序,不然在Configurations中找不到该程序的应用。右键Run As –> Run Configurations ,点击Arguments,填写输入目录,输出目录参数。
3.运行之后检测结果:
完成MapReduce测试,可以进行编写MapReduce脚本了。
总结
安装不是很难,具体遇到什么错解决什么错,不是什么大问题。