Nine (a): Mobile Internet traffic Summary sum

Source: Internet
Author: User

To summarize the mobile phone number traffic in the file:

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 website 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 2020315629362001363157983019 1371919941968-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 



To achieve the general function analysis diagram:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/78/1A/wKiom1Z1e9WjqTbmAAEUR-fDAcc250.png "title=" captures 0w. PNG "alt=" Wkiom1z1e9wjqtbmaaeur-fdacc250.png "/>

Implementation of 4 Java programs:

4.1. Create JavaBean for mobile data flow

4.2. Create a Mapper class

4.3. Create a Reducer class

4.4. Create a commit run class


4.1. Create JavaBean for mobile data flow

import java.io.datainput;import java.io.dataoutput;import java.io.ioexception;import  org.apache.hadoop.io.writable;import org.apache.hadoop.io.writablecomparable;/** *  Application example  *  deserialization of the serialization framework in Hadoop the reflection mechanism invokes the class's parameterless constructor  *  so, if you define a parameter structure in a class, you must remember to explicitly define the parameterless constructor  *   @author  * */public class FlowBean implements Writable {private  long upflow;private long dflow;private long sumflow;/** *  explicitly define an parameterless construct  */ Public flowbean ()  {}public flowbean (Long upflow, long dflow)  {this.upflow  = upflow;this.dflow = dflow;this.sumflow = upflow + dflow;} Public long getupflow ()  {return upflow;} Public void setupflow (Long upflow)  {this.upflow = upflow;} Public long getdflow ()  {return dflow;} Public void setdflow (Long dflow)  {this.Dflow = dflow;} Public long getsumflow ()  {return sumflow;} Public void setsumflow (Long sumflow)  {this.sumflow = sumflow;} /** *  the deserialization method, when deserializing, the order of the individual fields read from the stream should be consistent with the order in which they were written  */@Overridepublic  void readfields (datainput in)  throws ioexception {upflow = in.readlong ();d flow =  In.readlong (); Sumflow = in.readlong ();         }/** *   Method of serialization  */@Overridepublic  void write (dataoutput out)  throws ioexception  {out.writelong (Upflow); Out.writelong (dflow);//You can consider not serializing total traffic, because total traffic is calculated by upstream traffic and downstream traffic Out.writelong (Sumflow) ;} @Overridepublic  string tostring ()  { return  upflow +  "\ T"  +  dflow +  "\ t"  + sumflow;}}



4.2. Create a Mapper class

import java.io.ioexception;import org.apache.commons.lang.stringutils;import  org.apache.hadoop.io.longwritable;import org.apache.hadoop.io.text;import  org.apache.hadoop.mapreduce.mapper;public class flowcountmapper extends mapper< longwritable, text, text, flowbean> {@Overrideprotected  void map (LongWritable  key, text value, context context)  throws IOException,  interruptedexception {//  will pass in this line of data to stringstring line = value.tostring ();//  Slice this line, get the fields String[] fields = stringutils.split (lines, "\ t");//  get the phone number string phone  = fields[1];long upflow = long.parselong (fields[fields.length - 3]);long  Dflow = long.parselong (fields[fields.length - 2]); Flowbean bean = new flowbean (Upflow, dflow); Context.write (New Text (phone),  Bean);}}



4.3. Create a reducer class

Import Java.io.ioexception;import Org.apache.hadoop.io.text;import Org.apache.hadoop.mapreduce.reducer;public Class Flowcountreducer extends Reducer<text, Flowbean, text, flowbean>{@Overrideprotected void reduce (text key, Iterable<flowbean> beans, Context context) throws IOException, Interruptedexception {//First define two counters long upamount = 0; Long Damount = 0;//iterates through all of the user's traffic beans, accumulates sum for (Flowbean Bean:beans) {upamount + = Bean.getupflow ();d Amount + = Bean.getdflow ();} Constructs a flowbeanflowbean Countbean = new Flowbean (Upamount, Damount) for outputting the final result;//output Context.write (key, Countbean);}}




4.4. Create a commit run class

import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.path;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.output.fileoutputformat;public class flowcountdriver {public  static void main (String[] args)  throws exception {configuration conf  = new configuration (); Job job = job.getinstance (conf); Job.setjarbyclass (Flowcountdriver.class); Job.setMapperClass ( Flowcountmapper.class) Job.setreducerclass (flowcountreducer.class);/** *  if the output kv type of map and reduce are the same, The output KV type  *//* * job.setmapoutputkeyclass (text.class) of the map is not specifically set; *  Job.setmapoutputvalueclass (Flowbean.class);  */job.setoutputkeyclass (Text.class); Job.setOutputValueClass ( Flowbean.class), the default input and output components in/** * hadoop are Textinputformat and Textoutputformat, so the twoSentence code can also be omitted  *//* * job.setinputformatclass (Textinputformat.class); *  Job.setoutputformatclass (Textoutputformat.class);  */fileinputformat.setinputpaths (Job, new Path (Args[0])); Fileoutputformat.setoutputpath (Job, new path (args[1));boolean res =  Job.waitforcompletion (TRUE); System.exit (res ? 0 : 1);}}



Nine (a): Mobile Internet traffic Summary sum

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.