Search code examples
orientdb

OrientDB batch commands on unique edges and vertices


Suppose I want to create a set of unique edges and vertices.

create vertex A set etc.

create vertex B set etc.

create edge AB, create edge AC, 

And all of these edges and vertices are unique--so some of the commands will likely fail when they are unique.

How do I batch these commands such that I am guaranteed all commands will be run, even when some commands fail?


Solution

  • I tried your case, I have a Vertex class with a name property (unique index), you can execute batch commands in different ways:

    • Studio

      begin
      LET a = create vertex User set name = 'John'
      LET b = create vertex User set name = 'Jane'
      LET c = create edge FriendOf from $a to $b
      commit retry 100
      return $c
      
    • Java API

      OrientGraph g=new OrientGraph(currentPath);
      String cmd = "begin\n";
      cmd += "let $user2 = UPDATE User SET user_id = 'userX' UPSERT RETURN AFTER @rid WHERE user_id = 'userX'\n";
      cmd += "let $service = UPDATE Service SET service = 'serviceX' UPSERT RETURN AFTER @rid WHERE service = 'serviceX'\n";
      cmd += "CREATE edge link FROM $user2 TO $service\n";
      cmd += "commit";
      g.command(new OCommandScript("sql", cmd)).execute();
      
    • Console

      create a .txt file with your code like this:

      connect remote:localhost/stack49801389 root root
      
      begin
      create vertex User set name = 'John'
      create vertex User set name = 'Jane'
      create edge FriendOf from $a to $b
      commit retry 100
      return $c
      

      and then run it by console

    For more information you can take a look at this link

    Hope it helps

    Regards