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

时间:2018-06-03 17:43:17

标签: java graph orientdb tinkerpop-blueprint

我是OrientDB的新手。由于我是Java开发人员,因此选择Graph API作为OrientDB的连接器。

这里我创建了一个非常简单的代码示例:

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

和人类:

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

我正在用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>

此代码示例用于tinkerpop源代码。如你所见,我们这里有顶点类型的“人物”和某种类似“知道”和“创造”的边缘。现在我发现了如何创建顶点和边以及如何向它们添加元素。但问题是我不知道根据边缘从图中检索元素。我知道如何通过其属性检索数据如年龄等,但问题是我不明白如何根据边缘检索数据。例如如何通过Graph API(Tinkerpop蓝图)从上面显示的例子中找到一个不“知道”任何人的顶点?

2 个答案:

答案 0 :(得分:0)

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.

Hope it helps

Regards

答案 1 :(得分:0)

如果您使用的是OrientDB 2.x,它将实现Tinkerpop 2(又名蓝图),并且不支持您想要的内容。

相反,您必须使用OrientDB本机SQL。

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

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