在hadoop FS中编写二叉树

发布于 2024-12-22 21:52:44 字数 1095 浏览 2 评论 0原文

我需要将二叉树写入 HDFS,我将用它来表示决策树。但为了做到这一点,我首先需要创建一个 BinaryTreeNode 类,它将作为树节点。这些是我的类属性:

private String name;
private String attribute;
private String attType;
private String condition;
private String lines;
private BinaryTreeNode leftChild;
private BinaryTreeNode rightChild;

所以现在我需要实现 write 和 readFields 方法来读取和写入这些节点。这些是我所做的:

public void write(DataOutput d) throws IOException 
{
    d.writeUTF(name);
    d.writeUTF(attribute);
    d.writeUTF(attType);
    d.writeUTF(condition);
    d.writeUTF(lines);
    //SOMETHIN FOR LEFT AND RIGHT CHILD
}

public void readFields(DataInput di) throws IOException 
{
    name=di.readUTF();
    attribute=di.readUTF();
    attType=di.readUTF();
    condition=di.readUTF();
    lines=di.readUTF();
    //SOMETHIN FOR LEFT AND RIGHT CHILD
}

BinaryTreeNode read(DataInput in) throws IOException
{
     BinaryTreeNode ob = new BinaryTreeNode();
     ob.readFields(in);
     return ob;
}

我想不到的是如何写入和读取我的 2 个子节点。请注意,树将递归构建,每个节点将有 0-2 个子节点。所以我后来的目的是拥有一个具有 BinaryTreeNode root 属性的 BinaryTree 类。谢谢

I need to write a binary tree to HDFS, which i will use to represent a desicion tree. But in order to do that i first need to create a BinaryTreeNode class, which will be the tree node. These are my class attributes:

private String name;
private String attribute;
private String attType;
private String condition;
private String lines;
private BinaryTreeNode leftChild;
private BinaryTreeNode rightChild;

So now i need to implement the write and readFields methods for reading and writing these nodes. These are what i have done:

public void write(DataOutput d) throws IOException 
{
    d.writeUTF(name);
    d.writeUTF(attribute);
    d.writeUTF(attType);
    d.writeUTF(condition);
    d.writeUTF(lines);
    //SOMETHIN FOR LEFT AND RIGHT CHILD
}

public void readFields(DataInput di) throws IOException 
{
    name=di.readUTF();
    attribute=di.readUTF();
    attType=di.readUTF();
    condition=di.readUTF();
    lines=di.readUTF();
    //SOMETHIN FOR LEFT AND RIGHT CHILD
}

BinaryTreeNode read(DataInput in) throws IOException
{
     BinaryTreeNode ob = new BinaryTreeNode();
     ob.readFields(in);
     return ob;
}

What i cant think of is how to write and read my 2 child nodes. Note that the tree is going to be build recursively and every node will have 0-2 childs. So my later purpose is to have a BinaryTree class that will have the attribute BinaryTreeNode root. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榆西 2024-12-29 21:52:44

我需要将二叉树写入HDFS

我所需要的只是一种保存和加载我的树的方法。

使用 HDFS 的原因是什么? HDFS 是一个分布式文件系统,可以存储任何类型的数据/文件。您已经编写了大量代码来有效地大规模存储和检索图形。

您可以从面向图形的数据库存储和检索图形,例如 OrientDBNeo4j

此外,还有一些开源框架,例如 Apache GiraphApache HamaGoldenOrb。还可能存在与 Java 程序进行交互的绑定。

I need to write a binary tree to HDFS

All i need is a way to save and load my tree.

What's the reason to go with HDFS? HDFS is a distributed file system on which any type of data/files can be stored. You have write a lot of code to store and retrieve the graphs effectively on large scale.

You can store and retrieve graphs from graph oriented databases like OrientDB and Neo4j.

Also, there are open source frameworks like Apache Giraph, Apache Hama and GoldenOrb. There might also be bindings to interact from Java programs.

好的,我找到了解决方案。我不知道它是否足够有效,但它确实有效。这就是我所做的。这些在我的类定义中:

    public void write(DataOutput d) throws IOException
    {
      d.writeUTF(name);
      d.writeUTF(attribute);
      d.writeUTF(attType);
      d.writeUTF(condition);
      d.writeUTF(lines);
      ObjectWritable left=new ObjectWritable(BinaryTreeNode.class,leftChild);
      left.write(d);
      ObjectWritable right=new ObjectWritable(BinaryTreeNode.class,rightChild);
      right.write(d);
    }

    public void readFields(DataInput di) throws IOException
    {
      name=di.readUTF();
      attribute=di.readUTF();
      attType=di.readUTF();
      condition=di.readUTF();
      lines=di.readUTF();
      leftChild=(BinaryTreeNode) ObjectWritable.readObject(di, new Configuration());
      rightChild=(BinaryTreeNode) ObjectWritable.readObject(di, new Configuration());

    }

这就是我使用它们的方式:

用于写作:

      BinaryTreeNode bla=new BinaryTreeNode();
      //set the attributes
      ObjectWritable obj=new ObjectWritable(BinaryTreeNode.class,bla);
      obj.write(dos);

用于阅读:

      BinaryTreeNode bla=new BinaryTreeNode();
      bla= (BinaryTreeNode) ObjectWritable.readObject(in, conf);

这很好用。现在我可以创建我的决策树并通过存储我的根来存储它。 :D

Ok i found the solution. I dont know if it is efficient enough but it works. Here is what i did. These are in my class definition:

    public void write(DataOutput d) throws IOException
    {
      d.writeUTF(name);
      d.writeUTF(attribute);
      d.writeUTF(attType);
      d.writeUTF(condition);
      d.writeUTF(lines);
      ObjectWritable left=new ObjectWritable(BinaryTreeNode.class,leftChild);
      left.write(d);
      ObjectWritable right=new ObjectWritable(BinaryTreeNode.class,rightChild);
      right.write(d);
    }

    public void readFields(DataInput di) throws IOException
    {
      name=di.readUTF();
      attribute=di.readUTF();
      attType=di.readUTF();
      condition=di.readUTF();
      lines=di.readUTF();
      leftChild=(BinaryTreeNode) ObjectWritable.readObject(di, new Configuration());
      rightChild=(BinaryTreeNode) ObjectWritable.readObject(di, new Configuration());

    }

And this is how i use them:

For writing:

      BinaryTreeNode bla=new BinaryTreeNode();
      //set the attributes
      ObjectWritable obj=new ObjectWritable(BinaryTreeNode.class,bla);
      obj.write(dos);

For reading:

      BinaryTreeNode bla=new BinaryTreeNode();
      bla= (BinaryTreeNode) ObjectWritable.readObject(in, conf);

This works fine. Now i am able to create my decision tree and store it just by storing my root. :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文