Sunday, 30 January 2022

JanusGraph: Add multiple values to a vertex property

By specifying the vertex property cardinality, we can add multiple values to a vertex property. VertexProperty.Cardinality specifies the vertex cardinality.

public interface VertexProperty<V> extends Property<V>, Element {

    public enum Cardinality {
        single, list, set
    }


    .......
    .......
}

 

Example

janusGraphVertex.property(VertexProperty.Cardinality.set, "hobbies", "cricket");
janusGraphVertex.property(VertexProperty.Cardinality.set, "hobbies", "football");
janusGraphVertex.property(VertexProperty.Cardinality.set, "hobbies", "tennis");

If the cardinality is VertexProperty.Cardinality#single, then set the key to the value. If the cardinality is  VertexProperty.Cardinality#list, then add a new value to the key. If the cardinality is VertexProperty.Cardinality#set, then only add a new value if that value doesn't already exist for the key. If the value already exists for the key, add the provided key value vertex property properties to it.

 

AddMultipleValuesToAProperty.java

 

package com.sample.app.vertex;

import java.util.Iterator;
import java.util.Set;
import java.util.StringJoiner;

import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;

public class AddMultipleValuesToAProperty {

	public static void main(String args[]) {
		JanusGraph janusGraph = null;

		try {
			janusGraph = JanusGraphFactory.open("/Users/Shared/janus.properties");

			// Create a person vertex
			JanusGraphVertex janusGraphVertex = janusGraph.addVertex("person");

			// Add properties to the vertex
			janusGraphVertex.property("name", "Krishna");
			janusGraphVertex.property("age", 31);
			janusGraphVertex.property("gender", 'M');

			janusGraphVertex.property(VertexProperty.Cardinality.set, "hobbies", "cricket");
			janusGraphVertex.property(VertexProperty.Cardinality.set, "hobbies", "football");
			janusGraphVertex.property(VertexProperty.Cardinality.set, "hobbies", "tennis");

			janusGraph.tx().commit();

			Iterator<Vertex> janusGraphVertexes = janusGraph.vertices(janusGraphVertex.id());
			Vertex vertex = janusGraphVertexes.next();

			System.out.println("-----------------------------");
			System.out.println("id : " + vertex.id());
			System.out.println("long id : " + ((JanusGraphVertex) vertex).longId());
			System.out.println("label : " + vertex.label());

			System.out.println("properties");
			Set<String> properties = janusGraphVertex.keys();
			for (String property : properties) {

				if ("hobbies".equals(property)) {
					Iterator<? extends Property<Object>> propertyIterator = janusGraphVertex.properties(property);

					StringJoiner hobbies = new StringJoiner(",");
					while (propertyIterator.hasNext()) {
						hobbies.add(propertyIterator.next().value().toString());
					}

					System.out.println("\t" + property + " -> " + hobbies.toString());
				} else {
					System.out.println("\t" + property + " -> " + janusGraphVertex.property(property).value());
				}

			}
			System.out.println("-----------------------------");
		} finally {

			if (janusGraph != null)
				janusGraph.close();
			System.out.println("Done!!!");
		}

	}

}

 

Output

-----------------------------
id : 40964264
long id : 40964264
label : person
properties
	gender -> M
	hobbies -> cricket,football,tennis
	name -> Krishna
	age -> 31
-----------------------------
Done!!!

 

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment