简体   繁体   中英

Create Edges in OrientDB

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM