TinkerpopGraph 01: Easy API usage, examples

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

		//Create a new TinkerGraph
		Graph graph = TinkerGraph.open();
		//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 = vertexIterator.next();
			System.out.println("Now is Vertex "+ vertex.id()+"\t");
		//Iterate edges
		Iterator<Edge> edges = graph.edges();
			Edge edge = edges.next();
			System.out.println("Now is Edge"+ edge.id()+"\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 = TinkerGraph.open(); 
		Vertex marko = graph.addVertex(T.label, "person", T.id, 1, "name", "marko", "age", 29);
		Vertex vadas = graph.addVertex(T.label, "person", T.id, 2, "name", "vadas", "age", 27);
		marko.addEdge("knows", vadas, T.id, 7, "weight", 0.5f);

Here, T.id 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 "+ vertex.id()+"\t");
			//Get ajacent vertices
			Iterator<Vertex> ajacent= vertex.vertices(Direction.OUT);
				Vertex neighbourVertex = ajacent.next();

Please check API here:




http://grids.ucs.indiana.edu/ptliupages/NPAC-SCCSIndex-PDFPreserved/sccs-0666.pdf (Graph Coloring)

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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