mapreduce 自定义类型

主要是重写write 与 readFields 两个方法。
最好要把toString也重写了,因为我当是没有重写,结果保存到hdfs上的是自定义对象的引用地址。这个还不确定到底是不是没有重写toString造成的。先记下吧。


import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.PrintStream;
import org.apache.hadoop.io.Writable;

public class CinemaWrite implements Writable {
    String value = "";

    String index = "";

    Double num_null = Double.valueOf(0.0D);

    String type = "";

    Double num_string = Double.valueOf(0.0D);

    Double num_string_value_equals_null = Double.valueOf(0.0D);

    Double num_string_length_equals_zero = Double.valueOf(0.0D);

    Double num_string_length_max = Double.valueOf(0.0D);

    Double num_string_length_min = Double.valueOf(0.0D);

    Double num_numberic_sum = Double.valueOf(0.0D);

    Double num_numeric = Double.valueOf(0.0D);

    Double num_numeric_media = Double.valueOf(0.0D);

    Double num_numeric_mean = Double.valueOf(0.0D);

    Double num_numeric_max = Double.valueOf(0.0D);

    Double num_numeric_min = Double.valueOf(0.0D);

    Double num_numeric_variance = Double.valueOf(0.0D);

    Double num_numeric_mode = Double.valueOf(0.0D);

    public CinemaWrite() {
    }

    public CinemaWrite(String value, String sum, String num_string,
            String num_null, String num_string_length_max,
            String num_string_length_min, String null_num, String media,
            String mean, String max, String min) {
        this.index = value;
        this.num_numberic_sum = Double.valueOf(Double.parseDouble(sum));
        this.num_string = Double.valueOf(Double.parseDouble(num_string));
        this.num_string_value_equals_null = Double.valueOf(Double
                .parseDouble(num_null));
        this.num_string_length_max = Double.valueOf(Double
                .parseDouble(num_string_length_max));
        this.num_string_length_min = Double.valueOf(Double
                .parseDouble(num_string_length_min));
        this.num_numeric_media = Double.valueOf(Double.parseDouble(media));
        this.num_numeric_mean = Double.valueOf(Double.parseDouble(mean));
        this.num_numeric_max = Double.valueOf(Double.parseDouble(max));
        this.num_numeric_min = Double.valueOf(Double.parseDouble(min));
    }

    public void write(DataOutput out) throws IOException {
        out.writeUTF(this.value);
        out.writeUTF(this.index);
        out.writeDouble(this.num_null.doubleValue());
        out.writeUTF(this.type);

        out.writeDouble(this.num_string.doubleValue());
        out.writeDouble(this.num_string_value_equals_null.doubleValue());
        out.writeDouble(this.num_string_length_equals_zero.doubleValue());
        out.writeDouble(this.num_string_length_max.doubleValue());
        out.writeDouble(this.num_string_length_min.doubleValue());

        out.writeDouble(this.num_numberic_sum.doubleValue());
        out.writeDouble(this.num_numeric.doubleValue());
        out.writeDouble(this.num_numeric_media.doubleValue());
        out.writeDouble(this.num_numeric_mean.doubleValue());
        out.writeDouble(this.num_numeric_max.doubleValue());
        out.writeDouble(this.num_numeric_min.doubleValue());
        out.writeDouble(this.num_numeric_variance.doubleValue());
        out.writeDouble(this.num_numeric_mode.doubleValue());
    }

    public void readFields(DataInput in) throws IOException {
        this.value = in.readUTF();
        this.index = in.readUTF();
        this.num_null = Double.valueOf(in.readDouble());
        this.type = in.readUTF();

        this.num_string = Double.valueOf(in.readDouble());
        this.num_string_value_equals_null = Double.valueOf(in.readDouble());
        this.num_string_length_equals_zero = Double.valueOf(in.readDouble());
        this.num_string_length_max = Double.valueOf(in.readDouble());
        this.num_string_length_min = Double.valueOf(in.readDouble());

        this.num_numberic_sum = Double.valueOf(in.readDouble());
        this.num_numeric = Double.valueOf(in.readDouble());
        this.num_numeric_media = Double.valueOf(in.readDouble());
        this.num_numeric_mean = Double.valueOf(in.readDouble());
        this.num_numeric_max = Double.valueOf(in.readDouble());
        this.num_numeric_min = Double.valueOf(in.readDouble());
        this.num_numeric_variance = Double.valueOf(in.readDouble());
        this.num_numeric_mode = Double.valueOf(in.readDouble());
    }

