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

225 views Asked by At

I am very newbie in OrientDB. As I am a Java Developer I chose Graph API as a connector to the 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:

<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. 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. e.g. How to find a vertex that doesn't "know "anybody from the example shown above by Graph API (Tinkerpop Blueprints)?

2

There are 2 answers

0
diegomtassis On

If you're using OrientDB 2.x, it implements Tinkerpop 2 (aka Blueprints) and does not support what you want.

Instead you have to use OrientDB native SQL.

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

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

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