Reputation: 182
I am using titan 1.0.0-hadoop1. I am trying to add some list of properties to Vertex that i am creating. In earlier versions such as 0.5.4 you can add property directly with setProperty, but in latest API i find it difficult to add property. I could not even find right solution in the internet.
Please help me in adding the properties to Vertex in Titan Java API.
Upvotes: 1
Views: 728
Reputation: 90
I am also using titan 1.0.0 graph database with cassandra storage backend and has the same problem after upgrading from 0.5.4 version. I found simple generic solution to add any Collection
object (Set
or List
) to the vertex property using this method.
public static void setMultiElementProperties(TitanElement element, String key, Collection collection) {
if (element != null && key != null && collection != null) {
// Put item from collection to the property of type Cardinality.LIST or Cardinality.SET
for (Object item : collection) {
if (item != null)
element.property(key, item);
}
}
}
Same method implementation with Java 8 syntacs:
public static void setMultiElementProperties(TitanElement element, String key, Collection collection) {
if (element != null && key != null && collection != null) {
// Put item from collection to the property of type Cardinality.LIST or Cardinality.SET
collection.stream().filter(item -> item != null).forEach(item -> element.property(key, item));
}
}
TitanElement
object is the parent of TitanVertex
and TitanEdge
objects so you can pass vertex or edge to this method. Of course you need to declare element property first with Cardinality.Set or Cardinality.List using TitanManagement to use the multi value property.
TitanManagement tm = tittanGraph.openManagement();
tm.makePropertyKey(key).cardinality(Cardinality.LIST).make();// or Cardinality.SET
tm.commit();
To retrieve the collection from element property you can simple use:
Iterator<Object> collectionIter = element.values(key);
And this is Java 8 way to iterate over it:
List<Object> myList = new ArrayList<>();
collectionIter.forEachRemaining(item -> myList.add(item));
Upvotes: 0
Reputation: 3565
An example will help:
Vertex vertex = graph.addVertex();
vertex.property("ID", "123"); //Creates ID property with value 123
creates the property. To query the property:
vertex.property("ID"); //Returns the property object
vertex.value("ID"); //Returns "123"
vertex.values(); //Returns all the values of all the properties
When you having difficulty understanding the Titan API. I recommend looking at the TinkerPop API. Titan implements it so all tinkerpop commands work with titan graphs.
Upvotes: 1