【云计算】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<Object, Text, Text, IntWritable>{
    //把进入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<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> [<in>...] <out>");
      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 还进行了一下尝试:

但尝试失败,做个记录 }

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

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

【运行】

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路途…

点滴记录

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值