OrientDB SQL:如何在它们之间找到顶点并创建边?

时间:2015-04-13 19:38:40

标签: sql graph graph-databases orientdb

在我的数据库中,我运行以下查询:

SELECT @rid AS module_rid, out('USES').out('BELONGS_TO').@rid AS project_rid FROM MODULES LIMIT 10

我收到了以下回复:

module_rid | project_rid
-----------|----------------
#12:0      | []
#12:1      | []
#12:2      | []
#12:3      |        
#11:48677  | #11:48677 #11:48677 #11:48677 #11:48677 #11:48677 ..More(49)
#12:4      |        
#11:48677  | #11:48677 #11:48677 #11:48677 #11:48677 #11:48677 ..More(49)
#12:5      |        
#11:2526   | #11:2526 #11:2526 #11:47148 #11:47148 #11:25338 ..More(30)
#12:6      | []

如何在模块和它们所依赖的项目之间创建边缘(例如RELIES_ON)(它们至少使用项目的一个模块)?

2 个答案:

答案 0 :(得分:2)

create class Module extends V
create class Project extends V

create class Uses extends E
create class ReliesOn extends E


create vertex Module set name = 'm1'
create vertex Module set name = 'm2'
create vertex Module set name = 'm3'

create vertex Project set name = 'p1'
create vertex Project set name = 'p2'
create vertex Project set name = 'p3'


create edge Uses from (select from Module where name = 'm2') to (select from Project where name = 'p1')
create edge Uses from (select from Module where name = 'm3') to (select from Project where name = 'p2')
create edge Uses from (select from Module where name = 'm3') to (select from Project where name = 'p3')

我理解上面的情况与您所拥有的情况略有不同,但我相信它足以让您了解问题的可能解决方案。

您可以定义一个函数 createEdges ,例如:

var gdb = orient.getGraph();

if(to.size() != 0){
    var command = "create edge ReliesOn from " + from + " to " + to;
    gdb.command("sql", command);
}
return;

enter image description here

现在,以下查询将在创建边时找到顶点:

select from (
    select @rid as module_rid, out('Uses').@rid as project_rid from Module
)
let $ce = createEdges(module_rid, project_rid)

<强>更新

如果你想确保&#34;到&#34;不包含重复项,您可以:

select from (
    select @rid as module_rid, $aux[0].set.@rid as project_rid from Module
    let $aux = ( select set(out('Uses')) from $current )
) 
let $ce = createEdges(module_rid, project_rid)

答案 1 :(得分:1)

这就是我最终做的事情:

使用三个参数定义函数a = ["z", "a", "b", "c"] a1 = ["z", "a", "b", "c"] a2 = ["a", "b", "c"] b = ["a", "b", "c"] b1 = ["a", "b", "c"] b2 = ["a", "b", "c"] createEdges

createEdges(from, to, type)

使用一个参数定义函数// Check whether "from" is invalid or empty if (from instanceof java.util.Collection) { if (from.isEmpty()) { return []; } else { var it = from.iterator(); var obj = it.next(); if (!(obj instanceof com.orientechnologies.orient.core.id.ORecordId)) { throw "Bad Input: createdEdges() only accepts ORecordIds or Collections of ORecordIds"; } } } else if (!(from instanceof com.orientechnologies.orient.core.id.ORecordId)) { throw "Bad Input: createdEdges() only accepts ORecordIds or Collections of ORecordIds"; } // Check whether "to" is invalid or empty if (to instanceof java.util.Collection) { if (to.isEmpty()) { return []; } else { var it = to.iterator(); var obj = it.next(); if (!(obj instanceof com.orientechnologies.orient.core.id.ORecordId)) { throw "Bad Input: createdEdges() only accepts ORecordIds or Collections of ORecordIds"; } } } else if (!(to instanceof com.orientechnologies.orient.core.id.ORecordId)) { throw "Bad Input: createdEdges() only accepts ORecordIds or Collections of ORecordIds"; } var g = orient.getGraph(); var cmd = "CREATE EDGE " + type + " FROM " + from + " TO " + to; return g.command("sql", cmd); uniq

uniq(collection)

现在我可以运行以下SQL命令:

if (collection instanceof java.util.Collection) {
  if (collection.isEmpty()) {
    return collection
  } else {
    return new java.util.HashSet(collection)
  }
} else {
  throw "Bad Input: uniq() only accepts Java collections as input"
}

这将在模块和它们所依赖的项目之间创建独特的边缘,而且速度非常快。

积分归@vitorenesduarte提供初步回复和主要灵感...