Monday, 31 January 2022

JanusGraph: Add properties to a vertex property

In JanusGraph, you can attach properties to a vertex property object.

 

Syntax

public default <V> VertexProperty<V> property(final String key, final V value, final Object... keyValues)

Set the provided key to the provided value using default VertexProperty.Cardinality for that key. The default cardinality can be vendor defined and is usually tied to the graph schema. The provided key/values are the properties of the newly created VertexProperty.

 

public <V> VertexProperty<V> property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues)

Create a new vertex property. 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.

 

Example

janusGraphVertex.property("experience", 10.7, "ABC Corp", 3.5, "Delta Inc", 7.2);

 

Find the below working application.

 

AddPropertiesToAVertexProperty.java

package com.sample.app.vertex;

import java.util.Set;

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

public class AddPropertiesToAVertexProperty {

	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("employee");

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

			janusGraphVertex.property("experience", 10.7, "ABC Corp", 3.5, "Delta Inc", 7.2);

			janusGraph.tx().commit();

			JanusGraphVertex persistedVertex = (JanusGraphVertex) janusGraph.vertices(janusGraphVertex.id()).next();
			VertexProperty<? extends Object> experienceProperty = persistedVertex.property("experience");

			String key = experienceProperty.key();
			String value = experienceProperty.value().toString();
			System.out.println(key + " : " + value);

			Set<String> propertiesAssociaetd = experienceProperty.keys();
			for (String property : propertiesAssociaetd) {
				System.out.println("\t" + property + " --> " + experienceProperty.value(property).toString());
			}

		} finally {

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

	}

}

Output

experience : 10.7
	ABC Corp --> 3.5
	Delta Inc --> 7.2
Done!!!

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment