Hadoop在Bean对象不实现WritableComparable接口的情况下用自定义bean作为Mapper KEYOUT TopN全排序(RawComparator实现)

Bean

package com.zhengkw.rawcomparabletest;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.file.tfile.RawComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @ClassName:FlowBean
 * @author: zhengkw
 * @description:
 * @date: 20/03/02下午 8:09
 * @version:1.0
 * @since: jdk 1.8
 */
public class FlowBean implements Writable {

    //上行数据
    private Long upFlow;
    //下行数据
    private Long downFlow;
    //总量
    private Long totalFlow;


    public FlowBean() {
    }

    public void setUpFlow(Long upFlow) {
        this.upFlow = upFlow;
    }

    public void setDownFlow(Long downFlow) {
        this.downFlow = downFlow;
    }

    public void setTotalFlow(Long totalFlow) {
        this.totalFlow = totalFlow;
    }

    public Long getUpFlow() {
        return upFlow;
    }

    public Long getDownFlow() {
        return downFlow;
    }

    public Long getTotalFlow() {
        return totalFlow;
    }

    public void set(Long upFlow, Long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.totalFlow = upFlow + downFlow;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(upFlow);
        out.writeLong(downFlow);
        out.writeLong(totalFlow);
    }

    @Override
    public void readFields(DataInput in) throws IOException {

        this.upFlow = in.readLong();
        this.downFlow = in.readLong();
        this.totalFlow = in.readLong();
    }

    @Override
    public String toString() {
        return upFlow +
                "\t" + downFlow +
                "\t" + totalFlow;
    }
}

RawComparator实现类

package com.zhengkw.rawcomparabletest;

import org.apache.hadoop.io.DataInputBuffer;
import org.apache.hadoop.io.RawComparator;

import java.io.IOException;

/**
 * @ClassName:RawComparatorTest
 * @author: zhengkw
 * @description:
 * @date: 20/03/02下午 8:17
 * @version:1.0
 * @since: jdk 1.8
 */
public class RawComparatorTest implements RawComparator<FlowBean> {

    FlowBean flowBean1 = new FlowBean();
    FlowBean flowBean2 = new FlowBean();
    DataInputBuffer inputBuffer = new DataInputBuffer();

    @Override
    public int compare(FlowBean o1, FlowBean o2) {

/*
        if (o1.getTotalFlow() > o2.getTotalFlow()) {
            return -1;
        } else if (o1.getTotalFlow() == o2.getTotalFlow()) {
            return 0;

        } else return 1;
*/
//简化
return - o1.getTotalFlow().compareTo(o2.getTotalFlow());
    }

    /**
     * Compare two objects in binary.
     * b1[s1:l1] is the first object, and b2[s2:l2] is the second object.
     *
     * @param b1 The first byte array.
     * @param s1 The position index in b1. The object under comparison's starting index.
     * @param l1 The length of the object in b1.
     * @param b2 The second byte array.
     * @param s2 The position index in b2. The object under comparison's starting index.
     * @param l2 The length of the object under comparison in b2.
     * @return An integer result of the comparison.
     */
    @Override
    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {

        try {
            inputBuffer.reset(b1, s1, l1);
            flowBean1.readFields(inputBuffer);
            inputBuffer.reset(b2, s2, l2);
            flowBean2.readFields(inputBuffer);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return compare(flowBean1, flowBean2);
    }


}

Mapper

package com.zhengkw.rawcomparabletest;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @ClassName:FlowMapper
 * @author: zhengkw
 * @description:
 * @date: 20/03/02下午 8:08
 * @version:1.0
 * @since: jdk 1.8
 */
public class FlowMapper extends Mapper<LongWritable, Text, FlowBean, NullWritable> {


    FlowBean k = new FlowBean();

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

        String line = value.toString();
        String[] info = line.split("\t");

        Long upFlow = Long.parseLong(info[info.length - 3]);
        Long downFlow = Long.parseLong(info[info.length - 2]);

        k.set(upFlow, downFlow);
        context.write(k, NullWritable.get());

    }
}

Reducer

package com.zhengkw.rawcomparabletest;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @ClassName:FlowReducer
 * @author: zhengkw
 * @description:
 * @date: 20/03/02下午 8:09
 * @version:1.0
 * @since: jdk 1.8
 */
public class FlowReducer extends Reducer<FlowBean, NullWritable, FlowBean, NullWritable> {



    @Override
    protected void reduce(FlowBean key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {


        context.write(key, NullWritable.get());
    }


}

Driver

package com.zhengkw.rawcomparabletest;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
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;

/**
 * @ClassName:FlowDriver
 * @author: zhengkw
 * @description:
 * @date: 20/03/02下午 8:09
 * @version:1.0
 * @since: jdk 1.8
 */
public class FlowDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Path input = new Path("F:\\mrinput\\sort");
        Path output = new Path("F:/output5");


        Configuration conf = new Configuration();
        //conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR,"\t");
        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(output)) {
            fs.delete(output, true);
        }

        //反射创建对象
        Job job = Job.getInstance(conf);
        //给job指定RawComparator比较器
        job.setSortComparatorClass(RawComparatorTest.class);

        //job.setNumReduceTasks(1);

        //设置3个类
        job.setJarByClass(FlowDriver.class);
        job.setMapperClass(FlowMapper.class);
        job.setReducerClass(FlowReducer.class);

        //设置2个输入输出
        // job.setMapOutputKeyClass(N.class);
        // job.setMapOutputValueClass(FlowBean.class);
//        指定最终输出的数据的kv类型
        job.setOutputKeyClass(FlowBean.class);
        job.setOutputValueClass(NullWritable.class);

//       job.setInputFormatClass(KeyValueTextInputFormat.class);
        //指定输入输出路径
        FileInputFormat.setInputPaths(job, input);
        FileOutputFormat.setOutputPath(job, output);

        //将job中配置的相关参数,以及job所用的java类所在的jar包, 提交给yarn去运行
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}

测试数据

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

结果

7335	110349	117684
11058	48243	59301
3597	25635	29232
2481	24681	27162
9531	2412	11943
6960	690	7650
3659	3538	7197
3008	3720	6728
3156	2936	6092
918	4938	5856
4116	1432	5548
1938	2910	4848
1527	2106	3633
1938	180	2118
1116	954	2070
132	1512	1644
180	180	360
264	0	264
240	0	240
120	120	240
240	0	240

遇到的问题

1.输出完的结果条数偏少

bug代码

   @Override
    public int compare(FlowBean o1, FlowBean o2) {


   return o1.getTotalFlow() > o2.getTotalFlow()?-1:0;
    }

修正以后

   @Override
    public int compare(FlowBean o1, FlowBean o2) {


        if (o1.getTotalFlow() > o2.getTotalFlow()) {
            return -1;
        } else if (o1.getTotalFlow() == o2.getTotalFlow()) {
            return 0;

        } else return 1;

    }

compato比较条件不够细化。
2.EOFException在这里插入图片描述

原因 :
DataInputStream中的readFully方法中的count<0导致如下图

在这里插入图片描述
上图count是调用了InputStream中的read(3个参数)方法来获取的,当没有数据读到(即空行)返回 -1
在这里插入图片描述
解决办法:
清洗数据源,删掉空行即可!

注意事项

在这里插入图片描述在Driver类中没设置sortComparator会出现报错

源码出处
在这里插入图片描述若不是writableComparable的实现类,则会出现该异常。因为这个是默认值,所以需要指定sortComparator

  //给job指定RawComparator比较器
        job.setSortComparatorClass(RawComparatorTest.class);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值