如何在Java中查询OrientDB数据库的顶点?

时间:2015-11-06 18:46:44

标签: java database orientdb

我想连接到OrientDB。 OrientDB包含一个具有属性" SPECIAL-NODE"的唯一节点。我想查询该节点的数据库并将其打印出来。我有一行代码使用迭代器(见下文)和一行不使用(在底部下方)。

似乎两者都应该工作但只有第一个,第二个没有。

为什么会这样?

谢谢!

// 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"));

1 个答案:

答案 0 :(得分:1)

第二个&#34; println&#34;不会返回一个值,因为首先你必须在字段上创建一个索引&#34; SPECIAL - NODE&#34;否则命令将无效。

// 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"));