第一个mapreduce

hadoop环境搭好了,那么就试着写第一个mapreduce吧,以<hadoop: the definitive guide>中按年统计最高温度为例。温度文件的具体格式参考该书目。
原作者提供了温度文件的sample.txt,有5行记录作为测试,上传服务器。
Code和书中基本一致,书中的例子使用的早期的JobClient API,这里改为Job,

1. 建立项目
    在eclipse中新建一个java project,引入hadoop -core.jar.
    建3个类mapper:     MaxTemperatureMapper,reducer: MaxTemperatureReducer,主程序: MaxTemperature,然后打成jar包,上传到hadoop机器上即可。
    在hadoop上运行:
        hadoop jar ~/jars/maxTemperature.jar MaxTemperature ~/data/test/sample.txt output
 

附录:
    
MaxTemperatureMapper.java
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;
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{
            String line=value.toString();
            String year=line.substring(15,19);
            int temperature;
            if(line.charAt(87)=='+'){
                temperature=Integer.parseInt(line.substring(87,92));
            }else {
                temperature=Integer.parseInt(line.substring(87,92));
            }
            String quality=line.substring(92,93);
            //正则匹配
            if(temperature!=MISSING && quality.matches("[01459]")){
                context.write(new Text(year), new IntWritable(temperature));
            }
    }
}
MaxTemperatureReducer.java 
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class MaxTemperatureReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
    public void reduce(Text key,Iterable<IntWritable> values,Context context)
        throws IOException,InterruptedException{
        int maxTemp=Integer.MIN_VALUE;
        for(IntWritable temp:values){
            maxTemp=Math.max(maxTemp, temp.get());
        }
        context.write(key, new IntWritable(maxTemp));
    }
}
 
MaxTemperature.java
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;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
 
public class MaxTemperature {
    public static void main(String[] args) throws Exception{
        
        if(args.length!=2){
            System.err.println("usage: MaxTemperature <input path> <output path>");
            System.exit(-1);
        }         
        long begainTime=System.currentTimeMillis();        
        Job job=new Job();
        job.setJarByClass(MaxTemperature.class);         
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath( job, new Path(args[1]));         
        job.setMapperClass(MaxTemperatureMapper.class);
        job.setReducerClass(MaxTemperatureReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        boolean status=job.waitForCompletion(true);
        
        System.out.printf("runing time(ms) : %d",System.currentTimeMillis()-begainTime);
        System.exit(status?0:1);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是一个使用 MapReduce 统计单词频率的示例程序: ```java 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.LongWritable; 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<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable 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); } } ``` 该程序包含两个 MapReduce 阶段,第一个 MapReduce 阶段是 Mapper,它将输入数据中的每个单词作为键,值设置为 1。Reducer 将相同键的值相加,并将结果输出为键值对。该程序还使用了 Combiner,这是一个可选的优化步骤,它会在 Map 阶段之后,在每个 Map 节点上对中间结果进行一次局部合并,以减少数据传输量和计算量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值