Analysis of mobile internet traffic by MapReduce

Source: Internet
Author: User

I. Background of the problem

Now mobile just a call on the site to see their own call records, previously this month can only look at one months. But the flow is still only look at January.

The goal is to find the user's Internet traffic over a period of time.

This article does not group the time.

second, data set analysis

You can see that the actual data set does not have a value for each field, but fortunately, it is completely tab-delimited, the data format is good, we need to have the upstream downlink data, there is no missing value. In fact, this needs to be handled in the program, if not, what to do.

1363157985066 1372623050300-fd-07-a4-72-b8:cmcc120.196.100.82i02.c.aliimg.com24272481246812001363157995052 138265441015c-0e-8b-c7-f1-e0:cmcc120.197.40.44026402001363157991076 1392643565620-10-7a-28-cc-0a: CMCC120.196.100.992413215122001363154400022 139262511065c-0e-8b-8b-b1-50:cmcc120.197.40.44024002001363157993044 1821157596194-71-ac-cd-e6-18:cmcc-easy120.196.100.99iface.qiyi.com Video Site 1512152721062001363157995074 841384135c-0e-8b-8c-e8-20:7daysinn120.197.40.4122.72.52.122016411614322001363157993055 13560439658c4-17-fe-ba-de-d9:cmcc120.196.100.99181511169542001363157995033 159201332575c-0e-8b-c7-ba-20: cmcc120.197.40.4sug.so.360.cn Information Security 20203156293620013631579830191371919941968-a1-b7-03-07-b1: cmcc-easy120.196.100.824024002001363157984041 136605779915c-0e-8b-92-5c-20: cmcc-easy120.197.40.4s19.cnzz.com Site Statistics 24969606902001363157973098 150136858585c-0e-8b-c7-f7-90: Cmcc120.197.40.4rank.ie.sogou.com search engine 2827365935382001363157986029 15989002119e8-99-c4-4e-93-e0: Cmcc-easy120.196.100.99www.umeng.com Site Statistics 3319381802001363157992093 13560439658c4-17-fe-ba-de-d9:cmcc120.196.100.9915991849382001363157986041 134802531045c-0e-8b-c7-fc-80:cmcc-easy120.197.40.4331801802001363157984040 136028465655c-0e-8b-8b-b6-00: cmcc120.197.40.42052.flash2-http.qq.com Integrated Portal 1512193829102001363157995093 1392231446600-fd-07-a2-ec-ba: cmcc120.196.100.82img.qfc.cn1212300837202001363157982040 135024688235C-0A-5B-6A-0B-D4: cmcc-easy120.196.100.99y0.ifengimg.com Integrated Portal 5710273351103492001363157986072 1832017338284-25-db-4f-10-1a: Cmcc-easy120.196.100.99input.shouji.sogou.com search engine 2118953124122001363157990043 1392505741300-1f-64-e1-e6-9a: Cmcc120.196.100.55t3.baidu.com search engine 696311058482432001363157988072 1376077871000-fd-07-a4-7b-08: CMCC120.196.100.82221201202001363157985066 1372623888800-fd-07-a4-72-b8: cmcc120.196.100.82i02.c.aliimg.com24272481246812001363157993055 13560436666C4-17-FE-BA-DE-D9: CMCC120.196.100.9918151116954200
third, the experimental analysis3.1Mapper

First of all, we aim to count each person's upstream total traffic and downlink total flow, and up and down the total flow, upper and lower line = upstream + downlink, this is meaningful, because we do not get their own online traffic in real-time, this is a bit large, we do not have six-way calculation, or to achieve.

Input key is the line number, usually object and longwritable, the general input key is not used, the data mart line of text, input value is text, enter the phone number as key, then the text, output value contains three kinds of information, upstream total, The sum of the downstream total, can be encapsulated in the array, but this is not conducive to modification, such as the project experience to you to add some properties to display, or reduce some, generally with beans.

