Pay attention to the order when hadoop map reduce custom data types, otherwise an error will be reported.

When customizing the data type, implementing the Writable interface, and rewriting the write method and the readFields method, the order must be guaranteed when operating the fields. If the id field is written first in the write method, the id field is also read in readFields first. Otherwise an error is reported:

package com.my.hadoop;

import org.apache.hadoop.io.Writable;

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

public class PairWritable implements Writable{

    private int id;
    private String name;

    public PairWritable() {
    }

    public PairWritable(int id, String name) {
       set(id,name);
    }

    public void set(int id,String name){
        this.setId(id);
        this.setName(name);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return id+"\t"+name;
    }

    /**
     * The order of writing fields must be the same as the order of reading fields. If the id is read first in write, then in the read method, the id must be read first. Otherwise it will go wrong
     * @param dataOutput
     * @throws IOException
     */
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(id);
        dataOutput.writeUTF(name);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.id=dataInput.readInt();
        this.name=dataInput.readUTF();
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325809972&siteId=291194637