找到你要的答案

Q:How to get Vertex from provided attribute / key in Titan

Q:如何从泰坦提供的属性/键获得顶点

I am following code on Titan Documentation GraphOfTheGodsFactory source code I want to retrieve a vertex from the graph, given some specified value / key / attribute.

I searched on documentation (I am using Titan 0.5.2), and tried using different functions, they provide null when executed.

Things I tried,

    `TitanManagement mgmt = graph.getManagementSystem();
    final PropertyKey userid = mgmt.makePropertyKey("userid").dataType(Integer.class).make();
    TitanGraphIndex namei = mgmt.buildIndex("userid",Vertex.class).addKey(userid).unique().buildCompositeIndex();  
    TitanTransaction tx = graph.newTransaction();
    Vertex ver1 = tx.addVertexWithLabel("user");
    ver1.setProperty("userid", "2");
    ver1.setProperty("username", "abc");
    Vertex found = graph.getVertex("2");`

found returns null.

Please help!

我下面的代码在源代码文件graphofthegodsfactory泰坦我想检索从图的一个顶点,给出一些指定的值/关键/属性。

我搜索到的文件(我用的是泰坦0.5.2),并尝试使用不同的功能,它们提供了空时执行。

我尝试的东西,

    `TitanManagement mgmt = graph.getManagementSystem();
    final PropertyKey userid = mgmt.makePropertyKey("userid").dataType(Integer.class).make();
    TitanGraphIndex namei = mgmt.buildIndex("userid",Vertex.class).addKey(userid).unique().buildCompositeIndex();  
    TitanTransaction tx = graph.newTransaction();
    Vertex ver1 = tx.addVertexWithLabel("user");
    ver1.setProperty("userid", "2");
    ver1.setProperty("username", "abc");
    Vertex found = graph.getVertex("2");`

发现返回null。

请帮助!

answer1: 回答1:

You can't find a Vertex this way:

Vertex found = graph.getVertex("2"); 

That method looks a vertex up by id, but there is no vertex in the graph with the id of "2" that is a String. Now, because you have ver1, you do know the actual id of that Vertex so you can do:

Vertex found = graph.getVertex(ver1.getId()); 

Typically, you don't always have that identifier so you want to look things up by your domain data and the indices that you've created. In that case you do:

Vertex found = graph.getVertices("userid","2").next(); 

That will do an index lookup on your userid property. Since that actually returns an Iterator you have to next() off the result. Obviously, in this case, this only returns one result so no worries about anything else in the Iterator but you should probably account for that in some way in some real code.

你不能找到这样的顶点:

Vertex found = graph.getVertex("2"); 

该方法使用ID来查找顶点,但图中没有顶点为“2”,它是一个字符串。现在,因为你有我,你知道那顶点的实际身份所以你能做的:

Vertex found = graph.getVertex(ver1.getId()); 

通常情况下,您并不总是拥有标识符,所以您希望通过您的域数据和您创建的索引来查找数据。如果是那样的话:

Vertex found = graph.getVertices("userid","2").next(); 

这会对你的用户名属性做一个索引查找。因为这实际上返回一个迭代器,你必须next()从结果。显然,在这种情况下,这只返回一个结果,所以不必担心迭代器中的任何东西,但你应该在某些实际代码中以某种方式解释这个结果。

java  titan