eclipse运行hadoop wordcount example

eclipse 3.6.2

hadoop-0.20.2

(1)安装eclipse

(2)设置eclipse plugins
(3)设置环境变量
HADOOP4WIN_HOME:D:\hadoop
Path:D:\Program Files\cygwin\bin;D:\hadoop\bin;
(4)eclipse->windows->preferences->hadoop map/reduce 填写hadoop安装路径
(5)执行wordcount example

脚本在\hadoop\src\examples\org\apache\hadoop\examples下

//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 {

  /*
   * 通过扩展Mapper实现内部类TokenizerMapper
   */
  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{
    
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
    /*
     * 重载map方法(non-Javadoc)
     * @see org.apache.hadoop.mapreduce.Mapper#map(KEYIN, VALUEIN, org.apache.hadoop.mapreduce.Mapper.Context)
     */
    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);//写入处理的中间结果<key,value>
      }
    }
  }
 
  /*
   * 通过扩展Reducer实现内部类IntSumReducer
   */
  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    /*
     * 重载reduce方法(non-Javadoc)
     * @see org.apache.hadoop.mapreduce.Reducer#reduce(KEYIN, java.lang.Iterable, org.apache.hadoop.mapreduce.Reducer.Context)
     */
    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
    job.setJarByClass(WordCount.class);//设定执行类
    job.setMapperClass(TokenizerMapper.class);//设定Mapper实现类
    job.setCombinerClass(IntSumReducer.class);//设定Combiner实现类
    job.setReducerClass(IntSumReducer.class);//设定Reducer实现类
    job.setOutputKeyClass(Text.class);//设定OutputKey实现类,Text.class是默认实现
    job.setOutputValueClass(IntWritable.class);//设定OutputValue实现类
    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//设定job输入文件夹
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//设定job输出文件夹
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

11/07/11 12:14:57 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=
Exception in thread "main" org.apache.hadoop.mapreduce.lib.input.InvalidInputException: Input Pattern file:/user/pengxuan.lipx/test-in/test*.txt matches 0 files
    at org.apache.hadoop.mapreduce.lib.input.FileInputFormat.listStatus(FileInputFormat.java:224)
    at org.apache.hadoop.mapreduce.lib.input.FileInputFormat.getSplits(FileInputFormat.java:241)
    at org.apache.hadoop.mapred.JobClient.writeNewSplits(JobClient.java:885)
    at org.apache.hadoop.mapred.JobClient.submitJobInternal(JobClient.java:779)
    at org.apache.hadoop.mapreduce.Job.submit(Job.java:432)
    at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:447)
    at WordCount.main(WordCount.java:67)

传入参数错误,经过修改:

eclipse->run->run configurations->Java Application -> WordCount -> arguments

hdfs://localhost:9000/user/pengxuan.lipx/test-in/test*.txt  out

run:
11/07/11 12:16:37 INFO jvm.JvmMetrics: Initializing JVM Metrics with processName=JobTracker, sessionId=
11/07/11 12:16:37 INFO input.FileInputFormat: Total input paths to process : 2
11/07/11 12:16:37 INFO mapred.JobClient: Running job: job_local_0001
11/07/11 12:16:37 INFO input.FileInputFormat: Total input paths to process : 2
11/07/11 12:16:38 INFO mapred.MapTask: io.sort.mb = 100
11/07/11 12:16:38 INFO mapred.MapTask: data buffer = 79691776/99614720
11/07/11 12:16:38 INFO mapred.MapTask: record buffer = 262144/327680
11/07/11 12:16:38 INFO mapred.MapTask: Starting flush of map output
11/07/11 12:16:38 INFO mapred.MapTask: Finished spill 0
11/07/11 12:16:38 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000000_0 is done. And is in the process of commiting
11/07/11 12:16:38 INFO mapred.LocalJobRunner:
11/07/11 12:16:38 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000000_0' done.
11/07/11 12:16:38 INFO mapred.MapTask: io.sort.mb = 100
11/07/11 12:16:38 INFO mapred.MapTask: data buffer = 79691776/99614720
11/07/11 12:16:38 INFO mapred.MapTask: record buffer = 262144/327680
11/07/11 12:16:38 INFO mapred.MapTask: Starting flush of map output
11/07/11 12:16:38 INFO mapred.MapTask: Finished spill 0
11/07/11 12:16:38 INFO mapred.TaskRunner: Task:attempt_local_0001_m_000001_0 is done. And is in the process of commiting
11/07/11 12:16:38 INFO mapred.LocalJobRunner:
11/07/11 12:16:38 INFO mapred.TaskRunner: Task 'attempt_local_0001_m_000001_0' done.
11/07/11 12:16:38 INFO mapred.LocalJobRunner:
11/07/11 12:16:38 INFO mapred.Merger: Merging 2 sorted segments
11/07/11 12:16:38 INFO mapred.Merger: Down to the last merge-pass, with 2 segments left of total size: 78 bytes
11/07/11 12:16:38 INFO mapred.LocalJobRunner:
11/07/11 12:16:38 INFO mapred.TaskRunner: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting
11/07/11 12:16:38 INFO mapred.LocalJobRunner:
11/07/11 12:16:38 INFO mapred.TaskRunner: Task attempt_local_0001_r_000000_0 is allowed to commit now
11/07/11 12:16:38 INFO output.FileOutputCommitter: Saved output of task 'attempt_local_0001_r_000000_0' to out1
11/07/11 12:16:38 INFO mapred.LocalJobRunner: reduce > reduce
11/07/11 12:16:38 INFO mapred.TaskRunner: Task 'attempt_local_0001_r_000000_0' done.
11/07/11 12:16:38 INFO mapred.JobClient:  map 100% reduce 100%
11/07/11 12:16:38 INFO mapred.JobClient: Job complete: job_local_0001
11/07/11 12:16:38 INFO mapred.JobClient: Counters: 13
11/07/11 12:16:38 INFO mapred.JobClient:   FileSystemCounters
11/07/11 12:16:38 INFO mapred.JobClient:     FILE_BYTES_READ=70064
11/07/11 12:16:38 INFO mapred.JobClient:     HDFS_BYTES_READ=95
11/07/11 12:16:38 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=126319
11/07/11 12:16:38 INFO mapred.JobClient:   Map-Reduce Framework
11/07/11 12:16:38 INFO mapred.JobClient:     Reduce input groups=3
11/07/11 12:16:38 INFO mapred.JobClient:     Combine output records=6
11/07/11 12:16:38 INFO mapred.JobClient:     Map input records=2
11/07/11 12:16:38 INFO mapred.JobClient:     Reduce shuffle bytes=0
11/07/11 12:16:38 INFO mapred.JobClient:     Reduce output records=3
11/07/11 12:16:38 INFO mapred.JobClient:     Spilled Records=12
11/07/11 12:16:38 INFO mapred.JobClient:     Map output bytes=62
11/07/11 12:16:38 INFO mapred.JobClient:     Combine input records=6
11/07/11 12:16:38 INFO mapred.JobClient:     Map output records=6
11/07/11 12:16:38 INFO mapred.JobClient:     Reduce input records=6

你可能感兴趣的:(eclipse,hadoop,exception,buffer,input,output)