Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2173818
  • 博文数量: 436
  • 博客积分: 9833
  • 博客等级: 中将
  • 技术积分: 5558
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-29 10:27
文章存档

2013年(47)

2012年(79)

2011年(192)

2010年(118)

分类: LINUX

2011-09-25 23:57:20

这周主要进行了hadoop的简单运行调试及使用,并研究了hadoop集群运行测试代码中的天气数据分析。
 
以下是其中的部分代码:
public class MaxTemperature {

    static class MaxTemperatureMapper extends Mapper 
    {
        private static final int MISSING = 9999;
        
        public void map(LongWritable key, Text value, Context conext) throws IOException, InterruptedException
        {
            String line = value.toString();
            String year = line.substring(5, 9); 
            int airTemperature = Integer.parseInt(line.substring(15, 19)); 
            if(airTemperature != MISSING)
            {
                conext.write(new Text(year), new IntWritable(airTemperature));
            }
            
        }
        
    }
    
    static class MaxTemperatureReducer extends Reducer
    {
        public void reduce(Text key, Iterable 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) {
        // TODO Auto-generated method stub
        
        if(args.length != 2)
        {
            System.err.println("Usage: MaxTemperature ");
            System.exit(-1);
        }
         
        try {
            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);
            
            System.exit(job.waitForCompletion(true) ? 0 : 1);
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(ClassNotFoundException e)
        {
            e.printStackTrace();
        }catch(InterruptedException e)
        {
            e.printStackTrace();
        }
        

    }

 

这段代码主要叙述了Map-Reduce函数和调度函数(Job),并为天气运算示例做好了算法铺垫。其中代码运行的效率经过测试比不用MapReduce提升了30%(由于linux的部分配置还有问题)。这个代码虽然和我们平常用的代码思路不太相同,但是其效率的提升是非常明显的。

下周以及十一期间,我们将重点研究Hadoop在分布式中的应用方法,并组件小型云计算网络并进行测试。



}
阅读(4535) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

zhao68256512011-09-30 09:36:01

2011-09-28 19:46:56

http://www.bags2world.com/