OrientDB - 使用自连接创建边缘
OrientDB - Create Edge using kind of self join
我已经使用 OETL 从 RDMBS 将分层数据导入到 OrientDB 中。在 RDBMS 中,我们过去常常将 parentId 存储在同一行中。
例如table 结构是这样的:
ID - Name - Parent_ID
Corp - Corporate Office - Corp
D1 - District Office 1 - Corp
D2 - District Office 2 - Corp
SO1 - Small Office 1 - D1
SO2 - Small Office 2 - D2
SO3 - Small Office 3 - D1
现在每一行都是Orientdb中的一个节点。
我想创建一条边 (ParentOf),从 Corp 到 D1,从 D1 到 SO1 等等。
如何编写查询来实现此目的?以下是什么?
create edge parentOf from (select from node)a to (select from node
where a.id = parent_id)
抱歉,我还在用关系数据库的方式思考。
Orient DB 版本为 orientdb-community-2.0.9
您不能将信息从第一个 select 查询(从部分)传递到第二个查询(到部分)。所以你应该创建一个 server side function。这样的事情可能会起作用(我稍后会测试它,当我可以访问 Orient Studio 时):
var db = orient.getGraph;
var nodes = db.getVerticesOfClass("node");
for (var i=0; i<nodes.length; i++) {
var vertex = nodes[i]
var id = vertex.getProperty("id");
var children = db.getVertices("parent_id", id);
for (var j=0; j<children.length; j++) {
var child = children[j];
vertex.addEdge("parentOf", child)
}
}
感谢 pembeci,以下是最终成功的函数:
var db = orient.getGraph();
var nodes = db.getVerticesOfClass("nodex");
var itr = nodes.iterator();
while(itr.hasNext()) {
var vertex = itr.next();
var id = vertex.getProperty("id");
var children = db.getVertices("parent_id", id);
var childItr = children.iterator();
while(childItr.hasNext()) {
var child = childItr.next();
vertex.addEdge("parentOf", child);
}
}
js 函数永远 运行。最后写了 java 代码来做到这一点:
OrientGraph graph = new OrientGraph("remote:localhost/testNode", "root", "root");
for (Vertex v : graph.getVerticesOfClass("Node"))
{
String id = v.getProperty("ID");
System.out.println("Checking children of: " + id);
String sql = "select from node where PARENT_ID = '"+id+"'";
for (Vertex child : (Iterable<Vertex>) graph.command(
new OCommandSQL(sql))
.execute())
{
v.addEdge("parent_of", child);
System.out.println("\tAdded 'Parent Of' Edge from: " + id + " to " + child.getProperty("ID"));
}
graph.commit();
}
我已经使用 OETL 从 RDMBS 将分层数据导入到 OrientDB 中。在 RDBMS 中,我们过去常常将 parentId 存储在同一行中。 例如table 结构是这样的:
ID - Name - Parent_ID
Corp - Corporate Office - Corp
D1 - District Office 1 - Corp
D2 - District Office 2 - Corp
SO1 - Small Office 1 - D1
SO2 - Small Office 2 - D2
SO3 - Small Office 3 - D1
现在每一行都是Orientdb中的一个节点。
我想创建一条边 (ParentOf),从 Corp 到 D1,从 D1 到 SO1 等等。
如何编写查询来实现此目的?以下是什么?
create edge parentOf from (select from node)a to (select from node where a.id = parent_id)
抱歉,我还在用关系数据库的方式思考。
Orient DB 版本为 orientdb-community-2.0.9
您不能将信息从第一个 select 查询(从部分)传递到第二个查询(到部分)。所以你应该创建一个 server side function。这样的事情可能会起作用(我稍后会测试它,当我可以访问 Orient Studio 时):
var db = orient.getGraph;
var nodes = db.getVerticesOfClass("node");
for (var i=0; i<nodes.length; i++) {
var vertex = nodes[i]
var id = vertex.getProperty("id");
var children = db.getVertices("parent_id", id);
for (var j=0; j<children.length; j++) {
var child = children[j];
vertex.addEdge("parentOf", child)
}
}
感谢 pembeci,以下是最终成功的函数:
var db = orient.getGraph();
var nodes = db.getVerticesOfClass("nodex");
var itr = nodes.iterator();
while(itr.hasNext()) {
var vertex = itr.next();
var id = vertex.getProperty("id");
var children = db.getVertices("parent_id", id);
var childItr = children.iterator();
while(childItr.hasNext()) {
var child = childItr.next();
vertex.addEdge("parentOf", child);
}
}
js 函数永远 运行。最后写了 java 代码来做到这一点:
OrientGraph graph = new OrientGraph("remote:localhost/testNode", "root", "root");
for (Vertex v : graph.getVerticesOfClass("Node"))
{
String id = v.getProperty("ID");
System.out.println("Checking children of: " + id);
String sql = "select from node where PARENT_ID = '"+id+"'";
for (Vertex child : (Iterable<Vertex>) graph.command(
new OCommandSQL(sql))
.execute())
{
v.addEdge("parent_of", child);
System.out.println("\tAdded 'Parent Of' Edge from: " + id + " to " + child.getProperty("ID"));
}
graph.commit();
}