mapreduce求平均数
1.
现有某电商关于商品点击情况的数据文件,表名为goods_click,包含两个字段(商品分类,商品点击次数),分隔符“ ”,由于数据很大,所以为了方便统计我们只截取它的一部分数据,内容如下
要求使用mapreduce统计出每类商品的平均点击次数。
源代码:
package mapreduce; import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
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.Reducer.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import mapreduce.WordCount.MyMapper;
import mapreduce.WordCount.MyReducer; public class MyAverage {
public static class Map extends Mapper<Object, Text, Text, IntWritable> {
private static Text newKey = new Text(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) {
String line = itr.nextToken();
String arr[]=line.split(" ");
newKey.set(arr[]);
int click=Integer.parseInt((arr[].trim()));
context.write(newKey, new IntWritable(click)); }
} } public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int num=;
int count=;
for(IntWritable val:values) {
num+=val.get();
count++;
}
int avg=num/count;
context.write(key, new IntWritable(avg));
}
} public static void main(String[] args) throws Exception { Configuration conf = new Configuration();
System.out.println("start");
Job job = new Job(conf, "MyAverage");
job.setJarByClass(MyAverage.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
Path in = new Path("hdfs://localhost:9000/mymapreduce3/in/goods_click");
Path out = new Path("hdfs://localhost:9000/mymapreduce3/out"); FileInputFormat.addInputPath(job, in);
FileOutputFormat.setOutputPath(job, out);
System.exit(job.waitForCompletion(true) ? : );
}
}
统计数据:
遇到的问题:
1.原本给到的数据中第一列是“商家ID 点击次数”,但是在程序中无法将点击次数从“字符串”转换成“int"型。后来在元数据中去掉了这一行。
2.无法将”点击次数“的数据从String型转化成int型。刚开始发现获得的”点击次数“数据周围包含空格,然后用String.trim()去空格但是不管用。然后源数据中去掉空格。
猜想应该是从数据库导出时数据库中就保存的数据加空格吧。
mapreduce求平均数的更多相关文章
- Hadoop阅读笔记(二)——利用MapReduce求平均数和去重
前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...
- Mapreduce求气温值项目
Mapreduce前提工作 简单的来说map是大数据,reduce是计算<运行时如果数据量不大,但是却要分工做这就比较花时间了> 首先想要使用mapreduce,需要在linux中进行一些 ...
- 利用MapReduce计算平均数
利用mapreduce求出股票价格的开盘和收盘平均数 下图为采集到的股票信息,共计1416支股票的信息 因为在linux系统下默认采用utf-8的编码格式,而在win下txt默认采用ANSI编码格式. ...
- poj 1004:Financial Management(水题,求平均数)
Financial Management Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 126087 Accepted: ...
- C++求平均数
题目内容:求若干个证书的平均数. 输入描述:输入数据含有不多于5组的数据,每组数据由一个整数n(n<=50)打头,表示后面跟着n个整数. 输出描述:对于每组数据,输出其平均数,精确到小数点后3位 ...
- python求平均数及打印出低于平均数的值列表
刚学Python的时候还是要多动手进行一些小程序的编写,要持续不断的进行,知识才能掌握的牢.今天就讲一下Python怎么求平均数,及打印出低于平均数的数值列表 方法一: scores1 = [91, ...
- HDU 1064(求平均数 **)
题意是求 12 个浮点数的平均数,这题不用读题,看到运行时间为 200 ms 再看下样例就能猜出题目的要求了,代码如下: #include <bits/stdc++.h> using na ...
- Hadoop学习之路(二十)MapReduce求TopN
前言 在Hadoop中,排序是MapReduce的灵魂,MapTask和ReduceTask均会对数据按Key排序,这个操作是MR框架的默认行为,不管你的业务逻辑上是否需要这一操作. 技术点 MapR ...
- Hadoop 学习笔记 (十一) MapReduce 求平均成绩
china:张三 78李四 89王五 96赵六 67english张三 80李四 82王五 84赵六 86math张三 88李四 99王五 66赵六 77 import java.io.IOEx ...
随机推荐
- acrord32 pdf自动化
这个东西就是帮助用户自动化处理pdf的 . 可以看作是adobe reader的命令行
- ZROI2018提高day3t1
传送门 分析 我们可以用贪心的思想.对于所有并没有指明关系的数一定是将小的放在前面.于是我们按顺序在每一个已经指明大小顺序的数前面插入所有比它小且没有指明关系的数.详见代码. 代码 #include& ...
- 形式化验证工具(PAT)Perterson Algorithm学习
今天学习一下Perterson Algorithm. 这个算法是使用三个变量来实现并发程序的互斥性算法. 具体看一下代码: Peterson算法是一个实现互斥锁的并发程序设计算法,核心就是三个标志位是 ...
- 图--生成树和最小生成树.RP
树(自由树).无序树和有根树 自由树就是一个无回路的连通图(没有确定根)(在自由树中选定一顶点做根,则成为一棵通常的树). 从根开始,为每个顶点(在树中通常称作结点)的孩子规定从左到右的次 ...
- java的泛型的技巧
最近学习scala,了解了两个概念:class和type,什么是class,就是具有相同的class对象,List<String> ,List<Integer>具有相同的cla ...
- txt中把换行替换为空格
把合适改为html后打开,换行都没了,然后复制到另一个txt即可
- blkid找不到需要的uuid
记录: blkid找不到需要的uuid,需要格式化后才有
- [转]Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 2.
1. Introduction. 1.1 In part 1 of this series of articles, I explained how managed arrays may be tra ...
- sqlserver的索引创建
随着系统数据的增多,一些查询逐渐变慢,这时候我们可以根据sqlserver的执行计划,查看sql的开销,然后根据开销创建索引. 索引有聚集索引与非聚集索引. 聚集索引:聚集索引在存储上是按照顺序存储的 ...
- day05.2-Vim编辑器
一. 安装Vim编辑器和打开新建文件 安装Vim编辑器:apt-get install Vim 新建与打开Vim文件:vim 文件名 二. Vim编辑器三种模式的使用与切换 指令 ...