Package Cn.app.hadoop.mr.datacount;import Java.io.ioexception;import Java.text.fieldposition;import Org.apache.hadoop.io.longwritable;import Org.apache.hadoop.io.text;import org.apache.hadoop.mapreduce.mapper;// The first to deal with text is generally longwritable or object//line of text is text//output key mobile phone number positioning text//result is Databean must implement writable interface public class Datacountmapper extends Mapper<longwritable, text, text, databean> {//actual processing has been done in the data cleaning in this is not catchpublic void map ( Longwritable key, Text value, Context context) throws IOException, interruptedexception {String line = value.tostring (); string[] fields = Line.split ("\ t"); String Telno = Fields[1];long uppayload = Long.parselong (fields[8]); Long downpayload = Long.parselong (fields[9]);//  Encapsulation to the bean, if the format is not correct then skip here without regard, assuming already cleaned databean Databean = new Databean (telno,uppayload,downpayload);//But this way Each line is new a Databena the next time is not released very slow//can be outside the new Databean, every time to modify the value, if the previous thinking is not possible, because point to//the same piece of memory, can not add//But in Hadoop is OK, Because it is serialized after each write, the next time it is different context.write (new Text (Telno), Databean);}}

  

3.2 Databean

In order for the bean to be able to transmit across the network, the writable interface of Hadoop needs to be implemented, so the interface method is implemented first.

Deserialization deserializable from a  byte stream to a memory public void ReadFields (Datainput in) throws IOException {//TODO auto-generated met Hod stub//Note that   the order type  has no parameters//this.telno = In.readutf (Telno); is wrong this.telno = In.readutf (); this.uppayload = In.readlong (); this.downpayload = In.readlong (); this.totalpayload = In.readlong ();} Serialize serializable, write from memory to byte stream or via RMI on-line transfer public void write (DataOutput out) throws IOException {//TODO auto-generated Method stub//Mobile number is a string, but no writestring, with Writeutfout.writeutf (Telno); Out.writelong (uppayload); Out.writelong ( Downpayload); Out.writelong (totalpayload);}

Write is serialized, the object is written to the byte stream, note that the string type does not have the writestring algorithm, you can use the common writeUTF method, the certificate type with long, and the shadow of the Writelong.

ReadFile is the deserialization, read from the hard disk or the network, and then assign to the object, note that the reading time does not require parameters, such as

This.telno = On.readutf (Telno);

This is wrong, think about the objects are into a byte stream, how can see what is, so it is in order to determine the corresponding attributes, but how many bytes each attribute saved, this depends on the type of property to determine, this would like to allocate memory, the above purely personal understanding.

So what are the attributes of the bean? Need mobile phone number, uplink, downlink, and praise their total traffic, and then generate getter and Setter;mapper submit Databean, this need to have the parameters of the method, not every time setter, In addition, when Mapper is written by the reflection mechanism to get instantiated databean, then we need to find the default scale of the lunch method.

In addition to write Databean when the default output class name +hashcode value, this is not what we need, so rewrite the ToString method, eclipse can be generated automatically.

@Overridepublic String toString () {//Phone number not return "Databean [uppayload=" + uppayload+ ", downpayload=" + Downpayload + ", Totalpayload= "+ Totalpayload +"] ";}

  

3.3 Reducer

The input type of the reducer is the output type of mapper, respectively text and Databean, and his output type is text and Databean.

This is actually the business logic complex some of the Wordcount,mapper submitted to reducer have merged the same key format is <telno,{{up1,dw1},{up2,dw2}}> we need to facilitate the combination of value, Cheap upstream traffic to get upstream total traffic, cheap downlink traffic to get the total Liu Ling, then the most always traffic is up and down the line.

3.4 Main

Can be directly in Eclipse, new reducer driver, is the main function, why main always want new conf, this is to give this job personalized configuration, through the Conf.set is also kv pair, otherwise find all poly de conf.

Also note that when writing the path with hdfs://hostname:8020, because there is no context of the filesystem, so must add, otherwise considered a local file, the hint can not be found.

Four, experimental analysis

You can find two or more records of a user from the original data, sum it up, then compare it to see if the business logic is right.

Analysis of mobile internet traffic by MapReduce

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.