FlowBean类
import org.apache.hadoop.io.WritableComparable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
class FlowBean implements WritableComparable<FlowBean> {
private int sumFLow;
private int upFLow;
private int downFLow;
@Override
public String toString() {
return upFLow +"\t" + downFLow +
"\t" + sumFLow ;
}
public FlowBean(int sumFLow, int upFLow, int downFLow) {
this.sumFLow = sumFLow;
this.upFLow = upFLow;
this.downFLow = downFLow;
}
public FlowBean() {
}
public int getUpFLow() {
return upFLow;
}
public void setUpFLow(int upFLow) {
this.upFLow = upFLow;
}
public int getDownFLow() {
return downFLow;
}
public void setDownFLow(int downFLow) {
this.downFLow = downFLow;
}
public int getSumFLow() {
return sumFLow;
}
public void setSumFLow(int sumFLow) {
this.sumFLow = sumFLow;
}
@Override
public void write(DataOutput dataOutput) throws IOException {
dataOutput.writeInt(this.upFLow);
dataOutput.writeInt(this.downFLow);
dataOutput.writeInt(this.sumFLow);
}
@Override
public void readFields(DataInput dataInput) throws IOException {
upFLow = dataInput.readInt();
downFLow = dataInput.readInt();
sumFLow = dataInput.readInt();
}
@Override
public int compareTo(FlowBean o) {
int result;
result = Integer.compare(o.getSumFLow(), this.sumFLow);
if(result == 0){
result = this.downFLow > o.getDownFLow() ? -1 : 1;
}
return result;
}
}
FlowTopNMapper类
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
public class FlowTopNMapper extends Mapper<LongWritable, Text,FlowBean,Text> {
Text k = new Text();
FlowBean flowBean = new FlowBean();
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] splits = line.split("\t");
k.set(splits[0]);
flowBean.setUpFLow(Integer.parseInt(splits[1]));
flowBean.setDownFLow(Integer.parseInt(splits[2]));
flowBean.setSumFLow(Integer.parseInt(splits[3]));
context.write(flowBean,k);
}
}
FlowTopNReducer类
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class FlowTopNReducer extends Reducer<FlowBean, Text, Text, FlowBean> {
int i = 0;
@Override
protected void reduce(FlowBean key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
if (i < 10) {
for (Text v : values) {
context.write(v, key);
}
i++;
}
}
}
FlowTopNDriver类
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;
import java.io.IOException;
public class FlowTopNDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Job job = Job.getInstance();
job.setMapOutputKeyClass(FlowBean.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(FlowBean.class);
job.setMapperClass(FlowTopNMapper.class);
job.setReducerClass(FlowTopNReducer.class);
FileInputFormat.setInputPaths(job,new Path("F:\\test\\top10\\top10.txt"));
FileOutputFormat.setOutputPath(job,new Path("F:\\test\\top10\\top10out"));
job.waitForCompletion(true);
}
}
要分析的文件内容
13470253144 180 180 360
13509468723 7335 110349 117684
13560439638 918 4938 5856
13568436656 3597 25635 29232
13590439668 1116 954 2070
13630577991 6960 690 7650
13682846555 1938 2910 4848
13729199489 240 0 240
13736230513 2481 24681 27162
13768778790 120 120 240
13846544121 264 0 264
13956435636 132 1512 1644
13966251146 240 0 240
13975057813 11058 48243 59301
13992314666 3008 3720 6728
15043685818 3659 3538 7197
15910133277 3156 2936 6092
15959002129 1938 180 2118
18271575951 1527 2106 3633
18390173782 9531 2412 11943
84188413 4116 1432 5548