【问题标题】:MaxTemperature example for MapReduce on HadoopHadoop 上 MapReduce 的 MaxTemperature 示例
【发布时间】:2014-11-02 12:03:29
【问题描述】:

我正在尝试从 MapReduce 运行 MaxTemperature 示例。但我在 Hadoop MapReduce 示例中找不到 MaxTemperature.jar。谁能帮我找出 jar 文件或者执行这个程序并查看输出的可能性是什么?

【问题讨论】:

标签: hadoop mapreduce


【解决方案1】:

试试这个,

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;






public class Temp {

  public static class Map extends  Mapper<LongWritable, Text, Text, IntWritable> {
   // private final static IntWritable one = new IntWritable();
  //  private Text word = new Text();

    public void map(LongWritable key, Text value, Context context) throws IOException  InterruptedException
{
      String line = value.toString();
      String year=line.substring(0,4);
      //StringTokenizer tokenizer = new StringTokenizer(line);
     // while (tokenizer.hasMoreTokens()) {
      // word.set(tokenizer.nextToken());
       // output.collect(word, one);
      int Temp=Integer.parseInt(line.substring(6,8));
      context.write(new Text(year),new IntWritable(Temp));
      }
    }
  }
/*
  public static class Reduce extends  Reducer<Text, IntWritable, Text, IntWritable> {
    public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
     // int max=Integer.MIN_VALUE;
        int sum=0;
      while (values.hasNext()) {
          sum += values.next().get();
      }
      output.collect(key, new IntWritable(sum));
    }
  }*/
public class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> 
{
@Override
    public void reduce(Text key, Iterable<IntWritable> values,Context context)
    throws IOException, InterruptedException 
    {

        int maxValue = Integer.MIN_VALUE;
           for (IntWritable value : values) {
            maxValue = Math.max(maxValue, value.get());
     }
             context.write(key, new IntWritable(maxValue));
      }
}

  public static void main(String[] args) throws Exception {
    JobConf conf = new JobConf(Temp.class);
    conf.setJobName("Temp");

    conf.setOutputKeyClass(Text.class);
    conf.setOutputValueClass(IntWritable.class);

    conf.setMapperClass(Map.class);
    conf.setCombinerClass(Reduce.class);
    conf.setReducerClass(Reduce.class);

    conf.setInputFormat(TextInputFormat.class);
    conf.setOutputFormat(TextOutputFormat.class);

    FileInputFormat.setInputPaths(conf, new Path(args[0]));
    FileOutputFormat.setOutputPath(conf, new Path(args[1]));

    JobClient.runJob(conf);
  }
}

制作这个程序的jar文件并执行命令

hadoop jar Temp.jar Temp /hdfs_inputFile /hdfs_inputFile 

【讨论】:

  • 对不起,我有源代码,但我想知道我可以设置什么类路径来编译这个程序以制作 jar
猜你喜欢
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 2010-12-30
  • 2014-03-25
  • 2017-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多