/** * Copyright 2006 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/** * This is an example Hadoop Map/Reduce application. It reads the text input * files, breaks each line into words and counts them. The output is a locally * sorted list of words and the count of how often they occurred. * * To run: bin/hadoop jar build/hadoop-examples.jar wordcount [-m maps] * [-r reduces] in-dirout-dir * * @author Owen O'Malley */ publicclass WordCount { /*这个类有三个部分组成: * 1. Mapper Class * 2. Reducer Class * 3. main * 一个典型的Hadoop程序需要至少提供Mapper和Reducer类,可以选择性的提供InputFormat等类 * 若手头已经有hadoop0.1版本,并且配置好了,那么可以用以下命令行来运行wordcount程序 * bin/hadoop jar build/hadoop-examples.jar wordcount inputdir outputdir * */
/** * Counts the words in each line. For each line of input, break the line * into words and emit them as (word, 1). */ // MapReduceBase close configure(JobConf )
/** * A reducer class that just emits the sum of the input values. */ publicstaticclass Reduce extends MapReduceBase implements Reducer { /* * 举例说明Reduce类的作用 * 比如输入是 * 那么是这么调用的 reduce("hello", Iterator , output, reporter) * 输出是 * 这里的Iterator是java.util.Iterator接口。c++里也有类似 * */
/** * The main driver for word count map/reduce program. Invoke this method to * submit the map/reduce job. * * @throws IOException * When there is communication problems with the job tracker. */ publicstaticvoid main(String[] args)throwsIOException{ Configuration defaults =newConfiguration(); //内核中Configuration就是一个java.io.Properties 是一组对,存储hadoop程序的配置
List other_args =newArrayList(); for(int i = 0; i < args.length;++i){ try{ if("-m".equals(args[i])){ conf.setNumMapTasks(Integer.parseInt(args[++i]));// setInt(Configuration)