• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
导航菜单
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx
您的位置:首页 > 服务器 >云计算 > MapReduce处理数据平均值与数值大小排行比较,mapreduce平均值

MapReduce处理数据平均值与数值大小排行比较,mapreduce平均值

作者:网友 字体:[增加 减小] 来源:互联网

本文主要包含mapreduce过滤数据,mapreduce数据流图,mapreduce数据倾斜,mapreduce数据清洗,mapreduce数据去重等服务器相关知识,网友希望可以进行参考

MapReduce处理数据平均值与数值大小排行比较,mapreduce平均值


一:计算数据平均值

在map中将名称作为key 数据为value写出去

/*
 * 计算平均成绩
 * 名字作为key	分数值为value写出去
 */
public class AverageMap extends Mapper<LongWritable, Text, Text, IntWritable> {
	protected void map(
			LongWritable key,
			Text value,
			org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, IntWritable>.Context context)
			throws java.io.IOException, InterruptedException {
		String line = value.toString();
		if (line.trim().length() > 0) {
			String[] str = line.split("\t");
			if (str.length == 2) {
				context.write(new Text(str[0]),
						new IntWritable(Integer.valueOf(str[1])));
			}
		}

	};
}


 

public class AverageRedu extends
		Reducer<Text, IntWritable, Text, DoubleWritable> {
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,
			Reducer<Text, IntWritable, Text, DoubleWritable>.Context context)
			throws IOException, InterruptedException {
		int sum = 0;
		for (IntWritable value : values) {
			sum += value.get();
		}
		context.write(key, new DoubleWritable(sum / 2.0));
	}

}


 

public class AverageMain {
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = new Job(conf);
		job.setJarByClass(AverageMain.class);

		job.setMapperClass(AverageMap.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);

