Amelia O'Leary
Amelia O'Leary

Reputation: 31

Retrieve both edges and vertices in gremlin json

Hi I'm new to gremlin and need to, basically, get everything current to return as a json file. I'm using Syndeia which has a raw query option. I'm having difficulty figuring out how to get both vertices and edges to output. This is what I have thus far:

g.V().has('sLabel','Artifact').has('_isLatest','TRUE').both()

But I also need

g.V().has('sLabel','Artifact').has('_isLatest','TRUE').bothE()

Which gives me all the edges I need, but not the vertices. I realize that g.V() gives vertices and g.E() gives edges, but am unsure how to obtain both/combine them in the one line query. My json comes back as either

         "vertices":[] 0 items
         "edges":[...] 90 items 

Or

         "vertices":[...] 90 items
         "edges":[] 0 items

My next idea is to do multiple queries into java to parse, combine, and manipulate there. I would really like to avoid doing this, but will if it's the only way. Thank you for being kind and understanding and for your help. I appreciate you all.

Upvotes: 3

Views: 839

Answers (2)

You can do it as follows,

Just see the code below have demonstrated the following,

  1. Vertex query
  2. Edge query
  3. combined query
  4. converted combined query into valueMap
  5. Converted the combined query valueMap into JSON (using version 3)
  6. Converted the combined query valueMap into JSON (using version 1)

Vertex Query

    gremlin> g.V(131200).both()
    ==>v[40992]
    ==>v[12336]
    ==>v[20608]
    ==>v[180376]
    gremlin>

Edge query

    gremlin> g.V(131200).bothE()
    ==>e[7rps-2t8g-1fdh-vmo][131200-locatedin->40992]
    ==>e[3qpy-9io-1bf9-2t8g][12336-containedin->131200]
    ==>e[7rbk-fwg-1bf9-2t8g][20608-containedin->131200]
    ==>e[7l03-3v6g-1bf9-2t8g][180376-containedin->131200]
    gremlin>

Combined query

    gremlin> 
    gremlin>g.V(131200).project('nodes','edges').by(both().fold()).by(bothE().fold())
    ==>[nodes:[v[40992],v[12336],v[20608],v[180376]],edges:[e[7rps-2t8g-1fdh-vmo][131200-locatedin->40992],e[3qpy-9io-1bf9-2t8g][12336-containedin->131200],e[7rbk-fwg-1bf9-2t8g][20608-containedin->131200],e[7l03-3v6g-1bf9-2t8g][180376-containedin->131200]]]
    gremlin>

converted combined query into valueMap

    gremlin> 
    gremlin> output = g.V(131200).project('nodes','edges').by(both().valueMap().fold()).by(bothE().valueMap().fold()).next()
    ==>nodes=[{gId=[0b98e8d5-681d-4155-8aaf-5d86babc0cff], isDeleted=[FALSE], isEntity=[TRUE], name=[TotsukaCDC], basetype=[GeoLocation], source=[RCP-Base], type=[DC], geographyL2DisplayName=[KANAGAWA], geographyL2Name=[KANAGAWA], geographyL4Name=[KNG-TOTSUKAKU-NORTH], geographyL4DisplayName=[KNG-TOTSUKAKU-NORTH], displayName=[TotsukaCDC], latitude=[139.525166], locationType=[CDC], geographyL1Name=[KANTO], geographyL3DisplayName=[YOKOHAMA-SHI], geographyL3Name=[YOKOHAMA-SHI], geographyL1DisplayName=[KANTO], locationCode=[tt], longitude=[35.416535]}, {gId=[90e7407c-cd34-44b0-9bf9-1a8866aac429], isDeleted=[FALSE], isEntity=[TRUE], name=[UHN2KNGcdcttRA01_HundredGigE0/0/0/6], basetype=[Entity], source=[RCP-Base], type=[PhysicalPort], portInUse=[TRUE], portNum=[HundredGigE0/0/0/6]}, {gId=[d0fc7284-4cd8-4cba-8d3d-be3c4ca2ba31], isDeleted=[FALSE], isEntity=[TRUE], name=[UHN2KNGcdcttRA01_Hu0/2/0/35], basetype=[Entity], source=[RCP-Base], type=[PhysicalPort], portInUse=[TRUE], portNum=[Hu0/2/0/35]}, {gId=[2f4f5a3a-b88f-4915-acd5-a1d873507f11], isDeleted=[FALSE], isEntity=[TRUE], name=[UHN2KNGcdcttRA01_HundredGigE0/1/0/6], basetype=[Entity], source=[RCP-Base], type=[PhysicalPort], portInUse=[TRUE], portNum=[HundredGigE0/1/0/6]}]
    ==>edges=[{destName=TotsukaCDC, srcName=UHN2KNGcdcttRA01, gId=1133ff0f-2f4b-4d21-b2d6-fed796ed13f9}, {gId=220d7d2b-8b96-4331-bdc1-50127685946a, destName=UHN2KNGcdcttRA01, srcName=UHN2KNGcdcttRA01_HundredGigE0/0/0/6}, {gId=ebd78fc0-a66c-4033-a776-f80210c02e63, destName=UHN2KNGcdcttRA01, srcName=UHN2KNGcdcttRA01_Hu0/2/0/35}, {srcName=UHN2KNGcdcttRA01_HundredGigE0/1/0/6, gId=0df0f3d0-037a-4caa-9f81-6a6df1c22e25, destName=UHN2KNGcdcttRA01}]
    gremlin>

