Step 1: Get an instance of JanusGraph.
JanusGraph janusGraph = JanusGraphFactory.open("/Users/Shared/janus.properties");
Step 2: Create a new vertex in the graph with the vertex label.
JanusGraphVertex janusGraphVertex = janusGraph.addVertex("person");
Step 3: Attach properties to the vertex.
janusGraphVertex.property("name", "Krishna");
janusGraphVertex.property("age", 31);
janusGraphVertex.property("gender", 'M');
Step 4: Commit the transaction.
janusGraph.tx().commit();
Find the below working application.
AddVertex.java
package com.sample.app.vertex;
import java.util.Iterator;
import java.util.Set;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;
import org.janusgraph.core.JanusGraphVertex;
public class AddVertex {
public static void main(String args[]) {
JanusGraph 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');
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) {
System.out.println("\t" + property + " -> " + janusGraphVertex.property(property).value());
}
System.out.println("-----------------------------");
janusGraph.close();
System.out.println("Done!!!");
}
}
Output
----------------------------- id : 4184 long id : 4184 label : person properties gender -> M name -> Krishna age -> 31 ----------------------------- Done!!!
No comments:
Post a Comment