基于MapReduce的手机流量统计分析

1,代码

package mr;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.ArrayWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
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;

/**
 * 使用ArrayWritable
 */
public class TrafficApp4 {

    public static void main(String[] args) throws Exception{
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf , TrafficApp4.class.getSimpleName());
        job.setJarByClass(TrafficApp4.class);

        FileInputFormat.setInputPaths(job, args[0]);
        job.setMapperClass(TrafficMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongArrayWritable.class);

        job.setReducerClass(TrafficReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongArrayWritable.class);
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        job.waitForCompletion(true);
    }

    public static class TrafficMapper extends Mapper<LongWritable, Text, Text, LongArrayWritable>{
        @Override
        protected void map(LongWritable key, Text value,
                Mapper<LongWritable, Text, Text, LongArrayWritable>.Context context)
                        throws IOException, InterruptedException {
            String line = value.toString();
            String[] splited = line.split("\t");
            String phonenumber = splited[1];

            String upPackNum = splited[6];
            String downPackNum = splited[7];
            String upPayLoad = splited[8];
            String downPayLoad = splited[9];

            Text k2 = new Text(phonenumber);
            LongArrayWritable v2 = new LongArrayWritable(upPackNum, downPackNum, upPayLoad, downPayLoad);
            context.write(k2, v2);
        }
    }

    public static class TrafficReducer extends Reducer<Text, LongArrayWritable, Text, LongArrayWritable>{
        @Override
        protected void reduce(Text k2, Iterable<LongArrayWritable> v2s,
                Reducer<Text, LongArrayWritable, Text, LongArrayWritable>.Context context)
                        throws IOException, InterruptedException {

            long upPackNum = 0L;
            long downPackNum = 0L;
            long upPayLoad = 0L;
            long downPayLoad = 0L;
            for (LongArrayWritable v2 : v2s) {
                Writable[] values = v2.get();
                upPackNum += ((LongWritable)values[0]).get();
                downPackNum += ((LongWritable)values[1]).get();
                upPayLoad += ((LongWritable)values[2]).get();
                downPayLoad += ((LongWritable)values[3]).get();
            }

            LongArrayWritable v3 = new LongArrayWritable(upPackNum, downPackNum, upPayLoad, downPayLoad);
            context.write(k2, v3);
        }
    }

    public static class LongArrayWritable extends ArrayWritable{
        /**
         * 在调用的时候,首先调用该方法,然后调用set(Writable[])
         */
        public LongArrayWritable() {
            super(LongWritable.class);
        }
        /**
         * 直接调用该方法即可
         * @param values
         */
        public LongArrayWritable(LongWritable[] values) {
            super(LongWritable.class, values);
        }
        /**
         * 直接调用该方法即可
         * @param upPackNum
         * @param downPackNum
         * @param upPayLoad
         * @param downPayLoad
         */
        public LongArrayWritable(Long upPackNum, Long downPackNum, Long upPayLoad, Long downPayLoad) {
            super(LongWritable.class);
            LongWritable[] values = new LongWritable[4];
            values[0] = new LongWritable(upPackNum);
            values[1] = new LongWritable(downPackNum);
            values[2] = new LongWritable(upPayLoad);
            values[3] = new LongWritable(downPayLoad);
            super.set(values);
        }
        /**
         * 直接调用该方法即可
         * @param upPackNum
         * @param downPackNum
         * @param upPayLoad
         * @param downPayLoad
         */
        public LongArrayWritable(String upPackNum, String downPackNum, String upPayLoad, String downPayLoad) {
            super(LongWritable.class);
            LongWritable[] values = new LongWritable[4];
            values[0] = new LongWritable(Long.parseLong(upPackNum));
            values[1] = new LongWritable(Long.parseLong(downPackNum));
            values[2] = new LongWritable(Long.parseLong(upPayLoad));
            values[3] = new LongWritable(Long.parseLong(downPayLoad));
            super.set(values);
        }

        @Override
        public String toString() {
            String[] array =  super.toStrings();
            return StringUtils.join(array, "\t");
        }
    }

}

