一:下载eclipse,并解压安装(就不再多说了)
1、 目前这个插件还在开发中,你可以自己下载源码编译,网上也有人发布编译好的jar。 源码地址: https://github.com/winghc/hadoop2x-eclipse-plugin
2、把下载好的jar,复制到 F:\eclipse\plugins\ 目录下,然后重启eclipse
windows --> preferences--> hadoop map/reduce
鄙人才疏学浅,若有错误之处,还望指点!
我这里装在F:\eclipse-4.5.1二:下载 Hadoop2.6.0 包到本Windows上,并解压到F盘(这里你自己指定位置)
我这里装在F:\apache-hadoop-2.6.0三:下载插件:hadoop-eclipse-plugin-2.6.0.jar,并安装到eclipse中
1、 目前这个插件还在开发中,你可以自己下载源码编译,网上也有人发布编译好的jar。 源码地址: https://github.com/winghc/hadoop2x-eclipse-plugin
2、把下载好的jar,复制到 F:\eclipse\plugins\ 目录下,然后重启eclipse
四:在Eclipse中配置,测试DFS访问远程hadoop集群目录
1、指定Windows上的hadoop根目 录(即:$HADOOP_HOME)windows --> preferences--> hadoop map/reduce
2、打开eclipse窗口,切换到 map/reduce视图
windows --> show view --> other
3、添加一个 map/reduce Location,并配置信息
4、 点击Finish配置完成
5. 此时刷新一下DFS,既可以看到你远程hadoop集群上的文件夹了
五:创建HelloWord项目
File
-->
New
-->
Other --> MAP/Reduce Project --> Next
输入项目名称,检查没问题,就下一步下一步,完成ok。创建完成后会自动引用很多hadoop需要用到的jar,这里不用去管他。
接着创建WordCount程序:
package com.wordcount;
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();
Job job = Job.getInstance(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(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
右键-->Run As-->Run Configurations,配置好信息以后,点击运行
查看结果:
七:可能遇到的错误分析:
1、
在DFS Lcation 上不能远程访问文件及操作
:
这里报错,说没有权限的问题。你需要把hadoop集群中master机子上的hdfs-site.xml文件中添加:
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
2、log4j问题(这个问题最好处理一下,不然hadoop的有一些错误信息打印不出来)
只需在src文件下创建log4j.properties文件
3、Exception in thread "main" org.apache.hadoop.mapred.InvalidJobConfException: Output directory not set.
说明你的输出目录重复,先删除,再运行。
3、java.io.IOException: Could not locateexecutable null/bin/winutils.exe in the Hadoop binaries.
这个问题是因为hadoop2.6.0在Windows上运行时却少几个文件,需要自己添加一下。
3.1、首先下载hadoop-native-64-2.6.0.tar,解压以后把里面的所有文件拷贝到F:\apache-hadoop-2.6.0\hadoop-2.6.0\bin(你的hadoop目录)
3.2、在把hadoop.dll文件拷贝到C:\Windows\System32目录下
3.3、配置hadoop的
环境变量,新建HADOOP_PATH=F:\apache-hadoop-2.6.0\hadoop-2.6.0 然后在Path的最前面添加%HADOOP_HOME%\bin; ,保存退出。修改环境变量需重启电脑才能生效,也可不重启,但需在代码中插入一行,即
public static void main(String[] args) throws Exception {
System.setProperty("hadoop.home.dir", "F:/apache-hadoop-2.6.0/hadoop-2.6.0");
Configuration conf = new Configuration();
Job job = Job.getInstance(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(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
鄙人才疏学浅,若有错误之处,还望指点!