Posted in Graph Database

TinkerpopGraph 02: Gremlin TinkerGraph: Set Properties Examples

Days ago, when I tried to set a property to a vertex by using:

vertex.property("colored", color);

It seems very straight forword, but actually I met this error:
Exception in thread “main” java.lang.AbstractMethodError: Method ….
Googled but no answers.
In the Javadoc:

default VertexProperty property(String key, V value, Object… keyValues)
//Set the provided key to the provided value using default VertexProperty.Cardinality for that key.

VertexProperty.Cardinality has three types: list, set and single.
If you want to set only one property, then use single only. I did not try in person about list and set.In my example, the first parameter can be eliminated. After the Cardinality, there is the key and value pair.

Well, make sure you are using:
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;

Get properties and make changes:

//...
		if(graph.vertices(1).hasNext()){
			Iterator<VertexProperty<Double>> properties = graph.vertices(1).next().properties();
			while (properties.hasNext()) {
				VertexProperty<Double> vProperty = properties.next();
				System.out.println(vProperty.toString());
			}
			System.out.println("Get "+graph.vertices(1).next().property("colored"));
			graph.vertices(1).next().property(VertexProperty.Cardinality.single,"colored",true);
			System.out.println("After "+graph.vertices(1).next().property("colored"));

		}

OutPut:

vp[weight->234.26392670343824]
vp[colored->false]
Get vp[colored->false]
After vp[colored->true]

Use if statement to check the value of a given id property. Do not forget to use value( ).

     if(graph.vertices(1).next().property("colored").value().equals(true)){
				System.out.println("True");
			}else{
				System.out.println("False");
			}

In the example, the datatype is boolean.

Author:

Keep calm and update blog.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s