Search code examples
javahadooplinked-listmapreducecustom-data-type

Forming Custom Linked Lists in Hadoop using Java


I am new to Hadoop programming. Sorry if this is a silly question, but please help me.

For my project I need to form a Custom data type with linked list for value in (key,value) pair. Here is my class.

public class Nikhil implements Writable {
  String str;
  Nikhil next;
  Nikhil() {
    this.str="";
    this.next=null;
  }

  public void add(String t) {
    if(this.str.equals("")) {
      this.str=t;
      this.next=null;
      return;
    } else {
      Nikhil n=new Nikhil();
      n.str=t;
      n.next=null;
      Nikhil temp=this;
      while(temp.next!=null)
      {
        temp=temp.next;
      }
      temp.next=n;
      return;
    }
  }

  public String get()
  {
    if(!this.str.toString().equals(""))
    {
      String result="";
      result=this.str.toString();
      Nikhil temp=this.next;
      while(temp!=null)
      {
        result=result+","+temp.str.toString();
        temp=temp.next;
      }
      return result;
    }
    else
      return "";
  }

  @Override
  public void readFields(DataInput in) throws IOException {
    str=in.readUTF();
    //some code for reading next pointer
  }

  @Override
  public void write(DataOutput out) throws IOException {
    out.writeUTF(str);
    //some code for next
    //
  }
}

please correct the following code and help me for solving my problem.What is the approach for forming trees as custom data types in hadoop.


Solution

  •     @Override
        public void readFields(DataInput in) throws IOException {
            str = in.readUTF();
            //some code for reading next pointer
            if (!"".equals(str)) {
                boolean existNext = in.readBoolean();
                if (existNext) {
                    next = new Nikhil();
                    next.readFields(in);
                }
            }
        }
    
        @Override
        public void write(DataOutput out) throws IOException {
            out.writeUTF(str);
            //some code for next
            //
            if (!"".equals(str)) {
                boolean existNext = null != next;
                out.writeBoolean(existNext);
                if (existNext) {
                    next.write(out);
                }
            }
        }
    

    Maybe the above code is what you want. but other part of your code,especially add() , is not so strict. You'd better do more optimization.