Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

java - How do i set an Object as the Value for Map output in Hadoop MapReduce?

In the Hadoop MapReduce, for the intermediate Output (generated by the map()), i want the Value for the Intermediate output to be the following object.


MyObject{
  date:Date
  balance:Double
}

How would i do this. Should i create my own Writable Class?

I am a newbie to MapReduce.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can write your custom type which you can emit as the mapper value. But whatever you want to emit as value, must implement the Writable Interface. You can do something like this :

public class MyObj implements WritableComparable<MyObj>{

    private String date;
    private Double balance;

    public String getDate() { return date;}
    public Double getBalance() { return balance;}

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

        //Define how you want to read the fields
        }
    @Override
    public void writeFields(DataOutput out) throws IOException {

        //Define how you want to write the fields
    }
        .......
        .......
        .......

}

Alternatively you can make use of Avro serialization framework.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...