Advantages of using NullWritable in Hadoop

前端 未结 3 1590
时光说笑
时光说笑 2021-01-30 11:18

What are the advantages of using NullWritable for null keys/values over using null texts (i.e. new Text(null)). I see the fol

3条回答
  •  执笔经年
    2021-01-30 11:58

    You can always wrap your string in your own Writable class and have a boolean indicating it has blank strings or not:

    @Override
    public void readFields(DataInput in) throws IOException { 
        ...
        boolean hasWord = in.readBoolean();
        if( hasWord ) {
            word = in.readUTF();
        }
        ...
    }
    

    and

    @Override
    public void write(DataOutput out) throws IOException {
        ...
        boolean hasWord = StringUtils.isNotBlank(word);
        out.writeBoolean(hasWord);
        if(hasWord) {
            out.writeUTF(word);
        }
        ...
    }
    

提交回复
热议问题