问题
In older version of Titan DB (ver 0.5.2) the TitanVertex and TitanEdge implement TitanElement interface that has method getProperties(String key)
that I used to retrieve the element properties values. This method was removed in new version of Titan (I am using version 1.0.0). Instead of this method I found valueOrNull(PropertyKey key)
that does the same thing but receives PropertyKey as parameter and not String as key name.
What is the best way to retrieve the property value/values only using property key name as String object?
Or is there simple way to get PropertyKey object from property key name as String?
回答1:
Titan 1.0 is based on TinkerPop 3. In Titan 1.0, you will find that some methods that you previously called in Titan 0.5 are defined in the TinkerPop interfaces, not in the Titan interfaces.
Looking at the Javadoc for com.thinkaurelius.titan.core.TitanVertex
, you can see that it extends org.apache.tinkerpop.gremlin.structure.Vertex
http://thinkaurelius.github.io/titan/javadoc/1.0.0/com/thinkaurelius/titan/core/TitanVertex.html
You can find the method VertexProperty property(String key)
on org.apache.tinkerpop.gremlin.structure.Vertex
http://tinkerpop.incubator.apache.org/javadocs/3.0.1-incubating/full/org/apache/tinkerpop/gremlin/structure/Vertex.html#property-java.lang.String-
The best way to retrieve the property values on a vertex by using the property key is like this:
gremlin> graph = TitanFactory.build().set('storage.backend','inmemory').open()
==>standardtitangraph[inmemory:[127.0.0.1]]
gremlin> g = graph.traversal()
==>graphtraversalsource[standardtitangraph[inmemory:[127.0.0.1]], standard]
gremlin> v = graph.addVertex('name', 'octopus')
==>v[4296]
gremlin> v.values('name')
==>octopus
You can learn more about vertex properties in the TinkerPop3 documentation here http://tinkerpop.incubator.apache.org/docs/3.0.1-incubating/#vertex-properties
回答2:
I spent some time and found some simple solution of my problem (based on updated answer of Jason Plurad).
Looking at the Javadoc for com.thinkaurelius.titan.core.TitanElement
the method valueOrNull(PropertyKey key)
receives the property key object. The easiest way to get this object is using getPropertyKey(String keyName)
method of com.thinkaurelius.titan.core.TitanTransaction
that return the property key if it exist in Titan schema. http://thinkaurelius.github.io/titan/javadoc/1.0.0/com/thinkaurelius/titan/core/TitanTransaction.html
Example of Java code:
TitanTransaction tt = TitanGraph.newTransaction();
PropertyKey userNameKey = tt.getPropertyKey("userName");
TitanVertex v = tt.getVertex(someUserVertexId);
String userName = v.valueOrNull(userNameKey);
回答3:
You can do this
var = edge.inVertex().property("property").value();
来源:https://stackoverflow.com/questions/33065724/how-to-get-properties-of-vertex-or-edge-elements-in-titan-db-version-1-0-0