2,ArrayWritable的API

org.apache.hadoop.io
Class ArrayWritable

java.lang.Object
  

org.apache.hadoop.io.ArrayWritable
已实现的接口:
Writable

public class ArrayWritableextends Objectimplements Writable

A Writable for arrays containing instances of a class. The elements of this writable must all be instances of the same class. If this writable will be the input for a Reducer, you will need to create a subclass that sets the value to be of the proper type. For example: public class IntArrayWritable extends ArrayWritable { public IntArrayWritable() { super(IntWritable.class); } }


构造方法摘要
ArrayWritable(Class<? extends Writable> valueClass) 
           
ArrayWritable(Class<? extends Writable> valueClass, Writable[] values) 
           
ArrayWritable(String[] strings) 
           
方法摘要
 Writable[] get() 
           
 Class getValueClass() 
           
 void readFields(DataInput in) 
          Deserialize the fields of this object from in.
 void set(Writable[] values) 
           
 Object toArray() 
           
 String[] toStrings() 
           
 void write(DataOutput out) 
          Serialize the fields of this object to out.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
构造方法详细信息

ArrayWritable

public ArrayWritable(Class<? extends Writable> valueClass)

ArrayWritable

public ArrayWritable(Class<? extends Writable> valueClass,
                     Writable[] values)

ArrayWritable

public ArrayWritable(String[] strings)
方法详细信息

getValueClass

public Class getValueClass()

toStrings

public String[] toStrings()

toArray

public Object toArray()

set

public void set(Writable[] values)

get

public Writable[] get()

readFields

public void readFields(DataInput in)
                throws IOException
Description copied from interface: Writable
Deserialize the fields of this object from in.

For efficiency, implementations should attempt to re-use storage in the existing object where possible.

Specified by:
readFields in interface Writable
Parameters:
in - DataInput to deseriablize this object from.
Throws:
IOException

write

public void write(DataOutput out)
           throws IOException
Description copied from interface: Writable
Serialize the fields of this object to out.

Specified by:
write in interface Writable
Parameters:
out - DataOuput to serialize this object into.
Throws:
IOException

 

时间: 2024-10-05 20:34:30

基于MapReduce的手机流量统计分析的相关文章

引领手机流量营销 容联云通讯嘿嘿流量打造多场景专业服务

随着4G网络和智能设备的快速普及,手机流量作为移动互联网时代的用户刚需,已经被越来越多的企业采购作为宣传推广产品的敲门砖.手机流量营销,其实是一种全新的激励营销方式,电商.APP.游戏等产品在做营销推广时,以手机流量包作为激励品,刺激用户参与活动和互动,以达到营销推广的目的. 当然,这些流量活动都离不开第三方手机流量分发平台,一个连接着运营商和互联网厂商的关键点.今日,我们就拿嘿嘿流量为例,简单分析下第三方手机流量分发平台的使用场景. 嘿嘿流量:多场景手机流量营销方案 嘿嘿流量为企业提供灵活.便

ios测试基础五: ios手机流量消耗

iOS手机流量消耗 在iphone手机上使用wifi或者数据连接方式,操作某个应用下某个场景,实时监测流量消耗情况: (一般情况下,更多地要关注 数据连接2G或3G或4G 下流量消耗情况) 前提准备: 1. xcode 2. instruments(7.1.1版本):Network Activity 操作步骤: 1. 打开instrments,iphone连接上mac电脑(手机开启 数据连接 模式): 2.选择连接上的Iphone手机,再选择好待监控的应用: 3.在Library下选择Netwo

CG_Hadoop:基于MapReduce的计算几何

