我想连接到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"));

最佳答案

第二个“println”不返回值,因为首先必须在字段“special-node”上创建索引,否则该命令将不起作用。

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

09-30 15:21
查看更多