如何在Orientdb中获取顶点并创建边缘(How can I get vertex and create edge in Orientdb)

编程入门 行业动态 更新时间:2024-10-18 14:20:59
如何在Orientdb中获取顶点并创建边缘(How can I get vertex and create edge in Orientdb)

我在两个不同的类中创建了两个顶点,我试图在另一个类中创建Edge。 我怎样才能做到这一点?

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); }

如何在另一个类中创建上述两个顶点之间的任何一个帮助我

I have created two vertexes in two different class, and I am trying to create Edge in another class. How can I do this?

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); }

how can I create edge between the above two vertex in another class any one help me

最满意答案

我用你的代码尝试了你的情况,然后是我的例子。

这些是我建议您遵循的主要步骤:

创建类和属性; 插入数据; 在顶点之间创建边。

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(); } } }

工作室输出:

在此处输入图像描述

EDITED

嗨@eswara,考虑这个结构:

在此处输入图像描述

现在,您可以检索要查找的顶点并在它们之间创建边缘

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(); } } }

输出:

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

工作室:

在此处输入图像描述

希望能帮助到你

I tried your case with your code and following there's my example.

These are the main steps I advise you to follow:

Create classes and properties; Insert the data; Create the edges between the vertexes.

JAVA CODE:

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 OUTPUT:

enter image description here

EDITED

Hi @eswara, consider this structure:

enter image description here

Now you can retrieve the vertices you're looking for and create an edge between them

JAVA CODE:

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(); } } }

Output:

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

STUDIO:

enter image description here

Hope it helps

更多推荐

本文发布于:2023-07-04 17:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026938.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:顶点   边缘   如何在   Orientdb   edge

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!