一.需要的资源列表
hadoop2.7.2
Eclipse Version: Mars.2 Release (4.5.2)
Java jdk 1.7
winutils #2.7.2
hadoop.dll #2.7.2
hadoop-eclipse-plugin-2.7.2 #插件
二.hadoop-eclipse-plugin-2.7.2插件制作
问题保留:插件制作步骤暂时没有研究。所用的插件直接用现成下载的。
链接地址为:
http://download.csdn.net/detail/tondayong1981/9432425#comment
hadoop.dll和winutills链接地址为(ps:该版本为下载的是2.7.1,但是在2.7.2下面也可以运行):
http://download.csdn.net/detail/qq_16899785/9292533#comment
三.eclipse配置过程
第一步:将hadoop解压缩到某个目录下面。
第二步:将下载的hadoop.dll和winutills执行以下步骤:
- 将文件解压到hadoop的bin目录下
- 将hadoop.dll复制到C:\Window\System32下
- 添加环境变量HADOOP_HOME,指向hadoop目录
- 将%HADOOP_HOME%\bin加入到path里面
- 重启myeclipse或者eclipse
第三步:将hadoop-eclipse-plugin-2.7.2放在eclipse的pligins目录下,重启eclipse。
windows->perference->Hadoop Map/Reduce #配置hadoop安装目录。完成退出。
windows->open perspective->other->map/reduce.
新建一个map/reduce locations.具体设置如下:
Location Name : 此处为参数设置名称,可以任意填写
Map/Reduce Master (此处为Hadoop集群的Map/Reduce地址,应该和mapred-site.xml中的mapred.job.tracker设置相同)
DFS Master (此处为Hadoop的master服务器地址,应该和core-site.xml中的 fs.default.name 设置相同)
执行以下步骤:
dfs locations reconnected
可以看到连接到hdfs上了已经。
如果tmp目录下面出现permission denied 的问题。可以尝试两种解决方案。第一种修改用户与hadoop上的用户名一直。第二种,修改hdfs-site.xml文件,将dfs.permissions 设置为false。内容如下:
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
四.eclipse编写wordcount程序
new->other->map/reduce project
new->class->org.apache.hadoop.examples.WordCount
new->log4j.properties
####
log4j.rootLogger=debug,stdout,R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=mapreduce_test.log
log4j.appender.R.MaxFileSize=1MB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.logger.com.codefutures=DEBUG
####
WorldCount代码如下:
package org.apache.hadoop.examples;
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);
}
}
运行步骤:
run as-> run conf ->arguments
hdfs://192.168.186.101:9000/input/w3
hdfs://192.168.186.101:9000/input/out3
该路径为hdfs上输入文件的木和输出文件目录。输出文件目录会自动创建。(如果已经存在该目录,需要重新输入一个输出目录)
运行成功