利用MapReduce计算平均数
利用mapreduce求出股票价格的开盘和收盘平均数
下图为采集到的股票信息,共计1416支股票的信息
因为在linux系统下默认采用utf-8的编码格式,而在win下txt默认采用ANSI编码格式。所以需要在linux下将文件转换一下格式,可以采用:
- package economic;
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.StringTokenizer;
- 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 ScoreAvgTest {
- /**
- *
- * @author hadoop www.ysgj1688.com KEYIN:输入map的key值,为每行文本的开始位置子字节计算,(0,11...)
- * VALUEIN:输入map的value,为每行文本值 KEYOUT :输出的key值 VALUEOUT:输出的value值
- */
- public static class MapperClass extends Mapper<Object, Text, Text, Text> {
- private Text companyName = new Text();
- private Text open = new Text();
- private Text data=new Text();
- private int n = 0;
- @Override
- protected void map(Object key, Text value, Context context)
- throws IOException, InterruptedException {
- // TODO Auto-generated method stub
- System.out.println(this.n);
- n++;
- String lineText = value.toString();
- String[] args = lineText.split("\\s+");
- if (args.length == 4) {
- this.companyName.set(args[1]);
- }
- if (args.length == 7) {
- try {
- System.out.println("Bfter Reducer:" + companyName + ","
- + args[1]);
- data.set(args[1]+" "+args[4]);
- context.write(this.companyName, data);
- } catch (IOException e) {
- e.printStackTrace(www.vboyl130.cn);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- *
- * @author hadoop KEYIN:输入的名字 VALUEIN:输入的分数 KEYOUT:输出的名字 VALUEOUT:统计输出的平均分
- */
- public static class ReducerClass extends Reducer<Text, Text, Text, Text> {
- private Text text = new Text();
- protected void reduce(Text companyName, Iterable<Text> kaipan,
- Context context) throws IOException, InterruptedException {
- // TODO Auto-generated method stub
- double sumOpen = 0.0;
- double sumClose = 0.0;
- int num = 0;
- Iterator<Text> $it = kaipan.iterator();
- while ($it.hasNext())www.vboyule.cn {
- String record = $it.next(www.baohuayule.net ).toString();
- String[] getData=record.split(" ");
- System.out.println(num);
- System.out.println("原始数据:" + record);
- num++;
- System.out.println("第" www.qinlinyule.cn + num + "次循环");
- sumOpen += (Double.valueOf(getData[0])*100);
- sumClose+=(Double.valueOf(getData[1])*100);
- }
- double openPrise = sumOpen / (100 * num);
- double closePrise = sumClose / (100 * num);
- System.out.println("openPrice1:" + openPrise);
- System.out.println("www.120xh.cn closePrice1:" + closePrise);
- openPrise = (double) Math.round(openPrise * 100) / 100;
- closePrise = (double) Math.round(closePrise * 100) / 100;
- System.out.println("sumOpen:" + sumOpen+" sumClose"+sumClose);
- System.out.println("openPrice2:" + openPrise);
- System.out.println("closePrice2:" + closePrise);
- String result ="开盘平均价:"+Double.toString(openPrise)+", 收盘平均价:"+Double.toString(closePrise);
- text.set(result);
- try {
- context.write(companyName, text);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) throws IOException,
- InterruptedException, www.255055.cn/ ClassNotFoundException {
- Configuration conf = new Configuration();
- conf.set("fs.default.name", "hdfs://localhost:9000");
- String[] otherArgs = new String[] { "export", "output" }; //export为文本输入路径,output为输出路径
- if (otherArgs.length < 2) {
- System.err.println("Usage:wordcount<in>[<in>...]<out>");
- System.exit(2);
- }
- Job job = Job.getInstance(conf, "arg");
- job.setJarByClass(ScoreAvgTest.class);
- job.setMapperClass(MapperClass.class);
- // job.setCombinerClass(ReducerClass.class);
- System.out.println("Mapper over");
- job.setReducerClass(ReducerClass.class);
- System.out.println("Reducer over");
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(Text.class);
- FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
- FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- }
- }
运行后生成的output文件夹中的文件
利用MapReduce计算平均数的更多相关文章
- Hadoop阅读笔记(二)——利用MapReduce求平均数和去重
前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...
- MapReduce计算模型的优化
MapReduce 计算模型的优化涉及了方方面面的内容,但是主要集中在两个方面:一是计算性能方面的优化:二是I/O操作方面的优化.这其中,又包含六个方面的内容. 1.任务调度 任务调度是Hadoop中 ...
- (第4篇)hadoop之魂--mapreduce计算框架,让收集的数据产生价值
摘要: 通过前面的学习,大家已经了解了HDFS文件系统.有了数据,下一步就要分析计算这些数据,产生价值.接下来我们介绍Mapreduce计算框架,学习数据是怎样被利用的. 博主福利 给大家赠送一套ha ...
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- 使用mapreduce计算环比的实例
最近做了一个小的mapreduce程序,主要目的是计算环比值最高的前5名,本来打算使用spark计算,可是本人目前spark还只是简单看了下,因此就先改用mapreduce计算了,今天和大家分享下这个 ...
- 利用sklearn计算文本相似性
利用sklearn计算文本相似性,并将文本之间的相似度矩阵保存到文件当中.这里提取文本TF-IDF特征值进行文本的相似性计算. #!/usr/bin/python # -*- coding: utf- ...
- MapReduce计算模型
MapReduce计算模型 MapReduce两个重要角色:JobTracker和TaskTracker. MapReduce Job 每个任务初始化一个Job,没个Job划分为两个阶段:Map和 ...
- MapReduce——计算温度最大值 (基于全新2.2.0API)
MapReduce——计算温度最大值 (基于全新2.2.0API) deprecated: Job类的所有Constructors, 新的API用静态方法getInstance(conf)来去的Job ...
- hadoop笔记之MapReduce的应用案例(利用MapReduce进行排序)
MapReduce的应用案例(利用MapReduce进行排序) MapReduce的应用案例(利用MapReduce进行排序) 思路: Reduce之后直接进行结果合并 具体样例: 程序名:Sort. ...
随机推荐
- 使用免费公开的api接口示例(iOS)
做项目难免需要测试,要测试就需要一些接口,现在网上的很多接口都是需要收费的. 以下是目前找到的免费 JSON API免费接口 云聚数据 网吧数据 其中选取了一个百度百科的接口 百度接口 百度百科接口: ...
- UISearchController(使用)
效果图1 效果图2 其实一般是在第一种情况使用的UISearchController,但是第二种情况这种又懒得去用uisearchbar+uitableview等等去处理, 其实主要是对数据源的合理使 ...
- Python 初始—(项目 目录结构)
在os 包下,给出 import os ,os.path.abspath(__file__)获取当前文件的相对路径,os.path.dirname 获取当前文件所在的文件夹目录 print(os.pa ...
- 【例题收藏】◇例题·II◇ Berland and the Shortest Paths
◇例题·II◇ Berland and the Shortest Paths 题目来源:Codeforce 1005F +传送门+ ◆ 简单题意 给定一个n个点.m条边的无向图.保证图是连通的,且m≥ ...
- Windows Server 2012 搭建DHCP及远程路由访问
1.1 基础环境信息 1.2 DHCP与远程访问服务器角色安装 1.服务器管理器—>仪表板—>添加角色和功能,出现添加角色和功能向导,点击下一步 2.选择安装类型为基于角色或基 ...
- HTML基本教程,及一些基本常用标签。
HTML基本结构,及常用标签 <DOCTYPE html> <html> <head> <meta charset="UTF-8" /&g ...
- composer 使用中国镜像
本人使用的是windows系统,安装完Composer后,直接打开CMD,执行以下代码: composer config -g repo.packagist composer https://pack ...
- python——闰年的判断
写一个程序,判断给定年份是否为闰年. 这样定义闰年的:能被4整除但不能被100整除,或者能被400整除都是闰年. while(1): year = input("请输入一个年份,让我判断一下 ...
- 004---Django简单示例
一.MVC与MTV模型 在web开发领域里著名的MVC模式,所谓MVC就是把web应用分为模型(M).控制器(C).视图(V)三层,达到了解耦的效果. 一次完整的请求如图: 但是django用的是M ...
- 4-linux基本命令
1. cd命令 cd 回当前用户 家目录 cd /home 进入home目录 (绝对路径) (相对路径) cd – 上一目录和当前目录来回切换(主要用于返回上一目录) cd . ...