MapReduce 简单实现统计

reduce,对于许多的key我们的分布式进行分发,然后根据我们设计的计算方式得出结果
package com.tem;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;

import com.exercise.hadoop.command.HadoopManipulate;


public class MaxTemperature {
	public static Configuration config;

	static{
		config=new Configuration();
	}
	public static void main(String[] args) throws IOException {
//		if (args.length !=2) {
//			System.err.println("Usage: Maxtemperature <input path> <output path>");
//			System.exit(-1);
//		}
		JobConf conf = new JobConf(MaxTemperature.class);
		conf.setJobName("max temperature");
		FileInputFormat.addInputPath(conf, new Path("/t1/test"));
		FileOutputFormat.setOutputPath(conf, new Path("/z2"));
		conf.setMapperClass(MaxTemperatureMapper.class);
		conf.setReducerClass(MaxTemperatureReducer.class);
		conf.setOutputKeyClass(Text.class);
		conf.setOutputValueClass(Text.class);
		
		JobClient.runJob(conf);
		HadoopManipulate hm = new HadoopManipulate();
		hm.readFile("/z2/part-00000");
	}
}



package com.tem;
import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;

public class MaxTemperatureReducer extends MapReduceBase implements
		Reducer<Text, Text, Text, Text> {
	
	public void reduce(Text key, Iterator<Text> values,
			OutputCollector<Text, Text> output, Reporter reporter)
			throws IOException {
		int maxValue = Integer.MIN_VALUE;
		while (values.hasNext()) {
			maxValue = Math.max(maxValue, Integer.parseInt(values.next().toString()));
		}
		output.collect(key, new Text(String.valueOf(maxValue)));
	}


}

Mapper,作为hadoop强大的统计工具,讲我们大数据在文件中按照我们规定的格式写出后,对key进行查重合并传给reduce

package com.tem;
import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

public class MaxTemperatureMapper extends MapReduceBase implements
		Mapper<LongWritable, Text, Text, Text> {


	/**
	 * @param args
	 */

	public void map(LongWritable key, Text value,
			OutputCollector<Text, Text> output, Reporter reporter)
			throws IOException {
		String line =value.toString();
		if(line.split(" ").length==2)
		{
		String first = line.split(" ")[0];
		String second=line.split(" ")[1];
		output.collect(new Text(first), new Text(second));
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值