		job.setReducerClass(AverageRedu.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(DoubleWritable.class);

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


 

二:求最大最小值

求最大最小值(简单方式:将所有数据作为valueslist)

/*
 * 求最大最小值(简单方式:将所有数据作为valueslist)
 */
public class MaxminMap extends Mapper<LongWritable, Text, Text, LongWritable> {
	@Override
	protected void map(LongWritable key, Text value,
			Mapper<LongWritable, Text, Text, LongWritable>.Context context)
			throws IOException, InterruptedException {
		String line = value.toString();
		if (line.trim().length() > 0) {
			context.write(new Text("key:"),
					new LongWritable(Long.parseLong(line)));
		}
	}
}


 

public class MaxminRedu extends Reducer<Text, LongWritable, Text, LongWritable> {
	@Override
	protected void reduce(Text key, Iterable<LongWritable> values,
			Reducer<Text, LongWritable, Text, LongWritable>.Context context)
			throws IOException, InterruptedException {
		long max = Long.MIN_VALUE;
		long min = Long.MAX_VALUE;
		for (LongWritable val : values) {
			if (val.get() > max) {
				max = val.get();
			}
			if (val.get() < min) {
				min = val.get();
			}
		}
		context.write(new Text("Max"), new LongWritable(max));
		context.write(new Text("Min"), new LongWritable(min));
	}
}


 

public class MaxminMain {
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = new Job(conf);
		job.setJarByClass(MaxminMain.class);

		job.setMapperClass(MaxminMap.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(LongWritable.class);

		job.setReducerClass(MaxminRedu.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(LongWritable.class);

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

 

三:求数据的topN排行

?setup()此方法被MapReduce框架仅且执行一次,在执行Map任务前,进行相关变量或者资源的集中初始化工作。若是将资源初始化工作放在方法map()中,导致Mapper任务在解析每一行输入时都会进行资源初始化工作,导致重复,程序运行效率不高!


?cleanup()此方法被MapReduce框架仅且执行一次,在执行完毕Map任务后,进行相关变量或资源的释放工作。若是将释放资源工作放入方法map()中,也会导致Mapper任务在解析、处理每一行文本后释放资源,而且在下一行文本解析前还要重复初始化,导致反复重复,程序运行效率不高!

PS:大数据情况下首先在map中将数据进行一次数组排行

/*
 * 求数据的topN排行
 */
/*
 * 原始数据
 * orderid,userid,payment,productid
 * 1,9818,100,121
 * 2,8918,2999,22
 * 3,2322,1234,11
 * 4,343,2232,22
 * 5,232,434,1 
 * 6,34,232,11
 * 7,2322,9000,54
 * 8,45,3454,34
 */
/*
 * 预测结果(求TOP5-payment)
 * 1,9000
 * 2,
 * 3,
 * 4,
 * 5,
 */
/*
 * 在map是将N排序
 */
/*
 ?setup(),此方法被MapReduce框架仅且执行一次,在执行Map任务前,进行相关变量或者资源的集中初始化工作。若是将资源初始化工作放在方法map()中,导致Mapper任务在解析每一行输入时都会进行资源初始化工作,导致重复,程序运行效率不高!
 ?cleanup(),此方法被MapReduce框架仅且执行一次,在执行完毕Map任务后,进行相关变量或资源的释放工作。若是将释放资源工作放入方法map()中,也会导致Mapper任务在解析、处理每一行文本后释放资源,而且在下一行文本解析前还要重复初始化,导致反复重复,程序运行效率不高!
 */
public class topNMap extends
		Mapper<LongWritable, Text, IntWritable, IntWritable> {
	int len; // topN中的N
	int[] top; // top数组

	@Override
	protected void map(LongWritable key, Text value,
			Mapper<LongWritable, Text, IntWritable, IntWritable>.Context context)
			throws IOException, InterruptedException {
		String line = value.toString().trim();
		if (line.trim().length() > 0) {
			String str[] = line.split(",");
			if (str.length == 4) {
				int payment = Integer.parseInt(str[2]);
				// 将数据放入top[]数组中
				add(payment);
			}
		}
	}

	private void add(int payment) {
		top[0] = payment;
		Arrays.sort(top); // 数组的升序排序
	}

	// 初始化
	@Override
	protected void setup(
			Mapper<LongWritable, Text, IntWritable, IntWritable>.Context context)
			throws IOException, InterruptedException {
		len = context.getConfiguration().getInt("N", 5); // 默认为5
		top = new int[len + 1]; // 以top[0]为比较标准,其后的5个数与top[0]比较。所以要(len+1)个大小
	}

	@Override
	protected void cleanup(
			Mapper<LongWritable, Text, IntWritable, IntWritable>.Context context)
			throws IOException, InterruptedException {
		for (int i = 1; i < len + 1; i++) { // 只需要取出top后面的(除第一个位置以外的)几个数
			context.write(new IntWritable(top[i]), new IntWritable(top[i]));
		}
	}
}


 

public class TopNRedu extends
		Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
	int len;
	int[] top;

	@Override
	protected void reduce(
			IntWritable key,
			Iterable<IntWritable> values,
			Reducer<IntWritable, IntWritable, IntWritable, IntWritable>.Context context)
			throws IOException, InterruptedException {
		add(key.get());
	}

	private void add(int payment) {
		top[0] = payment;
		Arrays.sort(top); // 数组的升序排序
	}

	@Override
	protected void setup(
			Reducer<IntWritable, IntWritable, IntWritable, IntWritable>.Context context)
			throws IOException, InterruptedException {
		len = context.getConfiguration().getInt("N", 5);
		top = new int[len + 1];
	}

	@Override
	protected void cleanup(
			Reducer<IntWritable, IntWritable, IntWritable, IntWritable>.Context context)
			throws IOException, InterruptedException {
		for (int i = len; i >= 1; i--) { // 降序排 //key为从到小的次序 //value为top[]数组的值
			context.write(new IntWritable(len - i + 1), new IntWritable(top[i]));
		}
	}
}


 

public class topNMain {
	public static void main(String[] args) throws Exceptio
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • MapReduce编程之数据去重,mapreduce编程数据
  • [Hive]MapReduce将数据写入Hive分区表,mapreducehive
  • MapReduce处理数据平均值与数值大小排行比较,mapreduce平均值

相关文章

  • MapReduce编程之数据去重,mapreduce编程数据
  • CentOS6.5系统下Hadoop2.6.0完全分布式环境安装与配置信息介绍,centos6.5hadoop2.6
  • CentOS 下面解决libvirt版本过低、升级冲突问题,centoslibvirt
  • Spark的日志配置,Spark日志配置
  • 用户大会/会销怎么搞-Zoho CRM用户大会有感,-zohocrm
  • 索引更新,oracle索引更新
  • hadoop distcp 实现不同集群之间数据同步,hadoopdistcp
  • IBM Symphony开发,ibmsymphony开发
  • OpenStack-API开发,openstackapi
  • Hadoop之——HBASE结合MapReduce批量导入数据,hadoopmapreduce

文章分类

  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx

最近更新的内容

    • Dynamics CRM 2015 Update 1 系列(2): Upsert API,dynamicsupsert
    • MapReduce实现倒排索引,mapreduce实现索引
    • Azure云平台学习之路(一)——Azure简介,azure之路
    • CentOS6.5系统下Hadoop2.6.0完全分布式环境安装与配置信息介绍,centos6.5hadoop2.6
    • Namespace:Openstack的网络实现,namespaceopenstack
    • JS查看Object对象的内容,jsobject对象
    • 修改 openstack 中 nova boot 创建实例只能在10个以内的限制,openstacknova
    • Java 调用Hive 自定义UDF,java调用hiveudf
    • HDFS读文件解析,
    • Hadoop自测题,hadoop

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有