简体   繁体   中英

Compiling responses of gremlin traversal query

I have the following graph structure:

Vertexes -- Campaign, Project, Lead

{"Name":["CompanyV"],"sid":["SidFromSQL"]}
{"name":["Campaign3V"],"status":["paused"]}
{"name":["Campaign1"],"startDate":["Jan 1, 2019 5:30:00 AM"]}
{"name":["Campaign2V"],"status":["active"]}
{"name":["Lead11V"]}
{"name":["Lead2V"]}
{"name":["Project1V"],"Name":[""],"sid":["SidFromSQL"]}
{"name":["Project2V"],"Name":[""],"sid":["SidFromSQL"]}
{"name":["Lead3V"]}
{"name":["Campaign1V"],"status":["active"]}

Edges:

{"inVertex":{"id":"58b6e79f-6809-6fc4-9f0a-c8a26337a729","label":"Campaign"},"outVertex":{"id":"1cb6e79d-1ca7-4d3c-e71d-c05c13abac15","label":"Project"},"id":"a6b6e79f-6809-87bf-a535-eec9101e683c","label":"hasCampaign"}
{"inVertex":{"id":"c4b6e7ae-64d3-b8b9-ce7b-c319e7ed70ca","label":"Lead"},"outVertex":{"id":"1cb6e79d-1ca7-4d3c-e71d-c05c13abac15","label":"Project"},"id":"6cb6e7ae-64d4-6fb0-9314-411eb72d9a28","label":"hasLead"}
{"inVertex":{"id":"a2b6e79d-1ca7-db4e-f19f-2ef1df514ade","label":"Project"},"outVertex":{"id":"64b6e79c-d58b-37ad-6c3e-63a783e6df97","label":"Company"},"id":"4eb6e7a8-451f-4365-1d79-d5d118b5ff56","label":"hasProject"}
{"inVertex":{"id":"c4b6e7ae-64d3-b8b9-ce7b-c319e7ed70ca","label":"Lead"},"outVertex":{"id":"58b6e79f-6809-6fc4-9f0a-c8a26337a729","label":"Campaign"},"id":"96b6e7ae-64d4-b918-353d-fccc13cbd9bb","label":"hasLead"}
{"inVertex":{"id":"94b6e79f-69b9-ccfe-d9e6-a41c4be59979","label":"Campaign"},"outVertex":{"id":"a2b6e79d-1ca7-db4e-f19f-2ef1df514ade","label":"Project"},"id":"34b6e79f-69b9-bd15-9331-0551c464f222","label":"hasCampaign"}
{"inVertex":{"id":"36b6e7b2-3d78-3229-9ebd-05c2c5f5927b","label":"Lead"},"outVertex":{"id":"1cb6e79d-1ca7-4d3c-e71d-c05c13abac15","label":"Project"},"id":"c2b6e7b2-3d78-16d0-ee95-46b66108236e","label":"hasLead"}
{"inVertex":{"id":"36b6e7b2-3d78-3229-9ebd-05c2c5f5927b","label":"Lead"},"outVertex":{"id":"58b6e79f-6809-6fc4-9f0a-c8a26337a729","label":"Campaign"},"id":"04b6e7b2-3d79-3d95-855e-a206d38b8603","label":"hasLead"}
{"inVertex":{"id":"1cb6e79d-1ca7-4d3c-e71d-c05c13abac15","label":"Project"},"outVertex":{"id":"64b6e79c-d58b-37ad-6c3e-63a783e6df97","label":"Company"},"id":"ccb6e7a8-449b-d92c-1330-4f6288ab0852","label":"hasProject"}
{"inVertex":{"id":"7eb6e7dc-94f9-ca83-df4c-87284897151f","label":"Lead"},"outVertex":{"id":"1cb6e79d-1ca7-4d3c-e71d-c05c13abac15","label":"Project"},"id":"d2b6e7dc-94fb-8219-30a2-d304ccaed75d","label":"hasLead"}
{"inVertex":{"id":"d0b6e79f-692a-8c03-112e-9388e54b1f9d","label":"Campaign"},"outVertex":{"id":"1cb6e79d-1ca7-4d3c-e71d-c05c13abac15","label":"Project"},"id":"3cb6e79f-692b-4cad-48ac-1e4991e75b60","label":"hasCampaign"}

I am running the below query to fetch the Leads associated with a Project and a specific campaign.

        GraphTraversal t =g.V("1cb6e79d-1ca7-4d3c-e71d-c05c13abac15").out("hasLead")
                .where(in("hasLead").has("Campaign","name","Campaign1V"));

This is returning the information about the Leads in the output, I wanted to know if there is a way in which i can get the specific Campaign information as well as the ID information in the output response (using a single traversal statement) so that this can be utilised by the UI component to render in an HTML.

You just need to transform your result into the form that you want. In this case you can use something like project()

g.V("1cb6e79d-1ca7-4d3c-e71d-c05c13abac15").out("hasLead").
  where(__.in("hasLead").has("Campaign","name","Campaign1V")).
  project('lead','campaign').
    by().
    by(__.in("hasLead").has("Campaign","name","Campaign1V").fold())

You might want to include something in that first by() modulator to further transform the vertex to the properties you want and you might want to do the same for the second as well. Furthermore, the fold() is only necessary if you have more than one campaign per lead.

So, the above works nicely and is easy to follow, but it does traverse the same "hasLead" edges twice. You can avoid that, but it adds a bit of misdirection to readability that you have to decide if you can live with:

g.V("1cb6e79d-1ca7-4d3c-e71d-c05c13abac15").out("hasLead").
  project('lead','campaign').
    by().
    by(__.in("hasLead").has("Campaign","name","Campaign1V").fold()).
  filter(select('campaign').unfold())

Now you project all of the "leads" but filter away any that have an empty list for the "campaign".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM