作者:飞教书的粉红色 | 来源:互联网 | 2023-09-25 10:59
eclipse的hadoop插件下载地址:https:github.comwinghchadoop2x-eclipse-plugin将下载的压缩包解压,将hadoop-eclips
- eclipse的hadoop插件下载地址:https://github.com/winghc/hadoop2x-eclipse-plugin
- 将下载的压缩包解压,将hadoop-eclipse-kepler-plugin-2.2.0这个jar包扔到eclipse下面的dropins目录下,重启eclipse即可
- 进入windows->Preference配置根目录如我的jre1.8是64位,jre7是32位,如果这里面没有,你直接add即可,选中你的64位jre环境之后,就会出现了。
- 之后写个wordcount程序测试一下,贴出我的代码如下,前提是你已经在hdfs上建好了input文件,并且在里面放些内容
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 {
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 {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable 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 {
// System.setProperty("hadoop.home.dir", "E:\\hadoop2.2\\");
Configuration cOnf= new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
// if (otherArgs.length != 2) {
// System.err.println("Usage: wordcount ");
// 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("hdfs://master:9000/input"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/output"));
boolean flag = job.waitForCompletion(true);
System.out.print("SUCCEED!" + flag);
System.exit(flag ? 0 : 1);
System.out.println();
}
}
|
至此,程序终于运行成功,刷新一下你的DFS即可,看到输出结果
windows 8.0上eclipse 4.4.0 配置centos 6.5 上的hadoop2.2.0开发环境