Posted in Graph Database

TinkerpopGraph 01: Easy API usage, examples

import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;

		//Create a new TinkerGraph
		Graph graph =;
		//Create vertexes
		Vertex a = graph.addVertex("Irene");
		Vertex b = graph.addVertex("Li");
		Vertex c = graph.addVertex("Zihui");
		//Create edges: label, directed vertex
		a.addEdge("0", b);
		b.addEdge("1", a);
		a.addEdge("2", c);

		//Iterate vertexes
		//continuesly get vertex from 0 to the last one.
		Iterator<Vertex> vertexIterator = graph.vertices();
			Vertex vertex =;
			System.out.println("Now is Vertex "+"\t");
		//Iterate edges
		Iterator<Edge> edges = graph.edges();
			Edge edge =;
			System.out.println("Now is Edge"+"\t");
		System.out.println("Graph:"+ graph.toString());

	        //Check if a vertex is exist:
		// Iterate edges
		Iterator<Edge> edges = graph.edges(19);
			System.out.println("Null!" + "\t");

    • Output:

Read In from a CSV file.

The .csv file is shown below:

In line1: 44 -> 99, with the edge attributes being 0.5832..

So we can read in the cdv, then add vertices and edges.

  • More about adding:

Graph graph =; 
		Vertex marko = graph.addVertex(T.label, "person",, 1, "name", "marko", "age", 29);
		Vertex vadas = graph.addVertex(T.label, "person",, 2, "name", "vadas", "age", 27);
		marko.addEdge("knows", vadas,, 7, "weight", 0.5f);

Here, is the type of int, T.label is String type. Following by key-value pairs, separated by commars.

Label can be different, Id cannot, or it will be an excepetion.

  • To get all the neighbours of a given vertex(query by a given ID):
                        System.out.println("Now is Vertex "+"\t");
			//Get ajacent vertices
			Iterator<Vertex> ajacent= vertex.vertices(Direction.OUT);
				Vertex neighbourVertex =;

Please check API here:

Study: (Graph Coloring)


Keep calm and update blog.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s