FlowSumSort

package cn.itcast.mapreduce;


import java.io.IOException;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;




/**
 * 实现流量汇总并按照流量大小的倒序排序  前提:处理的数据是已经汇总的结果文件
 * @author AllenWoon
 *
 */
public class FlowSumSort {

public static class FlowSumSortMapper extends Mapper<LongWritable, Text, FlowBean, Text>{

FlowBean k = new FlowBean();
Text  v = new Text();

@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {

String line = value.toString();

String[] fields = line.split("\t");

String phoNum = fields[0];
long upFlowSum = Long.parseLong(fields[1]);
long downFlowSum = Long.parseLong(fields[2]);

k.set(upFlowSum, downFlowSum);
v.set(phoNum);

context.write(k, v);
}

}

public static class FlowSumSortReducer extends Reducer<FlowBean, Text, Text, FlowBean>{

@Override
protected void reduce(FlowBean bean, Iterable<Text> PhoneNum, Context context)
throws IOException, InterruptedException {

context.write(PhoneNum.iterator().next(), bean);
}
}

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);

job.setJarByClass(FlowSumSort.class);

//告诉程序,我们的程序所用的mapper类和reducer类是什么
job.setMapperClass(FlowSumSortMapper.class);
job.setReducerClass(FlowSumSortReducer.class);

//告诉框架,我们程序输出的数据类型
job.setMapOutputKeyClass(FlowBean.class);
job.setMapOutputValueClass(Text.class);

job.setOutputKeyClass(Text.class);
job.setOutputValueClass(FlowBean.class);

//告诉框架,我们程序使用的数据读取组件 结果输出所用的组件是什么
//TextInputFormat是mapreduce程序中内置的一种读取数据组件  准确的说 叫做 读取文本文件的输入组件
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);

//告诉框架,我们要处理的数据文件在那个路劲下
FileInputFormat.setInputPaths(job, new Path("/flowsum/output"));

//告诉框架,我们的处理结果要输出到什么地方
FileOutputFormat.setOutputPath(job, new Path("/flowsum/outputsort"));

boolean res = job.waitForCompletion(true);

System.exit(res?0:1);
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值