Nine (three): Mobile Internet traffic Summary sum (Sort Method II)

Source: Internet
Author: User

import java.io.datainput;import java.io.dataoutput;import java.io.ioexception;import  org.apache.hadoop.io.writablecomparable;public class flowbean implements  writablecomparable<flowbean>{//phone number private string phonenb;//upload traffic private long flow_up ;//Download Traffic private long flow_down;//total traffic private long flow_sum;public string  Getphonenb ()  {return phonenb;} Public void setphonenb (String phonenb)  {this.phonenb = phonenb;} PUBLIC&NBSP;LONG&NBSP;GETFLOW_UP ()  {return flow_up;} Public void setflow_up (LONG&NBSP;FLOW_UP)  {this.flow_up = flow_up;} Public long getflow_down ()  {return flow_down;} Public void setflow_down (Long flow_down)  {this.flow_down = flow_down;} Public long getflow_sum ()  {return flow_sum;} Public void setflow_sum (long flow_sum)  {this.flow_sum = flow_sum;} Note: In Hadoop use, if you want to use a constructive method with a parameter, the construction method of the parameterless must be written. Public flowbean ()  {}//method for the construction of the parameter Public flowbean (string phonenb, long flow_up,  Long flow_down)  {this.phonenb = phonenb;this.flow_up = flow_up;this.flow_down  = flow_down;this.flow_sum = flow_up+flow_down;} /** *  serialized objects: Converting structured objects to byte streams  */@Overridepublic  void write (dataoutput out)   Throws ioexception {out.writeutf (PHONENB); Out.writelong (FLOW_UP); Out.writelong (flow_down); o Ut.writelong (flow_sum);} /** *  Deserialization object: Stream the word to a structured object  */@Overridepublic  void readfields (datainput in)   Throws ioexception {this.phonenb = in.readutf (); This.flow_up = in.readlong (); This.flow_down = in.readlong (); This.flow_sum = in.readlong ();} @Overridepublic  string tostring ()  {return  ""  + flow_up +  "\ T"  +  flow_down+  "\ T"  + flow_sum +  "";} @Overridepublic  int compareto (Flowbean bean)  {return this.flow_sum >  Bean.getflow_sum ()  ? -1 : 1;}}
import java.io.ioexception;import java.util.uuid;import org.apache.commons.lang.stringutils; import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.path;import  org.apache.hadoop.io.longwritable;import org.apache.hadoop.io.nullwritable;import  org.apache.hadoop.io.text;import org.apache.hadoop.mapred.partitioner;import  org.apache.hadoop.mapred.lib.hashpartitioner;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;import cn.com.hadoop.mr.flowsum.flowbean;public  class flowsumsortrunner {public static void main (String[] args)   Throws exception {job job = job.getinstance (New configuration ()); Job.setjarbyclass (Flowsumsortrunner.class); JoB.setmapperclass (Flowsumsortmapper.class); Job.setreducerclass (Flowsumsortreducer.class); Job.setmapoutputkeyclass (Flowbean.class); Job.setmapoutputvalueclass (Nullwritable.class); Job.setOutputKeyClass ( Text.class); Job.setoutputvalueclass (flowbean.class);//Enter the first unsorted result, Then the execution of the MapReduce program is sorted fileinputformat.setinputpaths (Job, new path ("hdfs://xxx:9000/flowsum/ part-r-00000 ")); Fileoutputformat.setoutputpath (Job, new path ("Hdfs://xxx:9000/" +uuid.randomuuid ())); Job.waitforcompletion (TRUE);} Public static class flowsumsortmapper extends mapper<longwritable, text,  flowbean, nullwritable>{@Overrideprotected  void map (longwritable key,text  Value,mapper<longwritable, text, flowbean, nullwritable>. Context context) throws ioexception, interruptedexception {string line =  Value.tostring (); String[] split = stringutils.split (line,  "\ t"); Context.write (New flowbean (Split[0], long.parselong (split[1]),  long.parselong (split[2])),  Nullwritable.get ());}} Public static class flowsumsortreducer extends reducer<flowbean, nullwritable ,  text, flowbean>{@Overrideprotected  void reduce (flowbean key, iterable< Nullwritable> values,reducer<flowbean, nullwritable, text, flowbean>. Context context) Throws ioexception, interruptedexception {context.write (New Text ( Key.getphonenb ()),  key);}}}


Nine (three): Mobile Internet traffic Summary sum (Sort Method II)

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.