    public boolean merge(CinemaWrite cw) {
        try {
            if ((this.index.length() > 0) && (this.index != cw.getIndex())) {
                System.err.println("index is not equal!");
                return false;
            }
            this.index = cw.getIndex();
            this.num_null = cw.getNum_null();
            this.type = cw.getType();
            CinemaWrite tmp56_55 = this;
            tmp56_55.num_string = Double.valueOf(tmp56_55.num_string
                    .doubleValue() + cw.getNum_string().doubleValue());
            CinemaWrite tmp78_77 = this;
            tmp78_77.num_string_value_equals_null = Double
                    .valueOf(tmp78_77.num_string_value_equals_null
                            .doubleValue()
                            + cw.getNum_string_value_equals_null()
                                    .doubleValue());
            CinemaWrite tmp100_99 = this;
            tmp100_99.num_string_length_equals_zero = Double
                    .valueOf(tmp100_99.num_string_length_equals_zero
                            .doubleValue()
                            + cw.getNum_string_length_equals_zero()
                                    .doubleValue());
            if (this.num_string_length_max.doubleValue() < cw
                    .getNum_string_length_max().doubleValue())
                this.num_string_length_max = cw.getNum_string_length_max();
            if (this.num_string_length_min.doubleValue() > cw
                    .getNum_string_length_min().doubleValue())
                this.num_string_length_min = cw.getNum_string_length_min();
            CinemaWrite tmp174_173 = this;
            tmp174_173.num_numberic_sum = Double
                    .valueOf(tmp174_173.num_numberic_sum.doubleValue()
                            + cw.getNum_numberic_sum().doubleValue());
            CinemaWrite tmp196_195 = this;
            tmp196_195.num_numeric = Double.valueOf(tmp196_195.num_numeric
                    .doubleValue() + cw.getNum_numeric().doubleValue());
            if (this.num_numeric_max.doubleValue() < cw.getNum_numeric_max()
                    .doubleValue())
                this.num_numeric_max = cw.getNum_numeric_max();
            if (this.num_numeric_min.doubleValue() > cw.getNum_numeric_min()
                    .doubleValue())
                this.num_numeric_min = cw.getNum_numeric_min();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String getValue() {
        return this.value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getIndex() {
        return this.index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public Double getNum_null() {
        return this.num_null;
    }

    public void setNum_null(Double num_null) {
        this.num_null = num_null;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Double getNum_string() {
        return this.num_string;
    }

    public void setNum_string(Double num_string) {
        this.num_string = num_string;
    }

    public Double getNum_string_value_equals_null() {
        return this.num_string_value_equals_null;
    }

    public void setNum_string_value_equals_null(
            Double num_string_value_equals_null) {
        this.num_string_value_equals_null = num_string_value_equals_null;
    }

    public Double getNum_string_length_equals_zero() {
        return this.num_string_length_equals_zero;
    }

    public void setNum_string_length_equals_zero(
            Double num_string_length_equals_zero) {
        this.num_string_length_equals_zero = num_string_length_equals_zero;
    }

    public Double getNum_string_length_max() {
        return this.num_string_length_max;
    }

    public void setNum_string_length_max(Double num_string_length_max) {
        this.num_string_length_max = num_string_length_max;
    }

    public Double getNum_string_length_min() {
        return this.num_string_length_min;
    }

    public void setNum_string_length_min(Double num_string_length_min) {
        this.num_string_length_min = num_string_length_min;
    }

    public Double getNum_numberic_sum() {
        return this.num_numberic_sum;
    }

    public void setNum_numberic_sum(Double num_numberic_sum) {
        this.num_numberic_sum = num_numberic_sum;
    }

    public Double getNum_numeric() {
        return this.num_numeric;
    }

    public void setNum_numeric(Double num_numeric) {
        this.num_numeric = num_numeric;
    }

    public Double getNum_numeric_media() {
        return this.num_numeric_media;
    }

    public void setNum_numeric_media(Double num_numeric_media) {
        this.num_numeric_media = num_numeric_media;
    }

    public Double getNum_numeric_mean() {
        return this.num_numeric_mean;
    }

    public void setNum_numeric_mean(Double num_numeric_mean) {
        this.num_numeric_mean = num_numeric_mean;
    }

    public Double getNum_numeric_max() {
        return this.num_numeric_max;
    }

    public void setNum_numeric_max(Double num_numeric_max) {
        this.num_numeric_max = num_numeric_max;
    }

    public Double getNum_numeric_min() {
        return this.num_numeric_min;
    }

    public void setNum_numeric_min(Double num_numeric_min) {
        this.num_numeric_min = num_numeric_min;
    }

    public Double getNum_numeric_variance() {
        return this.num_numeric_variance;
    }

    public void setNum_numeric_variance(Double num_numeric_variance) {
        this.num_numeric_variance = num_numeric_variance;
    }

    public Double getNum_numeric_mode() {
        return this.num_numeric_mode;
    }

    public void setNum_numeric_mode(Double num_numeric_mode) {
        this.num_numeric_mode = num_numeric_mode;
    }

    public String toString() {
        return "CinemaWrite [value=" + this.value + ", index=" + this.index
                + ", num_null=" + this.num_null + ", type=" + this.type
                + ", num_string=" + this.num_string
                + ", num_string_value_equals_null="
                + this.num_string_value_equals_null
                + ", num_string_length_equals_zero="
                + this.num_string_length_equals_zero
                + ", num_string_length_max=" + this.num_string_length_max
                + ", num_string_length_min=" + this.num_string_length_min
                + ", num_numberic_sum=" + this.num_numberic_sum
                + ", num_numeric=" + this.num_numeric + ", num_numeric_media="
                + this.num_numeric_media + ", num_numeric_mean="
                + this.num_numeric_mean + ", num_numeric_max="
                + this.num_numeric_max + ", num_numeric_min="
                + this.num_numeric_min + ", num_numeric_variance="
                + this.num_numeric_variance + ", num_numeric_mode="
                + this.num_numeric_mode + "]";
    }

    public boolean parseFromString(String context) {
        return true;
    }

    public static void main(String[] args) {
        String con = "CinemaWrite [value=sdf, index=7, num_null=0.0, type=s, num_string=35188.0, num_string_value_equals_null=0.0, num_string_length_equals_zero=0.0,num_string_length_max=5.0, num_string_length_min=0.0, num_numberic_sum=0.0, num_numeric=0.0, num_numeric_media=0.0,num_numeric_mean=0.0, num_numeric_max=0.0, num_numeric_min=0.0, num_numeric_variance=0.0, num_numeric_mode=0.0]";
        ReflectUtil reflect = new ReflectUtil();
        Object obj = ReflectUtil.getClass(CinemaWrite.class, con);
        System.out.println(obj.toString());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值