0

I am new to Gremlin and am trying to convert a SQL query to Gremlin. I have two vertex types, labeled host and repo, and here is the Gremlin script to create the vertices and the edges:

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'))

I would like to write down a query in Gremlin that does the same thing as the bellow SQL query:

SELCET *
FROM asset a
JOIN repo r ON a.repoId = r.repoId
WHERE a.scanDate >= CURDATE() - INTERVAL (r.assetAge + 1) DAY

I have so far tried the bellow code in python:

from datetime import datetime, timedelta
from gremlin_python.process.traversal import gte

d = datetime.today() - timedelta(days=10) # here I have hard coded the days
traversal = g.V().hasLabel("asset").has("scanDate", gte(d))
traversal.valueMap().toList()

But I do not know how I can pass the repo.assetAge from the mapped repo vertices to the days parameter in timedelta(). Any help is really appreciated. Thanks.

Malgi
  • 680
  • 1
  • 7
  • 20
  • I answered your prior question but I see you have since deleted that question. https://stackoverflow.com/questions/63998978/gremlin-select-vertices-where-the-conditional-join-uses-the-property-of-a-mappe You cannot pass the results of a query into a Python function within the query unless you use a lambda. A lot of graph databases do not support lambda injection for security reasons. As I mentioned in my other answer, using numerical dates and the `math` step is one possibility. Another is to make two queries to the graph. – Kelvin Lawrence Sep 22 '20 at 16:30
  • Sorry @KelvinLawrence if I deleted that question; I thought the question was not framed correctly, and so that's why I did not receive good answers. I am trying to figure out how I can do it using two queries. Thanks. – Malgi Sep 22 '20 at 17:55

0 Answers0