1.下载对应版本的hadoop插件(http://download.csdn.net/detail/tondayong1981/8680589)我直接用的2.7.0的 ,或者自己编译
2.把插件放到eclipse/plugins目录下
3.重启eclipse之后,window-preferences中会出现Hadoop Map/Reduce选项,选中并设置hadoop在windows下的目录
4.在show view中把map/reduce显示到工具栏
5.配置远程信息
点击右侧黑色齿
打开 Hadoop Location配置窗口:
配置Map/Reduce Master和DFS Mastrer,Host和Port配置成与core-site.xml的一致
注:windows的hosts(C:\Windows\System32\drivers\etc)文件中要增加master信息:192.168.0.49 master
6.上图Finish之后,可以看到以下页面:(如果没出现,检查hadoop集群是否启动成功,看日志是否有错误,先把错误调完,正确启动集群后再干别的)
7.新建测试项目
File—>Project,选择Map/Reduce Project,输入项目名称WordCount等。
在WordCount项目里新建class,名称为WordCount,代码如下:
package sun.demo;
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);
}
}
1、在HDFS上创建目录/opt/hadoop/tmp/out
hadoop fs -mkdir /opt/hadoop/tmp/out
2、拷贝本地README.txt到HDFS的input里
hadoop fs -copyFromLocal /opt/hadoop/README.txt /opt/hadoop/tmp/out
3、点击WordCount.java,右键,点击Run As—>Run Configurations,配置运行参数,即输入(本地和远程文件都可以)和输出文件夹
D:\eclipse-mars4.5.2\hadoop-workplace\test.txt hdfs://master:9000/opt/hadoop/tmp/out
注:hdfs目录为hadoop数据管理目录,与系统目录不一样
run之后可见结果
8.结果
centos7 搭建hadoop2.7.3完全分布式集群环境
如有不妥之处,还望各位批评指正