第一个MapReduce程序

package lxkj.com.hadoop_02;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;;
//Mapper类是一个泛型类参数分别指(输入键【长整数偏移量】,输入值【一行文本】,输出键,输出值) 是一套可优化网络序列化传输的基本类型,这类类型都在org.apache.hadoop.io包中
public class MaxTemperatureMapper extends  Mapper<LongWritable, Text, Text, IntWritable> {
    private static final int MISSING=9999;
 public void map(LongWritable key, Text value, Context context )
   throws IOException, InterruptedException {
  //将输入的Text值转换为java的String类型
  String line=value.toString();
  //用substring()方法提取我们感兴趣的列
  String year=line.substring(15, 19);
  int airTemperature;
  if(line.charAt(87)=='+'){
    airTemperature=Integer.parseInt(line.substring(88, 92));   
  }else{
     airTemperature=Integer.parseInt(line.substring(87, 92)); 
  }
  String quality=line.substring(92, 93);
  if(airTemperature!=MISSING&&quality.matches("[01459]")){
   //输出写入内容
   context.write(new Text(year), new IntWritable(airTemperature));
  
  }
 }
 

}
package lxkj.com.hadoop_02;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
//同样reduce团也有4个参数指输入和输出类型,reduce的输入类型必须是map的输出类型
public class MaxTemperatureReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
   //Iterable<> 相当于List
 @Override
 protected void reduce(Text key, Iterable<IntWritable> values,
   Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
    int maxValue=Integer.MIN_VALUE;
    for(IntWritable value:values){
       maxValue=Math.max(maxValue, value.get());
    }
    //输出类型必须是hadoop自带的类型
    context.write(key, new IntWritable(maxValue));
 }
   
}

package lxkj.com.hadoop_02;
import java.io.IOException;
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.lib.input.FileInputFormat;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args ) throws IOException, ClassNotFoundException, InterruptedException
    {
     if(args.length!=2){
      System.out.println("Usage:MaxTempleature <input path> <ouput path>" );
      System.exit(-1);
     }
     //job对象指定作业额规范,可以控制整个作业的运行
     Job job=new Job();
     //在hadoop集群上作业时,要把代码打成一个Jar、文件,不必明确指定jar文件的名字,在job对象的setjarByClass()方法中传递一个类即可
       job.setJarByClass(App.class);
       //给作业起一个名字
       job.setJobName("Max templature");
       //输入数据路径
       FileInputFormat.addInputPath(job, new Path(args[0]));
       //输出数据的路径,只能有一个输出路径
       FileInputFormat.addInputPath(job, new Path(args[1]));
       //只能mapper类型和reduce类型
       job.setMapperClass(MaxTemperatureMapper.class);
       job.setReducerClass(MaxTemperatureReduce.class);
       //reduce函数的输出类型
       job.setOutputKeyClass(Text.class);
      
       job.setOutputValueClass(IntWritable.class);
       System.exit(job.waitForCompletion(true)?0:1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

飞腾创客

你的鼓励是我最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值