John M
John M

Reputation: 11

How to query OrientDB database for vertex in Java?

I want to connect to an OrientDB. The OrientDB contains one unique node with the property "SPECIAL-NODE". I want to query the database for this node and print it out. I have one line of code that works using an iterator (see below) and one that does not (below at bottom).

It seems like both should work yet only the first does, the second does not.

Why is this the case?

Thanks!

// create Orient graph object and connect to database, works successfully
OrientGraph ograph = new OrientGraph("remote:localhost/test", "username", "password");

// this WORKS - finds the one special node and prints it out
Iterator<Vertex> i =ograph.getVertices("SPECIAL-NODE", "SPECIAL").iterator();
System.out.println(i.next());

// this DOES NOT WORK. WHY???
System.out.println("SPECIAL NODE: " + ograph.getVertexByKey("SPECIAL-NODE",   "SPECIAL"));

Upvotes: 1

Views: 218

Answers (1)

LucaS
LucaS

Reputation: 1418

the second "println" doesn't return a value because first you have to create an index on the field " SPECIAL - NODE " otherwise the command will not work.

// create Orient graph object and connect to database, works successfully
OrientGraph ograph = new OrientGraph("remote:localhost/test", "username", "password");

//Create an index on your field "SPECIAL-NODE" (I used an INDEX_TYPE.UNIQUE)
ograph.getVertexType("V").createIndex("V.SPECIAL-NODE", OClass.INDEX_TYPE.UNIQUE, "SPECIAL-NODE");

// this WORKS - finds the one special node and prints it out
Iterator<Vertex> i = ograph.getVertices("SPECIAL-NODE", "SPECIAL").iterator();
System.out.println(i.next());

// YOU HAVE TO CREATE AN INDEX ON THE FIELD "SPECIAL-NODE"
System.out.println("SPECIAL NODE: " + ograph.getVertexByKey("SPECIAL-NODE", "SPECIAL"));

Upvotes: 1

Related Questions