Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get id and all properties from a vertex in gremlin?

I am using AWS Neptune with gremlin and I want to get the id of a vertex with all properties of the vertex in one query. How can I do this?

I have tried

g.V().hasLabel('file').valueMap(true) 

but the output is:

{ "fileSize": [ "9170" ], "Gremlin.Net.Process.Traversal.T": "f1fce58306f85ca7050503160640d735c9919c8fc85881d65de80bfe31b5ca24", "mimeType": [ "text/html" ] }

No label and no id is there. The problem with

project('id','label',' fileSize', 'mimeType', 'malwareSource').
    by(id).
    by(label).
    by('fileSize').
    by('mimeType').
    by('malwareSource') 

is that the propertie malwareSource is sometimes part of a file vertex an sometimes not. So if there is no malwareSource property an exception is thrown.

like image 773
Christopher Avatar asked Apr 03 '19 12:04

Christopher


People also ask

How do you get the vertex ID Gremlin?

Just extending on @Stephen's answer; to get the id and the map() output in a nice single Map for each Vertex , just use the plus or leftShift Map operations in the transform method.

How do you get edges in Gremlins?

To create a new edge between two vertices, use the addEdge(v1, v2, label) method. The edge will be created with the label specified. In the example below two vertices are created and assigned to a variable (Gremlin is based on Groovy), then an edge is created between them.

What is Vertex in Gremlin?

public interface Vertex extends Element, org.apache.tinkerpop.gremlin.structure.util.Host. A Vertex maintains pointers to both a set of incoming and outgoing Edge objects. The outgoing edges are those edges for which the Vertex is the tail. The incoming edges are those edges for which the Vertex is the head.

What is out in Gremlin?

outV Outgoing vertex. inV Incoming vertex. How on earth Incoming vertex = the vertex at the other end of the outgoing edge. If anything, i would think that the vertex at the other end of the outgoing edge if the OutV and not InV.


1 Answers

There are lots of ways, but generally use valueMap():

g.V(1).valueMap(true)

In TinkerPop 3.4.0+, the output is a bit better controlled with the addition of by():

g.V(1).hasLabel("person").valueMap().by(unfold()).with(WithOptions.ids)

You could also use project() in various ways, but that requires you to know all the keys you wish to grab. Typically, you should know your keys anyway.

g.V(1).
  project('id','label',' fileSize', 'mimeType', 'malwareSource').
    by(id).
    by(label).
    by('fileSize').
    by('mimeType').
    by('malwareSource')

If a property value is optional to a vertex then just create an if-then condition in the by() modulator:

g.V(1).
  project('id','label',' fileSize', 'mimeType', 'malwareSource').
    by(id).
    by(label).
    by('fileSize').
    by('mimeType').
    by(coalesce(values('malwareSource'),constant('N/A'))
like image 65
stephen mallette Avatar answered Sep 20 '22 06:09

stephen mallette