【云计算】MapReduce Commandline Coding

【编写Java代码】

WordCount.java

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{
    //把进入reduce的value都设置成1
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();
    //进入map端的数据,每次进入一行。
    //MapReduce都是具有一定结构的数据,有一定含义的数据。
    
    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);
      }
  //注意:在map或reduce上面的打印语句是没有办法输出的,但会记录到日志文件当中。
    }
  }
  
  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 {
    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 job = Job.getInstance(conf, "word count");
   //设置该作业所要执行的类
    job.setJarByClass(WordCount.class);
   //设置自定义的Mapper类以及Map端数据输出时的类型
    job.setMapperClass(TokenizerMapper.class);
    //job.setMapOutputKeyClass(Text.class);
    //job.setMapOutputValueClass(IntWritable.class);
    job.setCombinerClass(IntSumReducer.class);
  //设置自定义的Reducer类以及输出时的类型
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    for (int i = 0; i < otherArgs.length - 1; ++i) {
      FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
    }
    FileOutputFormat.setOutputPath(job,
      new Path(otherArgs[otherArgs.length - 1]));
//提交作业
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

【编译】

:javac WordCount.java

报错如下: 是应为环境变量缺少相应的依赖。

error: package org.apache.hadoop.conf does not exist
import org.apache.hadoop.conf.Configuration;

尝试修改环境变量CLASSPATH

sudo vim /etc/profile
# 添加如下内容
export HADOOP_HOME=/usr/local/hadoop    # 如果没设置的话, 路径是hadoop安装目录
export PATH=$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$PATH    # 如果没设置的话
export CLASSPATH=$($HADOOP_HOME/bin/hadoop classpath):$CLASSPATH

source /etc/profile

{  在尝试修改环境变量CLASSPATH 还进行了一下尝试:

【云计算】MapReduce Commandline Coding_第1张图片

但尝试失败,做个记录 }

再次编译: javac WordCount.java -d .         -d 指定生成目录

【云计算】MapReduce Commandline Coding_第2张图片

查看jar包内容:1.通过vim   2.jar -tvf WordCount.jar 

【云计算】MapReduce Commandline Coding_第3张图片

【运行】

运行hadoop jar WordCount.jar WordCount ../test/input/ ../test/output/    出现错误,我百度,WordCount 路径不对

【云计算】MapReduce Commandline Coding_第4张图片

运行hadoop jar WordCount.jar org.apache.hadoop.examples.WordCount ../test/input/ ../test/output/  运行成功  

【云计算】MapReduce Commandline Coding_第5张图片 【云计算】MapReduce Commandline Coding_第6张图片

你可能感兴趣的:(云计算)