原作:Ahmed Eldawy:Mohamed F.Mokbel (UMN) 翻译:Leo(CAU) 注:由于本人翻译水平有限,如有错误,敬请谅解,可以在评论中指出,欢迎交流! 摘要:Hadoop使用了MapReduce编程范式,目前已经被公认为是分布式环境中分析大数据的标准框架.然而,它并不能很好的应用于大规模的计算几何处理.本文介绍的CG_Hadoop是一套可伸缩的和高效的MapReduce算法,用于处理各种基本计算几何问题,例如多边形合并.skyline(轮廓线).convex hull(

精品软件 推荐 流量报表软件 networx 统计电脑网络用的流量 用手机流量卡的特别有用

介绍一个免费的软件,可以统计电脑每一个网卡的使用流量情况, 用手机流量卡的特别实用 下载地址也可以如下,或者百度中找到:http://www.400gb.com/file/116129429 精品软件 推荐 流量报表软件 networx  统计电脑网络用的流量 用手机流量卡的特别有用

基于HBase的手机数据备份系统

洞穴逃生 描述: 精灵王子爱好冒险,在一次探险历程中,他进入了一个神秘的山洞.在洞穴深处,精灵王子不小心触动了洞穴内暗藏的机关,整个洞穴将很快塌陷,精灵王子必须尽快逃离洞穴.精灵王子的跑步速度为17m/s,以这样的速度可能是无法逃出洞穴的.庆幸的是精灵王子拥有闪烁法术,可在1s内移动60m,不过每次使用闪烁法术都会消耗魔法值10点.精灵王子的魔法值恢复的速度为4点/s,只有处在原地休息状态时才能恢复. 现在已知精灵王子的魔法初值M,他所在洞穴中的位置与洞穴出口之间的距离S,距离洞穴塌陷的时间T.

15元网购2000M手机流量?都是骗人的!

消费者在运营商处购买手机流量包,一般是5元30M.10元70M,最大力度也就是10元100M,但是,在网络上,15元就能买到2000M.这么便宜,其中确实有诈. 前几天,消费者王先生找记者投诉,经过协调,目前淘宝已关闭了这家网店. 网售流量包价格超低 有消费者投诉称,他目前使用的是话费套餐,88元包含500M流量,但是每个月都不够用,额外增加流量包的话,10元钱只能买70M,就合计上网去淘淘便宜货. 在淘宝网上,销售手机流量包的商家非常多,而且优惠力度也很大.王先生找到一家几乎全是好评的网店,卖

基于mapreduce的大规模连通图寻找算法

基于mapreduce的大规模连通图寻找算法 当我们想要知道哪些账号是一个人的时候往往可以通过业务得到两个账号之间有联系,但是这种联系如何传播呢? 问题 已知每个账号之间的联系 如: A B B C D E F C B G 得到 对应的一个人的账号 如: A B C F G为同一个人 D E 为同一个人 当前迭代次数11次 理论最大迭代次数: max_nick_count = max(nick_count) max_iterator = log2(max_nick_count) 62%的账号可在

MapReduce教程(一)基于MapReduce框架开发&lt;转&gt;

1 MapReduce编程 1.1 MapReduce简介 MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算,用于解决海量数据的计算问题. MapReduce分成了两个部分: 1.映射(Mapping)对集合里的每个目标应用同一个操作.即,如果你想把表单里每个单元格乘以二,那么把这个函数单独地应用在每个单元格上的操作就属于mapping. 2.化简(Reducing)遍历集合中的元素来返回一个综合的结果.即,输出表单里一列数字的和这个任务属于reducing. 你向Ma

Hadoop伪分布安装详解+MapReduce运行原理+基于MapReduce的KNN算法实现

本篇博客将围绕Hadoop伪分布安装+MapReduce运行原理+基于MapReduce的KNN算法实现这三个方面进行叙述. (一)Hadoop伪分布安装 1.简述Hadoop的安装模式中–伪分布模式与集群模式的区别与联系. Hadoop的安装方式有三种:本地模式,伪分布模式,集群(分布)模式,其中后两种模式为重点,有意义 伪分布:如果Hadoop对应的Java进程都运行在一个物理机器上,称为伪分布 分布:如果Hadoop对应的Java进程运行在多台物理机器上,称为分布.[集群就是有主有从] 伪