MapReduce实现TopK算法(实战)

7 篇文章 0 订阅

习读书之业,便当知读书之乐
存为善之心,不必邀为善之名

项目介绍

  • 给定数据集data.csv,内含三列,格式如下
编号时间风速
WT23002015/9/1 0:003.34
WT23002015/9/1 0:103.12
WT23012015/6/18 6:000.73
WT23022015/11/30 23:503.32
  • 求不同编号的风机每个月风速排名前十
  • 需要得到的结果如下
风机编号-月份排名前十的风速
WT023002015117.41,16.81,16.55,16.4,15.63,15.59,15.53,15.44,15.29,15.12
WT023002015217.42,16.82,16.79,16.5,16.48,16.35,16.1,16.07,16.05,15.99
WT0230220151216.54,16.4,16.06,15.96,15.56,15.48,15.47,15.46,15.13,15.11

数据集下载

data.csv 提取码76h4

程序下载

代码讲解

  • FanData.java
    该类在Map阶段从按行读入的数据解析出FanData对象
package cn.neu.blog.topk;

public class FanData {
	
	private String fanNo;

	private String time;

	private String windSpeed;
	
	public void getInstance(String row) {
		String[] cols = row.split(",");
		this.fanNo = cols[0];
		this.time = cols[1];
		this.windSpeed = cols[2];
	}

	public String getFanNo() {
		return fanNo;
	}

	public void setFanNo(String fanNo) {
		this.fanNo = fanNo;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getWindSpeed() {
		return windSpeed;
	}

	public void setWindSpeed(String windSpeed) {
		this.windSpeed = windSpeed;
	}
	
	@Override
	public String toString() {
		return "fanNo=" + this.fanNo + ",time=" + this.time + ",windSpeed=" + this.windSpeed;
	}
}
  • TopK.java
    • Map阶段
      Map阶段对从数据集中按行读入的数据进行解析,产生fanData对象,获得其风机编号、时间和风速,然后对时间进行分割,仅取年份和月份,将风机编号与分割后的时间进行连接,作为Map输出阶段的Key
      注意:这里将每一行读入的数据都输出了进入Shuffle阶段,程序运行效率低,而有些数据在Map阶段就可以判断不可能是前K的数据,可以在此阶段尝试使用TreeMap,自己动动脑筋~
public static class TopKMapper extends Mapper<Object, Text, Text, DoubleWritable> {
        private FanData fanData = new FanData();
        private static final int K = 10;

        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            fanData.getInstance(value.toString());
            String fanNo = fanData.getFanNo();
            String time = fanData.getTime();
            time = time.substring(0,7).replace("/", "");
            String strKey = fanNo + time;
            double windSpeed = Double.parseDouble(fanData.getWindSpeed());
            context.write(new Text(strKey), new DoubleWritable(windSpeed));
        }
    }
  • Reduce阶段
    该阶段需要实例化一个TreeMap对象,用于对风速进行排序
    注意:该对象能对Double进行排序,但不能对DoubleWritable进行排序
    将Key对应的Values依次添加到TreeMap中,控制TreeMap的大小始终不超过K,若超过则将TreeMap第一个元素(最小的)剔除
public static class TopKReducer extends Reducer<Text, DoubleWritable, Text, Text> {
        private static final int K = 10;
        TreeMap<Double, Double> tm = new TreeMap<Double, Double>();
        StringBuffer sb = new StringBuffer();

        protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
            tm = new TreeMap<Double, Double>();
            sb = new StringBuffer();
            for(DoubleWritable val:values){
                tm.put(val.get(), val.get());
                if(tm.size() > K){
                    tm.remove(tm.firstKey());
                }
            }
            for(Double ws : tm.descendingKeySet()) sb.append(ws).append(",");
            context.write(key, new Text(sb.toString().substring(0, sb.toString().lastIndexOf(","))));
        }
    }
  • 主函数
    使用IDE运行该程序时需要设置参数
    otherArgs[0]为输入路径
    otherArgs[1]为输出路径
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        System.setProperty("HADOOP_USER_NAME", "root");
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.newInstance(conf);
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        if(otherArgs.length != 2){
            System.err.println("Usage: topk <in> <out>");
            System.exit(2);
        }
        Path outPath = new Path(otherArgs[1]);
        if(fs.exists(outPath)){
            fs.delete(outPath, true);
        }
        Job job = Job.getInstance(conf, "topk");
        job.setJarByClass(TopK.class);
        job.setMapperClass(TopKMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(DoubleWritable.class);
        job.setReducerClass(TopKReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }

运行结果示例

TopK运行结果

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值