Monday 7 February 2022

JanusGraph: change or update schema elements

Once edge label, property key, or vertex label defined and committed to the graph, their definition can't be changed. But you can change the name of schema elements.

 

Using 'changeName' method of JanusGraphManagement interface, you can change the name of schema element.

 

Signature

void changeName(JanusGraphSchemaElement element, String newName)

 

Example

vertexLabel = janusGraphManagement.getVertexLabel("spreadsheet");

janusGraphManagement.changeName(vertexLabel, "application_software");

 

Find the below working application.

 

ChangeSchemaElementName.java

package com.sample.app.schema;

import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.VertexLabel;
import org.janusgraph.core.schema.JanusGraphManagement;
import org.janusgraph.core.schema.VertexLabelMaker;

public class ChangeSchemaElementName {
	public static void main(String args[]) {

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

			JanusGraphManagement janusGraphManagement = janusGraph.openManagement();

			VertexLabelMaker vertexLabelMaker = janusGraphManagement.makeVertexLabel("spreadsheet");
			VertexLabel vertexLabel = vertexLabelMaker.make();

			janusGraph.tx().commit();

			vertexLabel = janusGraphManagement.getVertexLabel("spreadsheet");
			janusGraphManagement.changeName(vertexLabel, "application_software");

			janusGraph.tx().commit();

			String vertexLabels = janusGraphManagement.printVertexLabels();

			System.out.println(vertexLabels);
		} finally {
			System.out.println("\nDone!!!");
		}

	}
}

 

Output

------------------------------------------------------------------------------------------------
Vertex Label Name              | Partitioned | Static                                             |
---------------------------------------------------------------------------------------------------
application_software           | false       | false                                              |
---------------------------------------------------------------------------------------------------


Done!!!

 

Will the schema name changes reflect immediately?

No, not exactly.  When schema name changes are notified to all the JanusGraph instances via the storage backend, it will take a while for the schema changes to take effect.

 

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment