8 Eclipse下搭建Hadoop2.7.2开发环境
8.1下载并安装Eclipse
因为之前发现安装最新版本的Eclipse后,在Ubuntu中无法正常使用Eclipse的相关菜单选项,所以这里下载的是一个比较旧的SR2版本的Eclipse其下载地址为:http://mirror.rise.ph/eclipse/technology/epp/downloads/release/juno/SR2/eclipse-jee-juno-SR2-linux-gtk.tar.gz
下载完成之后,将压缩包解压到你需要的地方,例如这里解压到了/home/hadoop/HadoopRoot文件夹下。如图8.1-1所示。
下载hadoop-eclipse-plugin-2.7.0.jar(见http://pan.baidu.com/s/1mh62fVy ),将此文件放到Eclipse目录下的plugin目录中,如图8.1-2所示。
进入到Eclipse目录中,双击eclipse文件运行Eclipse IDE,如图8.1-3所示。
进入Eclipse后,点击菜单WindowàPreference,如图8.1-4所示。
如图8.1-5所示,设置hadoop的安装目录。
8.2创建Map/Reduce工程
在Eclipse中新建一个工程,并选中Map /Reduce Project,如图8.2-1所示。
新建一个WordCount.java文件,并输入如下的代码。
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.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.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object 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); } } } public static 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); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: wordcount <in> <out>"); System.exit(2); } Job job = new Job(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
如图8.2-2所示,为创建的代码。
在工作空间的下方找到Map/Reduce Locations选项卡,点击这个选项卡,然后在空白部分右键,然后在弹出的菜单中选中“New Hadoop Location“,如图8.2-3所示。
在弹出的配置页面中配置一下名称并修改一下DFS端口(这个端口应该与core-site.xml中的端口一致),如图8.2-4所示,这里的端口应该是9000。
配置后,如果配置正确,在左侧的TestLocation中可以看到DFS中的文件信息,如图8.2-5所示,可以看到之前用命令行创建的input和output文件夹。
如图8.2-6所示,点击Run Configurations,修改一下运行配置。
如图8.2-7所示,在Java Application中新建一个配置并将配置名称修改为WordCount,并在Arguments中输入参数:hdfs://localhost:9000/user/hadoop/input hdfs://localhost:9000/user/hadoop/outputtest ,这两个参数实际上就是main函数执行时的输入参数,这里用于输入输出。
点击run按钮,然后选中Run on Hadoop,如图8.2-8所示。
运行后可以在左侧相应的文件夹上右键点击刷新,即可看到最新生成的outputtest文件夹了,如图8.2-9和8.2-10所示。
至此,Eclipse的Hadoop开发环境已经搭建完毕。
转载请注明:悠然品鉴 » Ubuntu 下Hadoop+Eclipse环境搭建笔记<四> Eclipse下搭建Hadoop