Converted the combined query valueMap into JSON (Using Version 3)

    gremlin> 
    gremlin> mapper = GraphSONMapper.build().version(GraphSONVersion.V3_0).create().createMapper()
    ==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper@10a907ec
    gremlin>
    gremlin> mapper.writeValueAsString(output) // output is a variable in which data is collected in step 4
    ==>{"@type":"g:Map","@value":["nodes",{"@type":"g:List","@value":[{"@type":"g:Map","@value":["gId",{"@type":"g:List","@value":["0b98e8d5-681d-4155-8aaf-5d86babc0cff"]},"isDeleted",{"@type":"g:List","@value":["FALSE"]},"isEntity",{"@type":"g:List","@value":["TRUE"]},"name",{"@type":"g:List","@value":["TotsukaCDC"]},"basetype",{"@type":"g:List","@value":["GeoLocation"]},"source",{"@type":"g:List","@value":["RCP-Base"]},"type",{"@type":"g:List","@value":["DC"]},"geographyL2DisplayName",{"@type":"g:List","@value":["KANAGAWA"]},"geographyL2Name",{"@type":"g:List","@value":["KANAGAWA"]},"geographyL4Name",{"@type":"g:List","@value":["KNG-TOTSUKAKU-NORTH"]},"geographyL4DisplayName",{"@type":"g:List","@value":["KNG-TOTSUKAKU-NORTH"]},"displayName",{"@type":"g:List","@value":["TotsukaCDC"]},"latitude",{"@type":"g:List","@value":[{"@type":"g:Double","@value":139.525166}]},"locationType",{"@type":"g:List","@value":["CDC"]},"geographyL1Name",{"@type":"g:List","@value":["KANTO"]},"geographyL3DisplayName",{"@type":"g:List","@value":["YOKOHAMA-SHI"]},"geographyL3Name",{"@type":"g:List","@value":["YOKOHAMA-SHI"]},"geographyL1DisplayName",{"@type":"g:List","@value":["KANTO"]},"locationCode",{"@type":"g:List","@value":["tt"]},"longitude",{"@type":"g:List","@value":[{"@type":"g:Double","@value":35.416535}]}]},{"@type":"g:Map","@value":["gId",{"@type":"g:List","@value":["90e7407c-cd34-44b0-9bf9-1a8866aac429"]},"isDeleted",{"@type":"g:List","@value":["FALSE"]},"isEntity",{"@type":"g:List","@value":["TRUE"]},"name",{"@type":"g:List","@value":["UHN2KNGcdcttRA01_HundredGigE0/0/0/6"]},"basetype",{"@type":"g:List","@value":["Entity"]},"source",{"@type":"g:List","@value":["RCP-Base"]},"type",{"@type":"g:List","@value":["PhysicalPort"]},"portInUse",{"@type":"g:List","@value":["TRUE"]},"portNum",{"@type":"g:List","@value":["HundredGigE0/0/0/6"]}]},{"@type":"g:Map","@value":["gId",{"@type":"g:List","@value":["d0fc7284-4cd8-4cba-8d3d-be3c4ca2ba31"]},"isDeleted",{"@type":"g:List","@value":["FALSE"]},"isEntity",{"@type":"g:List","@value":["TRUE"]},"name",{"@type":"g:List","@value":["UHN2KNGcdcttRA01_Hu0/2/0/35"]},"basetype",{"@type":"g:List","@value":["Entity"]},"source",{"@type":"g:List","@value":["RCP-Base"]},"type",{"@type":"g:List","@value":["PhysicalPort"]},"portInUse",{"@type":"g:List","@value":["TRUE"]},"portNum",{"@type":"g:List","@value":["Hu0/2/0/35"]}]},{"@type":"g:Map","@value":["gId",{"@type":"g:List","@value":["2f4f5a3a-b88f-4915-acd5-a1d873507f11"]},"isDeleted",{"@type":"g:List","@value":["FALSE"]},"isEntity",{"@type":"g:List","@value":["TRUE"]},"name",{"@type":"g:List","@value":["UHN2KNGcdcttRA01_HundredGigE0/1/0/6"]},"basetype",{"@type":"g:List","@value":["Entity"]},"source",{"@type":"g:List","@value":["RCP-Base"]},"type",{"@type":"g:List","@value":["PhysicalPort"]},"portInUse",{"@type":"g:List","@value":["TRUE"]},"portNum",{"@type":"g:List","@value":["HundredGigE0/1/0/6"]}]}]},"edges",{"@type":"g:List","@value":[{"@type":"g:Map","@value":["destName","TotsukaCDC","srcName","UHN2KNGcdcttRA01","gId","1133ff0f-2f4b-4d21-b2d6-fed796ed13f9"]},{"@type":"g:Map","@value":["gId","220d7d2b-8b96-4331-bdc1-50127685946a","destName","UHN2KNGcdcttRA01","srcName","UHN2KNGcdcttRA01_HundredGigE0/0/0/6"]},{"@type":"g:Map","@value":["gId","ebd78fc0-a66c-4033-a776-f80210c02e63","destName","UHN2KNGcdcttRA01","srcName","UHN2KNGcdcttRA01_Hu0/2/0/35"]},{"@type":"g:Map","@value":["srcName","UHN2KNGcdcttRA01_HundredGigE0/1/0/6","gId","0df0f3d0-037a-4caa-9f81-6a6df1c22e25","destName","UHN2KNGcdcttRA01"]}]}]}
    gremlin>

