java - 如何通过servlet提交WordCount.jar到hadoop

标签 java hadoop servlets

我现在有一个WordCount.jar存储在linux本地文件系统中,并且一个文件包含一组存储在HDFS中的单词。
如何通过servlet运行WordCount.jar并在servlet中指定输入和输出路径。

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

    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();
    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.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);
    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);
  }
}

最佳答案

通常遵循以下步骤

  • 将您的wordcount类jar文件以及必要的hadoop客户端jar文件放入服务器类路径
  • 将输入和输出目录指定为http请求参数
  • 在servlet doGet()方法
  • 中解析来自http请求的目录
  • 使用JobClient提交您的工作
  • 关于java - 如何通过servlet提交WordCount.jar到hadoop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42642163/

    相关文章:

    java - 使用 Stream API 的对象的 Sum 属性

    hadoop - 如何在Windows7的Cygwin中安装sqoop?

    jsp - 如何在第一个 JSP servlet 中调用第二个 JSP servlet?

    java - 外部存储并不指大容量内部 SD 卡

    java - 使用 Java Stream 将数字字符串解析为 Integer 对象列表

    java - 待办事项列表示例对我不起作用

    hadoop - hadoop连接被外部请求拒绝

    hadoop - Hadoop 集群上的 Hive/Map-Reduce 作业 : How to (roughly) calculate the diskspace needed?

    eclipse - 如何在 Eclipse 中为 Servlet API 添加 Javadoc

    java - 带计时器的 ServletContextListener 未在控制台中显示输出