Create Edges in OrientDB

旧街凉风 提交于 2020-01-02 10:33:29

问题


I have an Orient database with vertices for servers from different management tools. As a server can be monitored by multiple management tools, I would like to create edges between the vertices for the same server. The connected components would then be the individual servers.

The problem I have, is that different systems will have different naming conventions - some use fully qualified domain names, some use internal domain names, and some only use the hostname.

My Sample Data:

orientdb {db=audit}> select * from V

+----+------+-------+----------+----------------------+----------------+
|#   |@RID  |@CLASS |Name      |DomainName            |LocalName       |
+----+------+-------+----------+----------------------+----------------+
|0   |#12:0 |Alpha  |compute-1 |null                  |null            |
|1   |#12:1 |Alpha  |compute-2 |null                  |null            |
|2   |#12:2 |Alpha  |compute-3 |null                  |null            |
|3   |#13:0 |Beta   |null      |compute-1.example.com |null            |
|4   |#13:1 |Beta   |null      |compute-2.example.com |null            |
|5   |#14:0 |Gamma  |null      |null                  |compute-1.local |
|6   |#14:1 |Gamma  |null      |null                  |compute-3.local |
+----+------+-------+----------+----------------------+----------------+

Expected Output:

I would expect 3 distinct commands (pseudocoded below), that would produce the below edges

Alpha to Beta:
    Select from Alpha, join Name as a substring of Beta.DomainName

    edge between #12:0 and #13:0
    edge between #12:1 and #13:1

Alpha to Gamma:
    Select from Alpha, join Name & ".local" with Gamma.LocalName

    edge between #12:0 and #14:0
    edge between #12:2 and #14:1

Beta to Gamma:
    Select LocalName from Gamma, remove ".local" suffix, join as a substring of Beta.DomainName

    edge between #13:0 and #14:0

回答1:


Try this JS function:

var db = orient.getGraph();
var a = db.command('sql','select from Alpha');
var b = db.command('sql','select from Beta');
var g = db.command('sql','select from Gamma');

//Alpha to Beta
for(i=0;i<b.length;i++)
{
  var name = a[i].getRecord().field('Name');
  var Arid = a[i].getRecord().field('@rid');
  for(j=0;j<b.length;j++)
  {
    var dn = b[j].getRecord().field('DomainName').substring(0,name.length);
    var Brid = b[j].getRecord().field('@rid');
    if(name==dn)
    {
      db.command('sql','create edge E from '+Arid+' to '+Brid+'');
    }
  }
}

//Alpha to Gamma
for(i=0;i<a.length;i++)
{
  var name = a[i].getRecord().field('Name');
  var Arid = a[i].getRecord().field('@rid');
  for(j=0;j<g.length;j++)
  {
    var ln = g[j].getRecord().field('LocalName').substring(0,name.length);
    var Grid = g[j].getRecord().field('@rid');
    if(name==ln)
    {
      db.command('sql','create edge E from '+Arid+' to '+Grid+'');
    }
  }
}

//Beta to Gamma
for(i=0;i<b.length;i++)
{
  var name = b[i].getRecord().field('DomainName').substring(0,9);
  var Brid = b[i].getRecord().field('@rid');
  for(j=0;j<g.length;j++)
  {
    var n = g[j].getRecord().field('LocalName').substring(0,name.length);
    var Grid = g[j].getRecord().field('@rid');
    if(name==n)
    {
      db.command('sql','create edge E from '+Brid+' to '+Grid+'');
    }
  }
}

This is the output:

Hope it helps.

Regards




回答2:


You can use these queries :

create edge from (select from Alpha where Name="compute-1") to (select from Beta where DomainName like "compute-1%")
create edge from (select from Alpha where Name="compute-2") to (select from Beta where DomainName like "compute-2%")

create edge from (select from Alpha where Name="compute-1") to (select from Gamma where LocalName like "compute-1%")
create edge from (select from Alpha where Name="compute-3") to (select from Gamma where LocalName like "compute-3%")

create edge from (select from Beta where DomainName like "compute-1%") to (select from Gamma where LocalName like "compute-1%")

Hope it helps



来源:https://stackoverflow.com/questions/37782349/create-edges-in-orientdb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!