Converted the combined query valueMap into JSON (Using Version 1)

    gremlin> 
    gremlin> mapper1 = GraphSONMapper.build().version(GraphSONVersion.V1_0).create().createMapper()
    ==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper@1a7163e3
    gremlin>
    gremlin> mapper1.writeValueAsString(output)
    ==>{"nodes":[{"gId":["0b98e8d5-681d-4155-8aaf-5d86babc0cff"],"isDeleted":["FALSE"],"isEntity":["TRUE"],"name":["TotsukaCDC"],"basetype":["GeoLocation"],"source":["RCP-Base"],"type":["DC"],"geographyL2DisplayName":["KANAGAWA"],"geographyL2Name":["KANAGAWA"],"geographyL4Name":["KNG-TOTSUKAKU-NORTH"],"geographyL4DisplayName":["KNG-TOTSUKAKU-NORTH"],"displayName":["TotsukaCDC"],"latitude":[139.525166],"locationType":["CDC"],"geographyL1Name":["KANTO"],"geographyL3DisplayName":["YOKOHAMA-SHI"],"geographyL3Name":["YOKOHAMA-SHI"],"geographyL1DisplayName":["KANTO"],"locationCode":["tt"],"longitude":[35.416535]},{"gId":["90e7407c-cd34-44b0-9bf9-1a8866aac429"],"isDeleted":["FALSE"],"isEntity":["TRUE"],"name":["UHN2KNGcdcttRA01_HundredGigE0/0/0/6"],"basetype":["Entity"],"source":["RCP-Base"],"type":["PhysicalPort"],"portInUse":["TRUE"],"portNum":["HundredGigE0/0/0/6"]},{"gId":["d0fc7284-4cd8-4cba-8d3d-be3c4ca2ba31"],"isDeleted":["FALSE"],"isEntity":["TRUE"],"name":["UHN2KNGcdcttRA01_Hu0/2/0/35"],"basetype":["Entity"],"source":["RCP-Base"],"type":["PhysicalPort"],"portInUse":["TRUE"],"portNum":["Hu0/2/0/35"]},{"gId":["2f4f5a3a-b88f-4915-acd5-a1d873507f11"],"isDeleted":["FALSE"],"isEntity":["TRUE"],"name":["UHN2KNGcdcttRA01_HundredGigE0/1/0/6"],"basetype":["Entity"],"source":["RCP-Base"],"type":["PhysicalPort"],"portInUse":["TRUE"],"portNum":["HundredGigE0/1/0/6"]}],"edges":[{"destName":"TotsukaCDC","srcName":"UHN2KNGcdcttRA01","gId":"1133ff0f-2f4b-4d21-b2d6-fed796ed13f9"},{"gId":"220d7d2b-8b96-4331-bdc1-50127685946a","destName":"UHN2KNGcdcttRA01","srcName":"UHN2KNGcdcttRA01_HundredGigE0/0/0/6"},{"gId":"ebd78fc0-a66c-4033-a776-f80210c02e63","destName":"UHN2KNGcdcttRA01","srcName":"UHN2KNGcdcttRA01_Hu0/2/0/35"},{"srcName":"UHN2KNGcdcttRA01_HundredGigE0/1/0/6","gId":"0df0f3d0-037a-4caa-9f81-6a6df1c22e25","destName":"UHN2KNGcdcttRA01"}]}

Upvotes: 0

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14391

You can write a Gremlin query that gives you both the vertices and the edges. There are a few ways to do that. Probably the easiest way is to use path as shown below.

g.V().has('sLabel','Artifact').
      has('_isLatest','TRUE').
      bothE().
      otherV().
      path()

as an alternative, you could do:


g.V().has('sLabel','Artifact').
      has('_isLatest','TRUE').
      bothE().as('e').
      otherV().as('v').
      select('v','e')

Upvotes: 3

Related Questions