Retrieving OrientVertex Objects from OrientDB

3.9k views Asked by At

I'm having trouble with the Graph API of OrientDB in Java.

Problem:

Retrieve Vertices (OrientVertex or Vertex?) from a persistent local graph database with several Vertices/Edges created via the console.

So for, I've been able to query the database from what I now think is the Document API using

graph = factory.getTx();
String string = String.format("select * from V where name like \"%s\"", criteria);
OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<OrientVertex>(string);
List<OrientDocument> results = graph.getRawGraph().command(query).execute();

But this will not work for Vertices. How do I run queries that return a list of Vertices in my database?

Thanks in advance.

3

There are 3 answers

6
rmuller On BEST ANSWER

Looking at your code, you are using the Graph API. After calling getRawGraph() you are not working with the Graph API any more, but with the Document API (method name is a little bit confusing).

Having a reference to the OrientGraph there are several possibilities

  • Using orientGraph#getVertex*(..) / orientGraph#getVertices*(..)style of methods
  • Using a Query object: orientGraph#query().has("key", value).vertices()
  • Using the gremlin query language
  • Using orientGraph#command(...).execute(). In this case the command is executed outside the transaction (thanks @wolf4ood)
2
wolf4ood On

You can avoid getting the rawGraph and executed command directly with orientGraph and returns an iterable of OrientVertex

like this :

graph = factory.getTx();
String query = String.format("select * from V where name like \"%s\"", criteria);
OSQLSynchQuery<OrientVertex> qr = new OSQLSynchQuery<OrientVertex>(query);
Iterable<OrientVertex> vertices = graph.command(qr).execute();
0
Abhimanyu Shekhawat On

Sometimes we want to retrieve Vertices specially based on other properties like names ,age etc so, I used getVertices() method to get the iterator and then extracted the vertex since getVertexByKey() is deprecated now.The code is: Vertex v2=graph.getVertices("name","Business").iterator().next();

So we just got the vertex without knowing the RID of the record. I don't know whether this is the best method to do this but it does the job.