Statistical analysis of mobile phone traffic based on MapReduce

Source: Internet
Author: User

1, code
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;/** * Use arraywritable*/ Public classTRAFFICAPP4 { Public Static voidMain (string[] args) throws exception{Configuration conf=NewConfiguration (); 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,NewPath (args[1])); Job.waitforcompletion (true); }             Public Static classTrafficmapper extends Mapper<longwritable, text, text, longarraywritable>{@Overrideprotected voidmap (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=NewText (PhoneNumber); Longarraywritable v2=Newlongarraywritable (Uppacknum, Downpacknum, Uppayload, downpayload);        Context.write (K2, V2); }    }         Public Static classTrafficreducer extends Reducer<text, longarraywritable, Text, longarraywritable>{@Overrideprotected voidReduce (Text K2, iterable<longarraywritable>V2s, Reducer<text, Longarraywritable, Text, longarraywritable>. Context context) throws IOException, interruptedexception {LongUppacknum =0L; LongDownpacknum =0L; LongUppayload =0L; LongDownpayload =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=Newlongarraywritable (Uppacknum, Downpacknum, Uppayload, downpayload);        Context.write (K2, V3); }    }         Public Static classLongarraywritable extends arraywritable{/** * When calling, call the method first, then call set (writable[])*/         Publiclongarraywritable () {super (longwritable.class); }        /** * Call this method directly * @param values*/         Publiclongarraywritable (longwritable[] values) {super (longwritable.class, values); }        /** * Call this method directly * @param uppacknum * @param downpacknum * @param uppayload * @pa Ram Downpayload*/         Publiclongarraywritable (Long uppacknum, long downpacknum, long uppayload, long Downpayload) {super (longwritable .class); Longwritable[] Values=Newlongwritable[4]; values[0] =Newlongwritable (Uppacknum); values[1] =Newlongwritable (Downpacknum); values[2] =Newlongwritable (uppayload); values[3] =Newlongwritable (downpayload); Super.Set(values); }        /** * Call this method directly * @param uppacknum * @param downpacknum * @param uppayload * @pa Ram Downpayload*/         Publiclongarraywritable (String uppacknum, String downpacknum, String uppayload, String downpayload) {Super (Long Writable.class); Longwritable[] Values=Newlongwritable[4]; values[0] =Newlongwritable (Long.parselong (uppacknum)); values[1] =Newlongwritable (Long.parselong (downpacknum)); values[2] =Newlongwritable (Long.parselong (uppayload)); values[3] =Newlongwritable (Long.parselong (downpayload)); Super.Set(values); } @Override PublicString toString () {string[] array=super.tostrings (); returnStringutils.join (Array,"\ t"); }    }    }

2,arraywritable's APIOrg.apache.hadoop.io
Class arraywritable
Java.lang.Object  
Org.apache.hadoop.io.ArrayWritable
Implemented interfaces:
writable
arraywritableextends objectimplements writable

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

Construction Method Summary
ArrayWritable(Class<? extends Writable> valueClass)
ArrayWritable(Class<? extends Writable> valueClass, Writable[] values)
ArrayWritable(String[] strings)
Method Summary
 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

Construction Method Details
Arraywritable
arraywritable (class< extends writable> valueclass)
Arraywritable
arraywritable (class< extends writable> Valueclass,                     Writable[] values)
Arraywritable
arraywritable (string[] strings)
Method details
Getvalueclass
Getvalueclass ()
Tostrings
tostrings ()
ToArray
ToArray ()
Set
Set (writable[] values)
Get
Get ()
ReadFields
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:
readFieldsIn interface Writable
Parameters:
in-To DataInput deseriablize the object from.
Throws:
IOException
Write
Write (DataOutput out)           Throws IOException
Description copied from interface: Writable
Serialize The fields of this
object to out .

Specified by:
writeIn interface Writable
Parameters:
out-to DataOuput serialize the object into.
Throws:
IOException

 

Statistical analysis of mobile phone traffic based on 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.