(1)启动hadoop守护进程
在Terminal中输入如下命令:
$ bin/hadoop namenode -format
$ bin/start-all.sh
(2)在Eclipse上安装Hadoop插件
找到hadoop的安装路径,我的是hadoop-0.20.2,将/home/wenqisun/hadoop-0.20.2/contrib/eclipse-plugin/下的hadoop-0.20.2- eclipse-plugin.jar拷贝到eclipse安装目录下的plugins里,我的是在/home/wenqisun/eclipse /plugins/下。
然后重启eclipse,点击主菜单上的window-->preferences,在左边栏中找到Hadoop Map/Reduce,点击后在右边对话框里设置hadoop的安装路径即主目录,我的是/home/wenqisun/hadoop-0.20.2。
(3)配置Map/Reduce Locations
在Window-->Show View中打开Map/Reduce Locations。
在Map/Reduce Locations中New一个Hadoop Location。
在打开的对话框中配置Location name(为任意的名字)。
配置Map/Reduce Master和DFS Master,这里的Host和Port要和已经配置的mapred-site.xml 和core-site.xml相一致。
一般情况下为
Map/Reduce Master
Host: localhost
Port: 9001
DFS Master
Host: localhost
Port: 9000
配置完成后,点击Finish。如配置成功,在DFS Locations中将显示出新配置的文件夹。
(4)新建项目
创 建一个MapReduce Project,点击eclipse主菜单上的File-->New-->Project,在弹出的对话框中选择Map/Reduce Project,之后输入Project的名,例如Q1,确定即可。然后就可以新建Java类,比如你可以创建一个WordCount 类,然后将你安装的hadoop程序里的WordCount源程序代码(版本不同会有区别),我的是在/home/wenqisun/hadoop-0.20.2/src /examples/org/apache/hadoop/examples/WordCount.java,写到此类中。以下是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(); 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); } }
(3)配置参数
点击Run-->Run Configurations,在弹出的对话框中左边栏选择Java Application,点击右键New,在右边栏中对Arguments进行配置。
在Program arguments中配置输入输出目录参数
/home/wenqisun/in /home/wenqisun/out
这里的路径是文件存储的路径。
在VM arguments中配置VM arguments的参数
-Xms512m -Xmx1024m -XX:MaxPermSize=256m
注意:
in文件夹是需要在程序运行前创建的,out文件夹是不能提前创建的,要由系统自动生成,否则运行时会出现错误。
(4)点击Run运行程序。
程序的运行结果可在out目录下进行查看。
在Console中可以查看到的运行过程为:
12/04/07 06:21:00 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=
12/04/07 06:21:00 WARN mapred.JobClient: No job jar file set. User classes may not be found. See JobConf(Class) or JobConf#setJar(String).
12/04/07 06:21:00 INFO input.FileInputFormat: Total input paths to process : 2
12/04/07 06:21:01 INFO mapred.JobClient: Running job: job_local_0001
12/04/07 06:21:01 INFO input.FileInputFormat: Total input paths to process : 2
12/04/07 06:21:02 INFO mapred.MapTask: io.sort.mb = 100
12/04/07 06:21:30 INFO mapred.MapTask: data buffer = 79691776/99614720
12/04/07 06:21:30 INFO mapred.MapTask: record buffer = 262144/327680
12/04/07 06:21:32 INFO mapred.JobClient: map 0% reduce 0%
12/04/07 06:21:34 INFO mapred.MapTask: Starting flush of map output
12/04/07 06:21:40 INFO mapred.LocalJobRunner:
12/04/07 06:21:40 INFO mapred.MapTask: Finished spill 0
12/04/07 06:21:40 INFO mapred.JobClient: map 100% reduce 0%
12/04/07 06:21:40 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting
12/04/07 06:21:40 INFO mapred.LocalJobRunner:
12/04/07 06:21:40 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000000_0' done.
12/04/07 06:21:44 INFO mapred.MapTask: io.sort.mb = 100
12/04/07 06:22:00 INFO mapred.MapTask: data buffer = 79691776/99614720
12/04/07 06:22:00 INFO mapred.MapTask: record buffer = 262144/327680
12/04/07 06:22:03 INFO mapred.MapTask: Starting flush of map output
12/04/07 06:22:03 INFO mapred.MapTask: Finished spill 0
12/04/07 06:22:03 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000001_0 is done. And is in the process of commiting
12/04/07 06:22:03 INFO mapred.LocalJobRunner:
12/04/07 06:22:03 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000001_0' done.
12/04/07 06:22:04 INFO mapred.LocalJobRunner:
12/04/07 06:22:04 INFO mapred.Merger: Merging 2 sorted segments
12/04/07 06:22:05 INFO mapred.Merger: Down to the last merge-pass, with 2 segments left of total size: 86 bytes
12/04/07 06:22:05 INFO mapred.LocalJobRunner:
12/04/07 06:22:08 INFO mapred.TaskRunner: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting
12/04/07 06:22:08 INFO mapred.LocalJobRunner:
12/04/07 06:22:08 INFO mapred.TaskRunner: Task attempt_local_0001_r_000000_0 is allowed to commit now
12/04/07 06:22:08 INFO output.FileOutputCommitter: Saved output of task 'attempt_local_0001_r_000000_0' to /home/wenqisun/out
12/04/07 06:22:08 INFO mapred.LocalJobRunner: reduce > reduce
12/04/07 06:22:08 INFO mapred.TaskRunner: Task 'attempt_local_0001_r_000000_0' done.
12/04/07 06:22:08 INFO mapred.JobClient: map 100% reduce 100%
12/04/07 06:22:09 INFO mapred.JobClient: Job complete: job_local_0001
12/04/07 06:22:09 INFO mapred.JobClient: Counters: 12
12/04/07 06:22:09 INFO mapred.JobClient: FileSystemCounters
12/04/07 06:22:09 INFO mapred.JobClient: FILE_BYTES_READ=39840
12/04/07 06:22:09 INFO mapred.JobClient: FILE_BYTES_WRITTEN=80973
12/04/07 06:22:09 INFO mapred.JobClient: Map-Reduce Framework
12/04/07 06:22:09 INFO mapred.JobClient: Reduce input groups=5
12/04/07 06:22:09 INFO mapred.JobClient: Combine output records=7
12/04/07 06:22:09 INFO mapred.JobClient: Map input records=4
12/04/07 06:22:09 INFO mapred.JobClient: Reduce shuffle bytes=0
12/04/07 06:22:09 INFO mapred.JobClient: Reduce output records=5
12/04/07 06:22:09 INFO mapred.JobClient: Spilled Records=14
12/04/07 06:22:09 INFO mapred.JobClient: Map output bytes=78
12/04/07 06:22:10 INFO mapred.JobClient: Combine input records=8
12/04/07 06:22:10 INFO mapred.JobClient: Map output records=8
12/04/07 06:22:10 INFO mapred.JobClient: Reduce input records=7