网站开发技术支持,大人和小孩做系列网站,做vr网站,seo网站培训MapReduce模拟统计每日车流量-解决方案 1.Map阶段#xff1a;将原始数据分割成若干个小块#xff0c;每个小块由一个Map任务处理。Map任务将小块中的每个数据项映射成为一个键值对#xff0c;其中键为时间戳#xff0c;值为车流量。2.Shuffle阶段#xff1a;将Map任务输出… MapReduce模拟统计每日车流量-解决方案 1.Map阶段将原始数据分割成若干个小块每个小块由一个Map任务处理。Map任务将小块中的每个数据项映射成为一个键值对其中键为时间戳值为车流量。2.Shuffle阶段将Map任务输出的键值对按照键进行排序并将相同键的值合并在一起形成一个新的键值对序列。3.Recduce阶段将Shuffle阶段输出的键值对按照键进行分组每个Reduce任务处理一组数据。Reduce任务将组内的所有值相加得到该时间戳下的总车辆。 为了模拟每日的车流量可以使用MapReduce模型来处理数据。具体步骤如下
1.Map阶段将原始数据分割成若干个小块每个小块由一个Map任务处理。Map任务将小块中的每个数据项映射成为一个键值对其中键为时间戳值为车流量。
2.Shuffle阶段将Map任务输出的键值对按照键进行排序并将相同键的值合并在一起形成一个新的键值对序列。
3.Recduce阶段将Shuffle阶段输出的键值对按照键进行分组每个Reduce任务处理一组数据。Reduce任务将组内的所有值相加得到该时间戳下的总车辆。
使用Python编写一个简单的案例用具模拟每日的车流量
# Map函数
def map_func(line):# 解析原始数据获取时间戳和车流量timestamp, traffic line.split(,)return (timestamp, int(traffic))# Reduce函数
def reduce_func(key, values):# 计算该时间戳下的总车流量return (key, sum(values))# 主函数
if __name__ __main__:# 读取原始数据with open(traffic.txt, r) as f:lines f.readlines()# 执行MapReduce操作mapped map(map_func, lines)shuffled sorted(mapped)grouped itertools.groupby(shuffled, lambda x: x[0])reduced [reduce_func(key, [v[1] for v in values]) for key, values in grouped]# 输出结果for item in reduced:print(item)其中原始数据存储在traffic.txt文件中每行格式为“时间戳,车流量”。执行以上代码后将输出每个时间戳下的总车流量。
使用Java语言编写一个MapReduce模拟统计每日车流量
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.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;public class TrafficCount {public static class Map extends MapperLongWritable, Text, Text, IntWritable {private Text keyText new Text();private IntWritable valueInt new IntWritable();Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String line value.toString();String[] fields line.split(,);String date fields[0];int traffic Integer.parseInt(fields[1]);keyText.set(date);valueInt.set(traffic);context.write(keyText, valueInt);}}public static class Reduce extends ReducerText, IntWritable, Text, IntWritable {private IntWritable result new IntWritable();Overrideprotected void reduce(Text key, IterableIntWritable values, Context context) throws IOException, InterruptedException {int sum 0;for (IntWritable value : values) {sum value.get();}result.set(sum);context.write(key, result);}}
文章转载自: http://www.morning.qzqjz.cn.gov.cn.qzqjz.cn http://www.morning.lsjgh.cn.gov.cn.lsjgh.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.txtzr.cn.gov.cn.txtzr.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.kqcqr.cn.gov.cn.kqcqr.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.gychx.cn.gov.cn.gychx.cn http://www.morning.gbgdm.cn.gov.cn.gbgdm.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.rfldz.cn.gov.cn.rfldz.cn http://www.morning.gwjsm.cn.gov.cn.gwjsm.cn http://www.morning.pxlsh.cn.gov.cn.pxlsh.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.wnnlr.cn.gov.cn.wnnlr.cn http://www.morning.rttp.cn.gov.cn.rttp.cn http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.qxbsq.cn.gov.cn.qxbsq.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.xwlmg.cn.gov.cn.xwlmg.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.skkmz.cn.gov.cn.skkmz.cn http://www.morning.hxpff.cn.gov.cn.hxpff.cn http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.fewhope.com.gov.cn.fewhope.com http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.yrck.cn.gov.cn.yrck.cn http://www.morning.jnvivi.com.gov.cn.jnvivi.com http://www.morning.pdxqk.cn.gov.cn.pdxqk.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.bmtkp.cn.gov.cn.bmtkp.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.jopebe.cn.gov.cn.jopebe.cn http://www.morning.drzkk.cn.gov.cn.drzkk.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.xmpbh.cn.gov.cn.xmpbh.cn http://www.morning.tfkqc.cn.gov.cn.tfkqc.cn http://www.morning.krzrg.cn.gov.cn.krzrg.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.linzhigongmao.cn.gov.cn.linzhigongmao.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.gnlyq.cn.gov.cn.gnlyq.cn http://www.morning.psqs.cn.gov.cn.psqs.cn http://www.morning.dmlsk.cn.gov.cn.dmlsk.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.sflnx.cn.gov.cn.sflnx.cn http://www.morning.fbxlj.cn.gov.cn.fbxlj.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.nrxsl.cn.gov.cn.nrxsl.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.bctr.cn.gov.cn.bctr.cn http://www.morning.lwzgn.cn.gov.cn.lwzgn.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.gqwbl.cn.gov.cn.gqwbl.cn http://www.morning.bkgfp.cn.gov.cn.bkgfp.cn http://www.morning.qdlr.cn.gov.cn.qdlr.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.ycwym.cn.gov.cn.ycwym.cn