0
点赞
收藏
分享

微信扫一扫

yarn命令调用mrwordcount

如何使用yarn命令调用mrwordcount

作为一名经验丰富的开发者,我可以教你如何使用yarn命令调用mrwordcount。在开始之前,让我先向你介绍整个流程,并提供每个步骤所需的代码。

整体流程如下所示:

  1. 准备Hadoop环境
  2. 编写MapReduce程序
  3. 打包程序
  4. 将程序上传到Hadoop集群
  5. 使用yarn命令调用mrwordcount

现在让我们开始逐步介绍每个步骤。

  1. 准备Hadoop环境

首先,你需要确保已经搭建好了Hadoop环境。如果还没有,可以参考Hadoop官方文档进行安装和配置。

  1. 编写MapReduce程序

接下来,你需要编写一个简单的MapReduce程序,用于统计单词个数。以下是一个示例代码:

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;

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();
    Job job = Job.getInstance(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(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

以上代码中,我们定义了一个TokenizerMapper类和一个IntSumReducer类,用于实现Map和Reduce任务。在main方法中,我们配置了作业的输入和输出路径,并指定了Mapper和Reducer的实现类。

  1. 打包程序

在编写完MapReduce程序后,你需要将其打包成一个可执行的JAR文件。你可以使用以下命令将代码编译成一个JAR文件:

$ javac -classpath $HADOOP_HOME/share/hadoop/common/hadoop-common-3.2.1.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-3.2.1.jar:$HADOOP_HOME/share/hadoop/common/lib/commons-cli-1.2.jar -d WordCount/ WordCount.java
$ jar -cvf WordCount.jar -C WordCount/ .

以上命令将生成一个名为WordCount.jar的JAR文件。

  1. 将程序上传到Hadoop集群

接下来,你需要将打包好的JAR文件上传到Hadoop集群的某个节点上。你可以使用以下命令将JAR文件上传到Hadoop集群的/user/<username>目录下:

$ hdfs dfs -mkdir -p /user/<username>/wordcount/input
$ hdfs dfs -put input.txt /user/<username>/wordcount/input
$ hdfs dfs -mkdir /user/<username>/wordcount/output
$ hdfs dfs -chmod -R 777 /user/<username>/wordcount
$ hdfs dfs -put WordCount.jar /user/<username>/wordcount

以上命令将创建一个input文件夹,并将输入文件input.txt上传到该文件

举报

相关推荐

0 条评论