2

我是 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 Blueprints)从上面显示的示例中找到一个不“知道”任何人的顶点?

4

2 回答 2

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();
于 2020-10-23T11:12:08.923 回答
0

尝试这个:

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

通过这种方式,您可以定义起始顶点、搜索方向以及要穿过的边的名称。这将返回所有连接顶点的列表,例如 peter。

希望能帮助到你

问候

于 2018-06-04T17:22:45.267 回答