Write the Phoneflow program, calculate the mobile phone upstream traffic, downstream traffic and total traffic, the data is as follows:
13685295623 122 201
13985295600 102 11
13885295622 22 101
13785295633 120 20
1, Flowmapper:
Package com.hadoop.flow;
Import java.io.IOException;
Import org.apache.hadoop.io.LongWritable;
Import Org.apache.hadoop.io.Text;
Import Org.apache.hadoop.mapreduce.Mapper;
Import Org.apache.commons.lang.StringUtils;
public class Flowmapper extends mapper<longwritable, text, text, flowbean>{
/**
* Data format:
* 13685295623 122 201
* 13985295600 102 11
* 13885295622 22 101
* 13785295633 120 20
*/
@Override
protected void Map (longwritable key, Text Value,context Context) throws IOException, Interruptedexception {
String line=value.tostring ();
String [] Fields=stringutils.split (line, "\ t");
String Phonenb=fields[0];
Long up_flow=long.valueof (fields[1]);
Long d_flow=long.valueof (fields[2]);
Context.write (New Text (PHONENB), New Flowbean (Phonenb,up_flow,d_flow));
}
}
2, Flowreducer:
Package com.hadoop.flow;
Import java.io.IOException;
Import Org.apache.hadoop.io.Text;
Import Org.apache.hadoop.mapreduce.Reducer;
public class Flowreducer extends Reducer<text, Flowbean,text, flowbean> {
@Override
protected void reduce (Text key, iterable<flowbean> Values,context Context) throws IOException, interruptedexception {
Long upflowc=0;
Long dflowd=0;
for (Flowbean bean:values) {
Upflowc+=bean.getup_flow ();
Dflowd+=bean.getd_flow ();
}
Context.write (Key,new Flowbean (key.tostring (), upflowc,dflowd));
}
}
3, Flowrunner
Package com.hadoop.flow;
Import org.apache.hadoop.conf.Configuration;
Import org.apache.hadoop.conf.Configured;
Import Org.apache.hadoop.fs.Path;
Import Org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
Import Org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
Import Org.apache.hadoop.mapreduce.Job;
Import Org.apache.hadoop.util.Tool;
Import Org.apache.hadoop.util.ToolRunner;
Import Org.apache.hadoop.io.Text;
public class Flowrunner extends configured implements tool{
public int run (string[] args) throws Exception {
Configuration conf=new configuration ();
Job job=job.getinstance (conf);
Job.setjarbyclass (Flowrunner.class);
Job.setmapperclass (Flowmapper.class);
Job.setreducerclass (Flowreducer.class);
Job.setoutputkeyclass (Text.class);
Job.setoutputvalueclass (Flowbean.class);
Job.setmapoutputkeyclass (Text.class);
Job.setmapoutputvalueclass (Flowbean.class);
Fileinputformat.setinputpaths (Job,new Path (args[0]));
Fileoutputformat.setoutputpath (Job,new Path (args[1]));
Return Job.waitforcompletion (True)? 0:1;
}
public static void Main (string[] args) throws Exception {
Toolrunner.run (New Configuration (), New Flowrunner (), args);
}
}
4, Flowbean:
Package com.hadoop.flow;
Import Java.io.DataInput;
Import Java.io.DataOutput;
Import java.io.IOException;
Import org.apache.hadoop.io.Writable;
public class Flowbean implements writable{
Private String Phonenb;
Private long Up_flow;
Private long D_flow;
Private long S_flow;
Public Flowbean () {
}
Public Flowbean (String phonenb,long up_flow,long d_flow) {
This.phonenb=phonenb;
This.up_flow=up_flow;
This.d_flow=d_flow;
This.s_flow=up_flow+d_flow;
}
Public String Getphonenb () {
return PHONENB;
}
public void Setphonenb (String phonenb) {
This.phonenb = Phonenb;
}
Public long Getup_flow () {
return up_flow;
}
public void Setup_flow (long up_flow) {
This.up_flow = Up_flow;
}
Public long Getd_flow () {
return d_flow;
}
public void Setd_flow (long d_flow) {
This.d_flow = D_flow;
}
Public long Gets_flow () {
return s_flow;
}
public void Sets_flow (long s_flow) {
This.s_flow = S_flow;
}
//
public void Write (DataOutput out) throws IOException {
Out.writeutf (PHONENB);
Out.writelong (Up_flow);
Out.writelong (D_flow);
Out.writelong (S_flow);
}
public void ReadFields (Datainput in) throws IOException {
phonenb= In.readutf ();
Up_flow=in.readlong ();
D_flow=in.readlong ();
S_flow=in.readlong ();
}
@Override
Public String toString () {
Return up_flow+ "" +d_flow+ "" + "" +s_flow;
}
}
Hadoop MapReduce (Flowcount) Java programming