在hadoop FS中编写二叉树
我需要将二叉树写入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 HDFS 的原因是什么? HDFS 是一个分布式文件系统,可以存储任何类型的数据/文件。您已经编写了大量代码来有效地大规模存储和检索图形。
您可以从面向图形的数据库存储和检索图形,例如 OrientDB 和 Neo4j。
此外,还有一些开源框架,例如 Apache Giraph、Apache Hama 和 GoldenOrb。还可能存在与 Java 程序进行交互的绑定。
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.
好的,我找到了解决方案。我不知道它是否足够有效,但它确实有效。这就是我所做的。这些在我的类定义中:
这就是我使用它们的方式:
用于写作:
用于阅读:
这很好用。现在我可以创建我的决策树并通过存储我的根来存储它。 :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:
And this is how i use them:
For writing:
For reading:
This works fine. Now i am able to create my decision tree and store it just by storing my root. :D