hadoop mapper reducer sample demo

package com.traveller.bumble.hadoop.mr.temperature;

import com.traveller.bumble.hadoop.mr.wordcount.WordCountMapper;
import com.traveller.bumble.hadoop.mr.wordcount.WordCountReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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;

import java.io.File;
import java.io.IOException;

/**
 * Created by macbook on 2017/5/23.
 */
public class TemperatureApp {

    public static void main(String[] args) {


        try {

            String srcPath = args[0];
            String destPath = args[1];
            String isLocal = args[2];
            boolean flag = false;
            //本地模式
            if (isLocal.equals("1")) {
                File file = new File(destPath);
                flag = deleteDir(file);
                //集群模式
            } else {

                flag = deletePath(new Path(destPath));

            }


            System.out.println(flag);
            Job job = Job.getInstance();
            job.setJobName("temperature");
            job.setJarByClass(TemperatureApp.class);

            job.setNumReduceTasks(1);

            FileInputFormat.addInputPath(job, new Path(srcPath));
            FileOutputFormat.setOutputPath(job, new Path(destPath));
            job.setMapperClass(TemperatureMapper.class);
            job.setReducerClass(TemperatureReducer.class);

            job.setOutputKeyClass(IntWritable.class);
            job.setMapOutputValueClass(IntWritable.class);
            job.waitForCompletion(true);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    private static boolean deletePath(Path path) {
        try {
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(conf);
            boolean flag = fs.deleteOnExit(path);
            return flag;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
    }
}

 

 

mapper

package com.traveller.bumble.hadoop.mr.temperature;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * Created by macbook on 2017/5/24.
 */
public class TemperatureMapper extends Mapper<LongWritable,Text,IntWritable,IntWritable> {

    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String [] strs = value.toString().split(",");
        IntWritable year = new IntWritable(Integer.valueOf(strs[0]));
        IntWritable temprature = new IntWritable(Integer.valueOf(strs[1]));
        context.write(year,temprature);

    }
}

reducer

package com.traveller.bumble.hadoop.mr.temperature;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * Created by macbook on 2017/5/24.
 */
public class TemperatureReducer extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable> {

    protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {


        Iterator<IntWritable> iterators = values.iterator();
        List<Integer> temp = new ArrayList<>();
        while (iterators.hasNext()){
            temp.add(iterators.next().get());
        }
        Integer max = Collections.max(temp);
        context.write(key,new IntWritable(max));
    }
}

 

转载于:https://my.oschina.net/heinrichchen/blog/907959

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值