簡體   English   中英

使用Hadoop Map Reduce執行多種計算

[英]Performing multiple computations with Hadoop Map Reduce

我有一個map reduce程序,用於查找每年2個單獨屬性的最小值/最大值。 在大多數情況下,這可以在hadoop中使用單個節點集群。 這是我當前的設置:

public class MaxTemperatureReducer extends
        Reducer<Text, Stats, Text, Stats> {

    private Stats result = new Stats();

    @Override
    public void reduce(Text key, Iterable<Stats> values, Context context)
            throws IOException, InterruptedException {

        int maxValue = Integer.MIN_VALUE;
        int minValue = Integer.MAX_VALUE;
        int sum = 0;

        for (Stats value : values) {
            result.setMaxTemp(Math.max(maxValue, value.getMaxTemp()));
            result.setMinTemp(Math.min(minValue, value.getMinTemp()));
            result.setMaxWind(Math.max(maxValue, value.getMaxWind()));
            result.setMinWind(Math.min(minValue, value.getMinWind()));

            sum += value.getCount();
        }

        result.setCount(sum);

        context.write(key, result);
    }
}

public class MaxTemperatureMapper extends
        Mapper<Object, Text, Text, Stats> {

    private static final int MISSING = 9999;
    private Stats outStat = new Stats();

    @Override
    public void map(Object key, Text value, Context context)
            throws IOException, InterruptedException {

        String[] split = value.toString().split("\\s+");
        String year = split[2].substring(0, 4);
        int airTemperature;
        airTemperature = (int) Float.parseFloat(split[3]);

        outStat.setMinTemp((float)airTemperature);
        outStat.setMaxTemp((float)airTemperature);

        outStat.setMinWind(Float.parseFloat(split[12]));
        outStat.setMaxWind(Float.parseFloat(split[14]));
        outStat.setCount(1);

        context.write(new Text(year), outStat);
    }
}

public class MaxTemperatureDriver extends Configured implements Tool {
    public int run(String[] args) throws Exception {

        if (args.length != 2) {
            System.err
                    .println("Usage: MaxTemperatureDriver <input path> <outputpath>");
            System.exit(-1);
        }

        Job job = new Job();
        job.setJarByClass(MaxTemperatureDriver.class);
        job.setJobName("Max Temperature");

        job.setMapperClass(MaxTemperatureMapper.class);
        job.setCombinerClass(MaxTemperatureReducer.class);
        job.setReducerClass(MaxTemperatureReducer.class);



        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Stats.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
        boolean success = job.waitForCompletion(true);
        return success ? 0 : 1;
    }

    public static void main(String[] args) throws Exception {
        MaxTemperatureDriver driver = new MaxTemperatureDriver();
        int exitCode = ToolRunner.run(driver, args);
        System.exit(exitCode);

    }
 }

目前,它僅打印每年的溫度和風速的最小值/最大值。 我確信這是一個簡單的實現,但是在任何地方都找不到答案。 我想嘗試找出每年最高的5分鍾/最大值。 有什么建議么?

讓我假設您的Stats類具有以下簽名。

/* the stats class need to be a writable, the below is just a demo*/
public class Stats {

public float getTemp() {
    return temp;
}
public void setTemp(float temp) {
    this.temp = temp;
}
public float getWind() {
    return wind;
}
public void setWind(float wind) {
    this.wind = wind;
}
private float temp;
private float wind;
}

這樣,讓我們​​如下更改減速器。

SortedSet<Float> tempSetMax = new TreeSet<Float>();
        SortedSet<Float> tempSetMin = new TreeSet<Float>();
        SortedSet<Float> windSetMin = new TreeSet<Float>();
        SortedSet<Float> windSetMax = new TreeSet<Float>();
        List<Stats> values = new ArrayList<Float>();
        for (Stats value : values) {

            float temp = value.getTemp();
            float wind = value.getWind();

            if (tempSetMax.size() < 5) {
                tempSetMax.add(temp);
            } else {
                float currentMinValue = tempSetMax.first();
                if (temp > currentMinValue) {
                    tempSetMax.remove(currentMinValue);
                    tempSetMax.add(temp);
                }
            }
            if (tempSetMin.size() < 5) {
                tempSetMin.add(temp);
            } else {
                float currentMaxValue = tempSetMin.last();
                if (temp < currentMaxValue) {
                    tempSetMax.remove(currentMaxValue);
                    tempSetMax.add(temp);
                }
            }

            if (windSetMin.size() < 5) {
                windSetMin.add(wind);
            } else {

                float currentMaxValue = windSetMin.last();
                if (wind < currentMaxValue) {
                    windSetMin.remove(currentMaxValue);
                    windSetMin.add(temp);
                }

            }
            if (windSetMax.size() < 5) {
                windSetMax.add(wind);
            } else {

                float currentMinValue = windSetMax.first();
                if (wind > currentMinValue) {
                    windSetMax.remove(currentMinValue);
                    windSetMax.add(temp);
                }

            }
        }

現在,您可以將每個列表的toString()寫入上下文,或者可以創建自定義可寫代碼。 在我的代碼中,請根據您的要求更改Stats 它必須是Writable 上面僅用於演示示例流程。

是MR Design Patterns Book中獲得前10名的代碼。在同一GitHub位置中,還有其他MR設計模式的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM