Search code examples
gremlin

How to get the results from properties of different verticies in gremlin?


I have this Database:

Clients => Incident => File => Filename

  • Clients have an ID
  • Incidents have an ID and a reportedOn property
  • Files have an ID and a fileSize, mimeType, malware property
  • Filenames have an ID

Client have a outgoing Edge to Incidents (reported), incident have a outgoing Edge to file (containsFile), file have a outgoing Edge to filename (hasName).

What Query I have to execute in gremlin to get the filename-ID, the file-ID, the file-fileSize and the incident-reportedOn values in one result?

Here is some sample DATA:

g.addV('client').property('id','1')
  addV('incident').property('id','11').property('reportedON'2/15/2019 8:01:19 AM')
  addV('file').property('id','100').property('fileSize', '432534')
  addV('fileName').property('id','file.pdf')
  addE('reported').from('1').to('11').
  addE('containsFile').from('11').to('100').
  addE('hasName').from('100').to('file.pdf').iterate()

Solution

  • The traversal you've posted to create the sample data contains dozens of errors. Don't be a pain in the ass, double-check what you post.

    Anyway, here's a fixed version of your query:

    g.addV('client').property('id','1').as('1').
      addV('incident').property('id','11').property('reportedON', '2/15/2019 8:01:19 AM').as('11').
      addV('file').property('id','100').property('fileSize', '432534').as('100').
      addV('fileName').property('id','file.pdf').as('file.pdf').
      addE('reported').from('1').to('11').
      addE('containsFile').from('11').to('100').
      addE('hasName').from('100').to('file.pdf').iterate()
    

    get the filename-ID, the file-ID, the file-fileSize and the incident-reportedOn values

    gremlin> g.V().has('client','id','1').
    ......1>   out('reported').as('incident').
    ......2>   out('containsFile').
    ......3>   out('hasName').
    ......4>   path().
    ......5>     from('incident').
    ......6>     by(union(group().
    ......7>                by(label).
    ......8>                by('id'),
    ......9>              valueMap()).
    .....10>     unfold().
    .....11>   filter(select(keys).is(neq('id'))).
    .....12>   group().
    .....13>     by(keys).
    .....14>     by(select(values).unfold())).
    .....15>   unfold().unfold().
    .....16>   group().
    .....17>     by(keys).
    .....18>     by(select(values).unfold())
    ==>[fileName:file.pdf,file:100,reportedON:2/15/2019 8:01:19 AM,fileSize:432534,incident:11]
    

    Only getting the path().from('incident').by(valueMap()) alone would already give you everything you need. However, I added a bit of re-grouping to get a nicer formatted result.