按照官网上的教程装好hadoop的环境后就可以开发了,哈哈,怎么装环境这里就不多说了,官网上说很很详细。hadoop环境安装入门
我用的hadoop是hadoop-0.20.2,
下面主要讲一下eclipse下的简单使用。
我用的eclipse是最新的3.7,插件不是用hadoop自带的那个,而是google code上的,附件有。
把插件复制到eclipse的plugin目录重启eclipse就可以了,新建项目时多了个Map/Reduce project说明插件已经OK
在Map/Reduce location窗口新建一个location,Map/Reduce Master的端口为9001,DFS Master的端口是9000 ,
和你一开是配置环境是配置的端口要一样。
location name 随便取一个。
这样就可以在eclipse中浏览hadoop上的文件系统了,当然其他操作基本也可以。
下面运行一个简单的例子,新建一个java项目,我这里在hadoop的源代码包里找了个demo:wordcount。
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); } }
运行是要加参数,hdfs://localhost:9000/user/cmzx3444/input hdfs://localhost:9000/user/cmzx3444/output012
运行完后在DFS location里就可以看到输出的文件了。
不知道为什么run on hadoop这个一直没反映,所以只能用上面的方式运行,很是不爽。