java - 使用Java在Hadoop中形成自定义链接列表

标签 java hadoop linked-list mapreduce custom-data-type

我是Hadoop编程的新手。抱歉,这是一个愚蠢的问题,请帮助我。

对于我的项目,我需要形成一个自定义数据类型,其中包含(键,值)对中值的链接列表。这是我的课。

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
    //
  }
}

请更正以下代码,并帮助我解决问题。在hadoop中将树形成为自定义数据类型的方法是什么?

最佳答案

    @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);
            }
        }
    }

也许上面的代码是您想要的。但是您代码的其他部分,尤其是add()并不是那么严格。您最好进行更多优化。

关于java - 使用Java在Hadoop中形成自定义链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36300116/

相关文章:

java - 如何在 OS X 10.9.1 Mavericks 上安装 JDK for Java 1.6 SE

hadoop - 通过Swagger Rest API调用运行Sqoop Java程序时,包丢失错误

hadoop - 如何在浏览器中 GCP 实例之外的 hadoop 中访问我的 Namenode GUI

c++ - 为什么地址一样?

java - 如何在Java中不使用构造函数手动实现链表?

java - Spring MVC : How to correctly create the entity for following JSON response

java - 透明 Activity 内容与父级不匹配

java - Jtwitter 的 100 多个搜索结果

hadoop - Hive 从表中选择复杂类型

C - 显示期间链表段错误