当前位置:首页 > 开发 > 开源软件 > 正文

Hadoop 查找最大数

发表于: 2012-08-15   作者:elan1986   来源:转载   浏览次数:
摘要: package com.lch.find; import java.io.IOException; import java.util.Iterator; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.FloatWr
package com.lch.find;

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.FloatWritable;
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;
import org.apache.hadoop.util.GenericOptionsParser;

public class SearchMaxValue {

	public static class SearchMaxValueMapper extends Mapper<LongWritable, Text, IntWritable, FloatWritable>{
		private final static IntWritable one = new IntWritable(1);

		@Override
		protected void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			String str = value.toString().trim();
			float flo = Float.parseFloat(str);
			context.write(one, new FloatWritable(flo));
		}
	}
	
	public static class SearchMaxValueReducer extends Reducer<IntWritable, FloatWritable, Text, FloatWritable>{
		@SuppressWarnings("rawtypes")
		public void reduce(IntWritable key, Iterable<FloatWritable> values, Context context) throws IOException, InterruptedException{
			Iterator it = values.iterator();
			float maxFloat = 0, tmp;
			if(it.hasNext()){
				maxFloat = ((FloatWritable) it.next()).get();
			}else{
				context.write(new Text("Max Value : "), null);
				return;
			}
			while(it.hasNext()){
				tmp = ((FloatWritable) it.next()).get();
				if(tmp > maxFloat){
					maxFloat = tmp;
				}
			}
			context.write(new Text("Max value is :"), new FloatWritable(maxFloat));
		}
	}
	
	public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
		Configuration conf = new Configuration();
		String[] paths = { "hdfs://192.168.1.136:9000/tmp/test.txt", "hdfs://192.168.1.136:9000/tmp/outputs" };
		String[] otherArgs = new GenericOptionsParser(conf, paths).getRemainingArgs();
		
		Job job = new Job(conf, "SearchValue");
		job.setJarByClass(SearchMaxValue.class);
		job.setMapperClass(SearchMaxValueMapper.class);
		job.setReducerClass(SearchMaxValueReducer.class);
		
		job.setMapOutputKeyClass(IntWritable.class);
		job.setMapOutputValueClass(FloatWritable.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(FloatWritable.class);
		
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
		
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}

代码比较简单 不多解释
附上数据信息
12.5
85.0
236
239
23
2
0
1
9
236
232.0

Hadoop 查找最大数

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
1 查找
查找概述 l查找——也叫检索,是根据给定的某个值,在表中确定一个关键字等于给定值的记录或数据元
问题背景 本次进行一个项目的重构,在某些活动数据量比较大的情况下,会偶尔出现1200s超时的情况,
本次进行一个项目的重构,在某些活动数据量比较大的情况下,会偶尔出现1200s超时的情况,如下: Att
问题背景 本次进行一个项目的重构,在某些活动数据量比较大的情况下,会偶尔出现1200s超时的情况,
http://codevs.cn/problem/1201/ 1201 最小数和最大数 时间限制: 1 s 空间限制: 128000 KB 题目等级
6 Hadoop
《Hadoop: The Definitive Guide》reading notes: This, in a nutshell, is what Hadoop provides:
7 Hadoop
Hadoop学习笔记一 简要介绍 这里先大致介绍一下Hadoop. 本文大部分内容都是从官网Hadoop上来的。其
8 Hadoop
Hadoop一个分布式系统基础架构,由Apache基金会开发。用户可以在不了解分布式底层细节的情况下,开发
9 Hadoop
《Hadoop: The Definitive Guide》reading notes: This, in a nutshell, is what Hadoop provides:
10 Hadoop
Hadoop 求助编辑百科名片 Hadoop示意图 <div style="overflow: hidde
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号