mapreduce 根据value值进行排序

目前所知有两种方法

1.map阶段不做改变,在reduce阶段对map的输出进行缓存,重写cleanup方法,在其中对缓存的数据进行排序输出。
缺点:如果数据量过大,将消耗大量的内存

2.进行两个Mapreduce操作
将第一个次Mapreduce的输出value作为第二次map的key
,在第二次reduce再还原成原来的key value形式
如下为按照手机号产生流量的value进行排序的java代码示例

package com.yyx.mapr.flow;

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.output.FileOutputFormat;

import java.io.IOException;

public class FlowSort {
    static  class FlowSortMapper extends Mapper<LongWritable,Text,FlowBean,Text> {
        private FlowBean flowBean = new FlowBean();
        private Text text = new Text();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] words = line.split("\t");
            String phoneNum = words[0];

            flowBean.set(Long.parseLong(words[1]), Long.parseLong(words[2]));
            text.set(phoneNum);

            context.write(flowBean, text);

        }
    }


    static class FlowSortReducer extends Reducer<FlowBean, Text, Text, FlowBean> {
        @Override
        protected void reduce(FlowBean key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            context.write(values.iterator().next(), key);
        }
    }


    public static void main(String[] args) throws Exception {


        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf);


        job.setJarByClass(FlowSort.class);
        //设置调用类
        job.setMapperClass(FlowSort.FlowSortMapper.class);

        job.setReducerClass(FlowSort.FlowSortReducer.class);

        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(Text.class);


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



        //指定输入输入输出路径

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //将job中配置的相关参数,以及job所用的java类所用的jar包,提交给yarn去运行
        //job.submit();

        boolean res = job.waitForCompletion(true);

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

}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值