Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I query OrientDB Vertex graph object by Record ID in Java?

Tags:

java

orientdb

How do I retrieve an OrientDB Document/Object or Graph object using its Record ID? (Language: Java)

I'm referring to http://orientdb.com/docs/2.0/orientdb.wiki/Tutorial-Record-ID.html and Vertex.getId() / Edge.getId() methods.

It is just like an SQL query "SELECT * from aTable WHERE ID = 1".

Usage/purpose description: I want to store the generated ID after it is created by OrientDB, and later retrieve the same object using the same ID.

like image 533
ikevin8me Avatar asked Nov 09 '22 22:11

ikevin8me


1 Answers

(1) I'd suggest using OrientDB 2.1, and its documentation, e.g. http://orientdb.com/docs/2.1/Tutorial-Record-ID.html

(2) From your post, it's unclear to me whether you need help obtaining the RID from the results of a query, or retrieving an object given its RID, so let me begin by mentioning that the former can be accomplished as illustrated by this example (in the case of an INSERT query):

ODocument result=db.command(new OCommandSQL(<INSERTQUERY>)).execute();

System.out.println(result.field("@rid"));

Going the other way around, there are several approaches. I have verified that the following does work using Version 2.1.8:

OrientGraph graph = new OrientGraph("plocal:PATH_TO_DB", "admin", "admin");
Vertex v = graph.getVertex("#16:0");

An alternative and more generic approach is to construct and execute a SELECT query of the form SELECT FROM :RID, along the lines of this example:

List<ODocument> results = db.query(new OSQLSynchQuery<ODocument>("select from " + rid));
for (ODocument aDoc : results) {
    System.out.println(aDoc.field("name"));
}

(3) In practice, it will usually be better to use some other "handle" on OrientDB vertices and edges in Java code, or indeed when using any of the supported programming languages. For example, once one has a vertex as a Java Vertex, as in the "Vertex v" example above, one can usually use it.

like image 125
peak Avatar answered Nov 14 '22 23:11

peak