MapReduce实践 Uber数据分析

栏目: 编程工具 · 发布时间: 6年前

内容简介:这篇博客是关于如何在Hadoop MapReduce中进行Uber数据分析的。该数据有4列:

这篇博客是关于如何在Hadoop MapReduce中进行Uber数据分析的。

数据

数据下载

数据展示

MapReduce实践 Uber数据分析

数据说明

该数据有4列:

  1. dispatching_base_number
  2. date
  3. active_vehicles
  4. trips

问题描述

计算每个Basement每个周几总共有多少trips

MapReduce实现

Mapper

在Mapper中 使用 java.time.LocalDate 来获取每个年月日具体是星期几,并将 Basement_number+dayofweek 作为keys, tripNum 作为value。

public static class ExtractTripMapper extends Mapper<Object, Text, Text, IntWritable>
{
    private IntWritable tripNum; // trip 值
    String specifyDate = "MM/DD/YYYY";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/y");  // date转化格式
    LocalDate date;
    String dayOfWeek;

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException
    {
        String[] splitArray = value.toString().split(","); // 对字符串进行切分
        specifyDate = splitArray[1];
        // 使用try来处理不和谐的数据
        try{
            date = LocalDate.parse(specifyDate,formatter);
            dayOfWeek = date.getDayOfWeek().toString();
            tripNum =  new IntWritable(new Integer(splitArray[3]));
        }
        catch (DateTimeParseException e){
            e.printStackTrace();
            return;
        }

        context.write(new Text(splitArray[0] + "+" + dayOfWeek), tripNum);
    }
}

Combiner&Reducer

之后就与WordCont相同,进行简单的统计和合并。

public static class SumReducer extends Reducer<Text, IntWritable, Text, IntWritable>
{
    public void reduce(Text key, Iterable<IntWritable> values, Context context)
            throws IOException, InterruptedException
    {
        int sum = 0;
        for (IntWritable val : values)
        {
            sum += val.get();
        }
        context.write(key, new IntWritable(sum));
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法时代

算法时代

Luke Dormehl / 胡小锐、钟毅 / 中信出版集团 / 2016-4-1 / CNY 59.00

世界上的一切事物都可以被简化成一个公式吗?数字可以告诉我们谁是适合我们的另一半,而且能和我们白头偕老吗?算法可以准确预测电影的票房收入,并且让电影更卖座吗?程序软件能预知谁将要实施犯罪,并且精确到案发时间吗?这些事听起来都像是科幻小说中的情节,但事实上,它们仅是日益被算法主宰的人类世界的“冰山一角”。 近年来随着大数据技术的快速发展,我们正在进入“算法经济时代”。每天,算法都会对展示在我们眼......一起来看看 《算法时代》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具