MapReduce处理公共自行车数据
来源:互联网 发布:js 跨域删除cookie 编辑:程序博客网 时间:2024/03/29 20:58
利用Hadoop平台处理公共自行车数据,数据Excel表如下:
Excel表中有一列duration表示自行车使用时间,利用MapReduce统计自行车使用时间为60s,120,180s以此类推的使用量。
代码如下:
Map端:
package com.tyut.rcr;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.LongWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Mapper;public class Map extends Mapper<LongWritable, Text, Text, IntWritable> { private static final IntWritable one = new IntWritable(1); 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(); String[] datas= line.split(","); String during=datas[0]; int dur = Integer.parseInt(during); if(dur<60){ String str = "自行车使用时间<60s次数: "+dur; context.write(new Text(str),one); } else { for(int i=1;i<2000;i++){ if (dur>=(i*60)&&dur<(i+1)*60){ String str = "自行车使用时间"+(i*60)+"s到"+(i+1)*60+"s之间次数: "; context.write(new Text(str),one); } else if(dur>=(2000*60)) { String str = "自行车使用时间大于120000s的次数: "; context.write(new Text(str),one); break; } } } };}
Reduce端:
package com.tyut.rcr;import java.io.IOException;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;public class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { @Override protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException { int sum = 0; for ( IntWritable val : values){ sum+=val.get(); } context.write(key, new IntWritable(sum)); }}
driver端:
package com.tyut.rcr;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;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;public class Time { public static void main(String[] args) throws Exception, IOException { Configuration conf = new Configuration(); @SuppressWarnings("deprecation") Job job = new Job (conf,"Time"); job.setJarByClass(Time.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.waitForCompletion(true); }}
将上述代码打包为.jar包提交给集群运行,结果如下:
自行车使用时间1020s到1080s的次数: 2225
自行车使用时间1080s到1140s的次数: 1764
自行车使用时间1140s到1200s的次数: 1491
自行车使用时间1200s到1260s的次数: 1222
自行车使用时间120s到180s之间次数: 3613
自行车使用时间1260s到1320s的次数: 1111
自行车使用时间1320s到1380s的次数: 1008
自行车使用时间1380s到1440s的次数: 847
自行车使用时间1440s到1500s的次数: 798
自行车使用时间1500s到1560s的次数: 676
自行车使用时间1560s到1620s的次数: 623
自行车使用时间1620s到1680s的次数: 553
自行车使用时间1680s到1740s的次数: 433
自行车使用时间1740s到1800s的次数: 367
自行车使用时间1800s到1860s的次数: 360
自行车使用时间180s到240s之间次数: 9291
自行车使用时间1860s到1920s的次数: 274
自行车使用时间1920s到1980s的次数: 251
自行车使用时间1980s到2040s的次数: 187
自行车使用时间2040s到2100s的次数: 179
自行车使用时间2100s到2160s的次数: 155
自行车使用时间2160s到2220s的次数: 147
自行车使用时间2200s到2280s的次数: 138
自行车使用时间2280s到2340s的次数: 128
自行车使用时间2340s到2400s的次数: 114
自行车使用时间2400s到2460s的次数: 125
自行车使用时间240s到300之间次数: 11902
自行车使用时间2460s到2520s的次数: 118
自行车使用时间2520s到2580s的次数: 104
自行车使用时间2580s到2640s的次数: 79
自行车使用时间300s到360s之间次数: 12339
自行车使用时间360s到420s之间次数: 12447
自行车使用时间420s到480s之间次数: 11749
自行车使用时间480s到540s之间次数: 11122
自行车使用时间540s到600s之间次数: 10093
自行车使用时间600s到660s之间次数: 9029
自行车使用时间60s到120s之间次数: 1115
自行车使用时间660s到720s之间次数: 7570
自行车使用时间720s到780s之间次数: 6176
自行车使用时间780s到840s之间次数: 4904
自行车使用时间840s到900s之间次数: 4112
自行车使用时间900s到960s之间次数: 3369
自行车使用时间960s到1020s的次数: 2774
自行车使用时间大于2700s的次数: 6838
- MapReduce处理公共自行车数据
- 公共自行车相关
- 台北公共自行车预测
- 公共自行车租赁系统
- MapReduce处理HBase中的数据
- 使用MapReduce处理Hbase数据
- MapReduce-输入数据整块处理
- 公共自行车iPhone版(苏州数据加密算法改变,更新中)
- 西安自行车租赁办卡点 公共自行车的使用方法
- 珠海公共自行车系统分析系列 - 前言
- 珠海公共自行车系统分析系列 - 目录
- 珠海公共自行车系统分析系列 - 硬件分析
- 珠海公共自行车系统分析系列 - 查询系统
- 珠海公共自行车系统分析系列 - 网站
- 珠海公共自行车系统分析系列 - Android App
- MapReduce处理数据(用户每周登录次数)
- MapReduce中跨InputSplit数据的处理
- mapreduce处理数据倾斜的一些方法
- NOIP复习资料——往年习题精选
- 2016云栖大会-物联网专场总结
- VC 6.0环境下配置ucos ii
- oracle行转列和列转行
- POJ 1088 滑雪 DP
- MapReduce处理公共自行车数据
- 腾讯IM集成到eclipse(随心聊IM)
- html&css笔记
- MyEclipse下创建的项目 导入eclipse
- Transmission Media
- excel中的填充
- 数组排序
- bzoj 1260: [CQOI2007]涂色paint 区间动态规划
- 遗传算法解析