在Eclipse中配置Hadoop2.6.0
1、下载并配置插件
将下载的hadoop-eclipse-plugin-2.6.0.jar 放到Eclipse的dropins目录下,重启Eclipse.
2、配置 hadoop 安装目录
- window ->preference -> hadoop Map/Reduce -> Hadoop installation directory
3、配置Map/Reduce 视图
- window ->Open Perspective -> other->Map/Reduce -> 点击“OK”
- windows → show view → other->Map/Reduce Locations-> 点击“OK”
- 控制台会多出一个“Map/Reduce Locations”的Tab页
在“Map/Reduce Locations” Tab页 点击图标<大象+>或者在空白的地方右键,选择“New Hadoop location…”,弹出对话框“New hadoop location…”,配置如下内容:将ha1改为自己的hadoop用户
注意:MR Master和DFS Master配置必须和mapred-site.xml和core-site.xml等配置文件一致
打开Project Explorer,查看HDFS文件系统。
4、新建Map/Reduce任务
File->New->project->Map/Reduce Project->Next
编写WordCount类:记得先把服务都起来
- import java.io.IOException;
- import java.util.*;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.conf.*;
- import org.apache.hadoop.io.*;
- import org.apache.hadoop.mapred.*;
- import org.apache.hadoop.util.*;
- public class WordCount {
- public static class Map extends MapReduceBase implements 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, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
- String line = value.toString();
- StringTokenizer tokenizer = new StringTokenizer(line);
- while (tokenizer.hasMoreTokens()) {
- word.set(tokenizer.nextToken());
- output.collect(word, one);
- }
- }
- }
- public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
- public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
- int sum = 0;
- while (values.hasNext()) {
- sum += values.next().get();
- }
- output.collect(key, new IntWritable(sum));
- }
- }
- public static void main(String[] args) throws Exception {
- JobConf conf = new JobConf(WordCount.class);
- conf.setJobName("wordcount");
- conf.setOutputKeyClass(Text.class);
- conf.setOutputValueClass(IntWritable.class);
- conf.setMapperClass(Map.class);
- conf.setReducerClass(Reduce.class);
- conf.setInputFormat(TextInputFormat.class);
- conf.setOutputFormat(TextOutputFormat.class);
- FileInputFormat.setInputPaths(conf, new Path(args[0]));
- FileOutputFormat.setOutputPath(conf, new Path(args[1]));
- JobClient.runJob(conf);
- }
- }
5、配置运行时参数:右键-->Run as-->Run Confiugrations
user/ha1/input/hadoop是你上传在hdfs的文件夹(自己创建),里面放要处理的文件。ouput4放输出结果
将程序放在hadoop集群上运行:右键-->Runas -->Run on Hadoop,最终的输出结果会在HDFS相应的文件夹下显示。至此,ubuntu下hadoop-2.6.0 eclipse插件配置完成。
特别注意:按以上配置后,执行MR控制台没有日志详细信息。
解决方案,将hadoop-2.6.0/etc/haoop/log4j.properties 文件拷贝到 Eclipse项目下即可 (例如,将其拷贝到/home/hadoop/workspace/WordCount路径下<疑惑:在Linux文件系统中拷贝到该路径无效,但是从Eclipse视图中拷贝就有效。。且上述路径中相应的生成log4j.properties 文件>)。
http://blog.csdn.net/zythy/article/details/17397153