Tuesday 22 March 2016

Tutorial 2: AddV() for adding a vertex to graph

Titan Graph DB Tutorial: Getting Started

Gremlin is awesome

Now that you installed Titan and tried few things like V() and E(), let’s learn how to do more interesting things with Gremlin.

But before that, let’s ask ourselves what is Gremlin?

Gremlin is a query language that we use to query and mutate a graph that supports Tinkerpop3. So, you can use Gremlin to add vertex or edge, update one edge or vertex or update many of them. You could also query the graph for data or for relationships. Let me show you how.

The Gremlin console is very friendly, try this:

1+1

See? It’s cool. It can do a lot. So now let’s

Open empty graph

You could always start an empty in-memory graph using Gremlin console.

In-memory graph means when you close the program, everything will be gone so remember that very well.

graph = TinkerGraph.open()
g=graph.traversal()

I want to add a vertex

g.addV('person')

You have just created a vertex and gave it a label person.

The label of the vertex helps distinguish between different types of vertexes in your graph DB. So if you have persons, places and pages. You could assign the appropriate label for each one to distinguish between them later with your database is big.

OR

v=g.addV('person')

You assigned the new created vertex into an object called v. Now you could do whatever you want with it.

OR

g.addV('person').property("name", "Mo")

You created a vertex and added a property to it. The property has two main elements, key and value. So this person is called Mo. Great!

Each vertex can have an unlimited number of key/value pairs. You could add as many as you want.

Interested more in properties? Go to next class here.

Please don’t forget to subscribe to my blog to get all updated straight to your inbox for FREE.

4 comments :

  1. Thanks you for posting content around Titan. I think the horisontal scalability and the open sourcy dev makes it the best graph db right now. And John Wooden rocks.

    ReplyDelete
  2. When I run g.addV('person') it says that I need to provide args in a multiple of 2 as key/value pairs. Is it possible the update way to do this is g.addV("label", "person")?

    ReplyDelete
  3. When I run g.addV('person') it says that I need to provide args in a multiple of 2 as key/value pairs. Is it possible the update way to do this is g.addV("label", "person")?

    ReplyDelete
  4. I had to add the vertex like this:
    g.addV(label, 'person', 'name', 'Scott')

    Otherwise I got an error:
    The provided key/value array length must be a multiple of two

    ReplyDelete