简体   繁体   English

如何通过OrientDB中的Graph API(Tinkerpop Blueprints)按边检索顶点?

[英]How to retrieve vertexes by edges by Graph API (Tinkerpop Blueprints) in OrientDB?

I am very newbie in OrientDB.我是 OrientDB 的新手。 As I am a Java Developer I chose Graph API as a connector to the OrientDB.由于我是一名 Java 开发人员,因此我选择了 Graph API 作为 OrientDB 的连接器。

Here I have created a very simple example of code:在这里,我创建了一个非常简单的代码示例:

package launcher;

import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import com.tinkerpop.frames.FramedGraph;
import com.tinkerpop.frames.FramedGraphFactory;

public class TinkerpopTest {

    public static void main(String[] args) {

        OrientGraphNoTx graph = new OrientGraphNoTx("memory:tinkerpop");
        FramedGraphFactory factory = new FramedGraphFactory();
        FramedGraph<Graph> manager = factory.create(graph);

        Vertex marko = graph.addVertex("class:Person");
        marko.setProperty("name", "marko");
        marko.setProperty("age", 29);

        Vertex vadas = graph.addVertex("class:Person");
        vadas.setProperty("name", "vadas");
        vadas.setProperty("age", 27);

        Vertex lop = graph.addVertex("class:Person");
        lop.setProperty("name", "lop");
        lop.setProperty("lang", "java");

        Vertex josh = graph.addVertex("class:Person");
        josh.setProperty("name", "josh");
        josh.setProperty("age", 32);

        Vertex ripple = graph.addVertex("class:Person");
        ripple.setProperty("name", "ripple");
        ripple.setProperty("lang", "java");

        Vertex peter = graph.addVertex("class:Person");
        peter.setProperty("name", "peter");
        peter.setProperty("age", 35);

        graph.addEdge("class:Person", marko, vadas, "knows").setProperty("weight", 0.5f);
        graph.addEdge("class:Person", marko, josh, "knows").setProperty("weight", 1.0f);
        graph.addEdge("class:Person", marko, lop, "created").setProperty("weight", 0.4f);

        graph.addEdge("class:Person", josh, ripple, "created")
                .setProperty("weight", 1.0f);
        graph.addEdge("class:Person", josh, lop, "created").setProperty("weight", 0.4f);

        graph.addEdge("class:Person", peter, lop, "created").setProperty("weight", 0.2f);
    }
}

and Person class:和人类:

package launcher;

import com.tinkerpop.frames.Adjacency;
import com.tinkerpop.frames.Property;

public interface Person {

    @Property("name")
    String getName();

    @Property("name")
    void setName(String name);

    @Property("age")
    int getAge();

    @Property("age")
    void setAge(int age);

    @Adjacency(label = "knows")
    Iterable<Person> getKnowsPeople();

    @Adjacency(label = "knows")
    void addKnowsPerson(Person person);
}

I am building my app with maven and here are project dependencies:我正在使用 maven 构建我的应用程序,这里是项目依赖项:

<dependencies>
    <dependency>
        <groupId>com.orientechnologies</groupId>
        <artifactId>orientdb-core</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.orientechnologies</groupId>
        <artifactId>orientdb-graphdb</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.orientechnologies</groupId>
        <artifactId>orientdb-object</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.tinkerpop.blueprints</groupId>
        <artifactId>blueprints-core</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>com.tinkerpop</groupId>
        <artifactId>frames</artifactId>
        <version>2.6.0</version>
    </dependency>
</dependencies>

This code example is used from the tinkerpop source code.此代码示例来自 tinkerpop 源代码。 As you see we have here vertex type of "Person" and some kind of edges like "knows" and "created".如您所见,这里有“人”的顶点类型和“知道”和“创建”等某种边。 Now I found out how to create vertex and edge and how to add an element to them.现在我发现了如何创建顶点和边以及如何向它们添加元素。 But the problem is that I don't know to retrieve element from the graph according edges.但问题是我不知道根据边从图中检索元素。 I know how retrieve data by its properties likes age etc, but the problem is that I don't understand how retrieve data according edges.我知道如何通过诸如年龄等属性检索数据,但问题是我不明白如何根据边缘检索数据。 eg How to find a vertex that doesn't "know "anybody from the example shown above by Graph API (Tinkerpop Blueprints)?例如,如何通过 Graph API (Tinkerpop Blueprints) 从上面显示的示例中找到“不知道”任何人的顶点?

Try this:尝试这个:

Iterable<Vertex> people=peter.getVertices(Direction.OUT, "knows");
Iterable<OVertex> OVertex().getVertices(ODiriection dir, String... name) (version 3.0)

In this way you're defining the beginning vertex, the direction to search and the name of the edge to across.通过这种方式,您可以定义起始顶点、搜索方向和要跨越的边的名称。 This will return a list of all the vertices connected, for example to peter.这将返回所有连接顶点的列表,例如到 peter。

Hope it helps希望能帮助到你

Regards问候

If you're using OrientDB 2.x, it implements Tinkerpop 2 (aka Blueprints) and does not support what you want.如果您使用的是 OrientDB 2.x,它实现了 Tinkerpop 2(又名蓝图)并且不支持您想要的。

Instead you have to use OrientDB native SQL.相反,您必须使用 OrientDB 本机 SQL。

String sqlQuery = "select from Person where outE('knows').size() = 0";

Iterable<Vertex> lonelyPeople = oGraph.command(new OCommandSQL(sqlQuery)).execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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