2016-05-05 5 views
0

2つの異なるクラスで2つの頂点を作成しましたが、別のクラスでEdgeを作成しようとしています。これどうやってするの?Orientdbで頂点を取得してエッジを作成する方法

class m1{ 

     OrientGraph graph=factory.getTx(); 

     OrientVertexType v=graph.createVertexType("Delears"); 

     v.createProperty("ID", OType.INTEGER); 

     v.createProperty("Name",OType.STRING); 

     v.createProperty("Address", OType.STRING); 
} 

class m2{ 

     OrientVertexType v1=graph.createVertexType("SuperMarket"); 

     v1.createProperty("Item", OType.STRING); 

     v1.createProperty("Code", OType.DOUBLE); 

     v1.createProperty("Quantity", OType.INTEGER); 

} 

どのように私は別のクラスでは上記の二つの頂点間のエッジを作成することができますいずれかが私に

答えて

0

を助ける私はあなたのコードをあなたのケースを試してみましたが、私の例があります以下。

これら

は、私はあなたが従うことを助言する主な手順は次のとおりです。

  • クラスとプロパティを作成します。
  • データを挿入します。
  • 頂点間にエッジを作成します。

Javaコード:

import java.io.IOException; 

import com.orientechnologies.orient.client.remote.OServerAdmin; 
import com.orientechnologies.orient.core.metadata.schema.OType; 
import com.tinkerpop.blueprints.impls.orient.OrientEdge; 
import com.tinkerpop.blueprints.impls.orient.OrientEdgeType; 
import com.tinkerpop.blueprints.impls.orient.OrientGraph; 
import com.tinkerpop.blueprints.impls.orient.OrientVertex; 
import com.tinkerpop.blueprints.impls.orient.OrientVertexType; 

public class Stack37046827 { 

private static String remote = "remote:localhost/"; 

public static void main(String[] args) { 

    try { 

     String DBname = "Stack37046827"; 
     String currentPath = remote + DBname; 

     OServerAdmin serverAdmin = new OServerAdmin(currentPath).connect("root", "root"); 

     OrientGraph g = new OrientGraph(currentPath); 

     // OrientVertexType used to create classes 

     OrientVertexType v = g.createVertexType("Delears"); 
     v.createProperty("ID", OType.INTEGER); 
     v.createProperty("Name", OType.STRING); 
     v.createProperty("Address", OType.STRING); 

     OrientVertexType v1 = g.createVertexType("SuperMarket"); 
     v1.createProperty("Item", OType.STRING); 
     v1.createProperty("Code", OType.DOUBLE); 
     v1.createProperty("Quantity", OType.INTEGER); 

     OrientEdgeType e = g.createEdgeType("myEdge", "E"); 

     // Once classes and properties are created, you can populate the DB 

     // OrientVertex used to create the vertexes 

     OrientVertex delears = g.addVertex("class:Delears"); 
     delears.setProperties("ID", "1"); 
     delears.setProperties("Name", "name1"); 
     delears.setProperties("Address", "address1"); 

     OrientVertex superMarket = g.addVertex("class:SuperMarket"); 
     superMarket.setProperties("Item", "item1"); 
     superMarket.setProperties("Code", "2"); 
     superMarket.setProperties("Quantity", "5"); 

     // OrientEdge to create the edge between vertexes 

     OrientEdge e1 = g.addEdge(null, delears, superMarket, "myEdge"); 

     g.shutdown(); 

     serverAdmin.close(); 

    } catch (IOException e) { 

     e.printStackTrace(); 

    } 

} 

} 

STUDIO出力:

enter image description here

EDITED

enter image description here

今、あなたが探している頂点を取得し、それらの間の

Javaコードエッジを作成することができます:

import java.io.IOException; 

import com.orientechnologies.orient.client.remote.OServerAdmin; 
import com.tinkerpop.blueprints.Vertex; 
import com.tinkerpop.blueprints.impls.orient.OrientEdge; 
import com.tinkerpop.blueprints.impls.orient.OrientGraph; 
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory; 

public class Stack37046827 { 
private static String remote = "remote:localhost/"; 

public static void main(String[] args) { 
    try { 
     String DBname = "Stack37046827"; 
     String currentPath = remote + DBname; 
     OServerAdmin serverAdmin = new OServerAdmin(currentPath).connect("root", "root"); 
     OrientGraphFactory factory = new OrientGraphFactory(currentPath); 
     OrientGraph g = factory.getTx(); 
     Iterable<Vertex> delears = g.getVerticesOfClass("Delears"); 
     Iterable<Vertex> sMarkets = g.getVerticesOfClass("SuperMarket"); 
     for (Vertex delear : delears) { 
      for (Vertex sMarket : sMarkets) { 
       if (delear.getProperty("Name").equals("name0") && sMarket.getProperty("Item").equals("item0")) { 
        OrientEdge e1 = g.addEdge(null, delear, sMarket, "myEdge"); 
        g.commit(); 
        System.out.println(e1); 
       } 
      } 
     } 
     g.shutdown(); 
     serverAdmin.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 
をこんにちは@eswara、このような構造を検討

出力:

e[#14:0][#12:0-myEdge->#13:0] 

STUDIO:

enter image description here

は、それが

+0

おかげでお役に立てば幸いです。私の必要条件は、すべての頂点、挿入、およびエッジの作成を含む単一のクラスではなく、別々のクラスにエッジを作成することです。 – eswara

+0

こんにちは@eswara、私は必要な頂点を取得し、それらの間に新しいエッジを作成する別のJavaクラスで答えを編集しました。 – LucaS

+0

非常に便利です – eswara

関連する問題