I am new Gremlin and trying to only select few properties of from
and to
vertices from a sample graph along with the property id
of the from
vertex. Here is how I created the graph:
g.addV('asset').property(id, 'a1').property('ip', '127.4.8.51').property('scanDate', '2020-09-10').property('repoId', 1)
g.addV('asset').property(id, 'a2').property('ip', '127.4.8.55').property('scanDate', '2020-09-20').property('repoId', 1)
g.addV('asset').property(id, 'a3').property('ip', '127.4.8.57').property('scanDate', '2020-09-21').property('repoId', 1)
g.addV('asset').property(id, 'a4').property('ip', '127.4.10.36').property('scanDate', '2020-09-12').property('repoId', 2)
g.addV('asset').property(id, 'a5').property('ip', '127.4.10.75').property('scanDate', '2020-09-14').property('repoId', 2)
g.addV('repo').property(id, 'r1').property('repoName', 'repo1').property('assetAge', 10).property('repoId', 1)
g.addV('repo').property(id, 'r2').property('repoName', 'repo2').property('assetAge', 9).property('repoId', 2)
g.V('a1').addE('has').to(g.V('r1'))
g.V('a2').addE('has').to(g.V('r1'))
g.V('a3').addE('has').to(g.V('r1'))
g.V('a4').addE('has').to(g.V('r2'))
g.V('a5').addE('has').to(g.V('r2'))
And here is how I got the asset.scanDate
(from
vertex) and repo.assetAge
(to
vertex) properties:
gremlin> g.V().hasLabel("asset").as("a").out("has").as('b').select('a', 'b').by('scanDate').by('assetAge')
==>{a=2020-09-20, b=10}
==>{a=2020-09-12, b=9}
==>{a=2020-09-21, b=10}
==>{a=2020-09-14, b=9}
==>{a=2020-09-10, b=10}
I would like the results to be like the following:
==>{id=a2, scanDate=2020-09-20, assetAge=10}
==>{id=a4, scanDate=2020-09-12, assetAge=9}
==>{id=a3, scanDate=2020-09-21, assetAge=10}
==>{id=a5, scanDate=2020-09-14, assetAge=9}
==>{id=a1, scanDate=2020-09-10, assetAge=10}
Any help is greatly appreciated; thanks.
You can accomplish this using the project() step within Gremlin like below
g.V().
hasLabel("asset").as("a").
out("has").
project('id', 'a', 'b').
by(select('a').id()).
by(select('a').values('scanDate')).
by(values('assetAge'))