OrientDB - 使用一种自连接创建边缘 [英] OrientDB - Create Edge using kind of self join

查看:48
本文介绍了OrientDB - 使用一种自连接创建边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用 OETL 从 RDMBS 将分层数据导入 OrientDB.在 RDBMS 中,我们曾经将 parentId 存储在同一行中.例如表结构是这样的:

I have imported hierarchical data into OrientDB from RDMBS using OETL. In RDBMS we used to store parentId in the same row. e.g. the table structure is something like this:

ID - 姓名 - Parent_ID

ID - Name - Parent_ID

公司 - 公司办公室 - 公司

Corp - Corporate Office - Corp

D1 - 地区办公室 1 - 公司

D1 - District Office 1 - Corp

D2 - 地区办公室 2 - 公司

D2 - District Office 2 - Corp

SO1 - 小型办公室 1 - D1

SO1 - Small Office 1 - D1

SO2 - 小型办公室 2 - D2

SO2 - Small Office 2 - D2

SO3 - 小型办公室 3 - D1

SO3 - Small Office 3 - D1

现在每一行都是 Orientdb 中的一个节点.

Now each row is a node in Orientdb.

我想创建一条从 Corp 到 D1、D1 到 SO1 的边 (ParentOf),依此类推.

I want to create an edge (ParentOf) from say Corp to D1 and D1 to SO1 and so on.

如何编写查询来实现此目的?遵循以下原则?

How can I write a query to achieve this? Something along the line of following?

从(从节点选择)a到(从节点选择其中 a.id = parent_id)

create edge parentOf from (select from node)a to (select from node where a.id = parent_id)

对不起,我还在用关系数据库的方式思考.

Sorry I am still thinking in relational db way.

Orient DB 版本为 orientdb-community-2.0.9

Orient DB version is orientdb-community-2.0.9

推荐答案

js 函数一直在运行.最后写了java代码来做到这一点:

The js function was running forever. Finally wrote java code to do this:

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

这篇关于OrientDB - 使用一种自连接创建边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