Search code examples
node.jscassandragremlinjanusgraphgremlin-server

Get data from gremlin database and iterate in nodejs


I am new to gremlin. Using nodejs I connected gremlin and added few vertices.

Assume I have 10 different vertices and connected with edges. Is there any way to read and iterate the data in nodejs. Its like simle select query with a condition.. (select * from users where username='john')

 async get_vertices_by_props(input) {
        
        var graph_data = await this.get_vertex(input.label,input.property_name)
         // some code here.. 
     
    }

async get_vertex(label, property) {
        if (!label || !property || !value) {
            return error;
        }
        return await this.g.V().hasLabel(label);
    }

Solution

  • Assuming that you have added "username" as a property on the vertices and "users" is a label on vertices, the Gremlin equivalent of SQL select * from users where username='john' is g.V().hasLabel('users').has('username','john').valueMap(true).

    In the above query:

    V() gives you all vertices.

    hasLabel('users') is a filter applies on label of vertices.

    has('username','john') is a filter condition on properties of the vertices.

    valueMap is a projection operator would provide you with the ID, label and all the properties of the vertices.

    Since you are beginning to learn, may I suggest reading through https://kelvinlawrence.net/book/Gremlin-Graph-Guide.html for an initial introduction to Gremlin.