OrientDB导线功能

时间:2015-05-18 06:39:29

标签: orientdb

我想选择连接到另一个顶点的所有顶点。我目前正在使用OrientDB中的try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()){ // use httpClient (no need to close it explicitly) } catch (IOException e) { // handle } 函数。请考虑以下示例:

traverse

现在,当我试图寻找史密斯教授教授的课程时,我使用以下命令:

> create class professor extends V
> create class course extends V
> insert into professor set name='Smith'
Inserted record 'professor#14:0{name:Smith} v1'

> insert into course set name='Calculus'
Inserted record 'course#15:0{name:Calculus} v1'

> create class teaches extends E
> create edge teaches from #14:0 to #15:0
Created edge '[teaches#16:0{out:#14:0,in:#15:0} v3]'

为什么这会让我回到边缘而不是我正在寻找的顶点(路线)?返回给我顶点的适当命令是什么?我希望返回'微积分'的记录。

3 个答案:

答案 0 :(得分:1)

我稍微扩展了您的图表以尝试您的查询。

enter image description here

如果你想通过边缘“教”只知道某些起始顶点的连接顶点,你应该使用SELECT EXPAND (OUT / IN / BOTH),因为如果你想在不同的深度探索图形,TRAVERSE会更有用(在我的情况下,“史密斯”有@rid#11:0):

select expand(out('teaches')) from (select from Professor where name='Smith')

----+-----+------+------------+----------+----------
#   |@RID |@CLASS|name        |in_teaches|in_follows
----+-----+------+------------+----------+----------
0   |#12:0|course|Calculus    |[size=1]  |[size=1]
1   |#12:1|course|Astrophysics|[size=1]  |[size=1]
2   |#12:2|course|Law         |[size=2]  |[size=1]
----+-----+------+------------+----------+----------

或使用select expand(out('teaches')) from #11:0,您将获得相同的结果:

----+-----+------+------------+----------+----------
#   |@RID |@CLASS|name        |in_teaches|in_follows
----+-----+------+------------+----------+----------
0   |#12:0|course|Calculus    |[size=1]  |[size=1]
1   |#12:1|course|Astrophysics|[size=1]  |[size=1]
2   |#12:2|course|Law         |[size=2]  |[size=1]
----+-----+------+------------+----------+----------

或者你可以获得教授“史密斯”的所有连接顶点

select expand(out()) from professor where name="Smith"

----+-----+----------+------------+----------+----------+------------+----------
#   |@RID |@CLASS    |name        |in_teaches|in_follows|in_studiesAt|in_worksAt
----+-----+----------+------------+----------+----------+------------+----------
0   |#12:0|course    |Calculus    |[size=1]  |[size=1]  |null        |null
1   |#12:1|course    |Astrophysics|[size=1]  |[size=1]  |null        |null
2   |#12:2|course    |Law         |[size=2]  |[size=1]  |null        |null
3   |#16:0|university|Cambridge   |null      |null      |[size=1]    |[size=1]
----+-----+----------+------------+----------+----------+------------+----------

您的查询traverse out_teaches from #11:0似乎列出了起始顶点以及所有相对INOUT顶点的连接边:

----+-----+---------+-----+-----------+-----------+-----+-----
#   |@RID |@CLASS   |name |out_teaches|out_worksAt|out  |in
----+-----+---------+-----+-----------+-----------+-----+-----
0   |#11:0|professor|Smith|[size=3]   |[size=1]   |null |null
1   |#13:0|teaches  |null |null       |null       |#11:0|#12:0
2   |#13:1|teaches  |null |null       |null       |#11:0|#12:1
3   |#13:2|teaches  |null |null       |null       |#11:0|#12:2
----+-----+---------+-----+-----------+-----------+-----+-----

我也尝试了traverse out_teaches from professor,结果与之前的查询类似:

----+-----+---------+-----+-----------+-----------+-----+-----
#   |@RID |@CLASS   |name |out_teaches|out_worksAt|out  |in
----+-----+---------+-----+-----------+-----------+-----+-----
0   |#11:0|professor|Smith|[size=3]   |[size=1]   |null |null
1   |#13:0|teaches  |null |null       |null       |#11:0|#12:0
2   |#13:1|teaches  |null |null       |null       |#11:0|#12:1
3   |#13:2|teaches  |null |null       |null       |#11:0|#12:2
4   |#11:1|professor|Green|[size=1]   |[size=1]   |null |null
5   |#13:3|teaches  |null |null       |null       |#11:1|#12:2
----+-----+---------+-----+-----------+-----------+-----+-----

答案 1 :(得分:0)

您的查询对我来说很好。

在我的情况下,我为#11:0教授,#12:0为课程,#13:0为教授

再次重新运行您的查询或尝试以下内容:

遍历#12:0

中的两个('教')

答案 2 :(得分:0)

选择课程的正确语法(至少在OrientDB 2.1中)将基于out('teaches')。例如:

> select expand(out('teaches')) from (select from Professor where name='Smith')

----+-----+------+--------+----------
#   |@RID |@CLASS|name    |in_teaches
----+-----+------+--------+----------
0   |#12:0|Course|Calculus|[size=1]  
----+-----+------+--------+----------

也就是说,正如预期的那样,只有一个顶点。

请注意'traverse'用于其他目的。它涉及遍历图的迭代过程。

out_teaches

“out_teaches”是对边缘的引用。使用OrientDB 2.1.7,我为“out_teaches”查询获得的响应如下:

> select expand(out_teaches) from (select from Professor where name='Smith')

----+-----+-------+-----+-----
#   |@RID |@CLASS |out  |in   
----+-----+-------+-----+-----
0   |#13:0|teaches|#11:0|#12:0
----+-----+-------+-----+-----

同样,这是人们所期望的 - 一种优势。