使用gremlin的航班连接

使用gremlin的航班连接,gremlin,tinkerpop,tinkerpop3,Gremlin,Tinkerpop,Tinkerpop3,我是小精灵的新手。我有一个以机场为节点,以航班为边,以到达时间和出发时间为边属性的图。现在,我正试图使用tinkerpop 3.1来获得1次中途停留、2次中途停留、3次中途停留的连接列表,给定起点机场、目的地机场和出发时间。我得到了它的工作,为1停留使用下面的查询。 我很难用repeat和match来概括这个查询以查找n个连接。感谢您的帮助 示例图形脚本 graph = TinkerFactory.createModern() g = graph.traversal() g.addV('airp

我是小精灵的新手。我有一个以机场为节点,以航班为边,以到达时间和出发时间为边属性的图。现在,我正试图使用tinkerpop 3.1来获得1次中途停留、2次中途停留、3次中途停留的连接列表,给定起点机场、目的地机场和出发时间。我得到了它的工作,为1停留使用下面的查询。 我很难用repeat和match来概括这个查询以查找n个连接。感谢您的帮助

示例图形脚本

graph = TinkerFactory.createModern()
g = graph.traversal()
g.addV('airport').property('name','PDX').as('PDX').
  addV('airport').property('name','JFK').as('JFK').
  addV('airport').property('name','PHX').as('PHX').
  addV('airport').property('name','ORD').as('ORD').
  addV('airport').property('name','IAD').as('IAD').
  addE('flight').property('depTime',9).property('arrTime',10).from('PDX').to('JFK').
  addE('flight').property('depTime',10).property('arrTime',11).from('PDX').to('PHX').
  addE('flight').property('depTime',12).property('arrTime',13).from('PDX').to('ORD').
  addE('flight').property('depTime',13).property('arrTime',14).from('PDX').to('IAD').
  addE('flight').property('depTime',11).property('arrTime',14).from('JFK').to('IAD').
  addE('flight').property('depTime',10).property('arrTime',12).from('PHX').to('IAD').
  addE('flight').property('depTime',14).property('arrTime',15).from('ORD').to('IAD').iterate()


Gremlin query


g.V().has("airport","name","PDX").outE().has("depTime",gt(6)).
match(
__.as('e1').values('arrTime').as('e1Arr'),
__.as('e1').outV().as('v1'),
__.as('e1').inV().as('v2'),
__.as('v2').outE().as('e2'),
__.as('e2').values('depTime').as('e2Dep')).
where('e2Dep',gt('e1Arr')).select('e2').inV().as("v3").has("airport","name","IAD").
select("v1","e1","v2","e2","v3").by("name").by(valueMap()).by("name").by(valueMap()).by("name");

我没有图表来测试它,但是这个查询应该可以做到:

g.V().has("airport","name","PDX").outE().has("depTime", gt("09:30")).as("e").inV().
 emit().
   repeat(outE().filter(project("a","b").by("depTime").by(select(last, "e").by("arrTime")).
            where("a", gt("b"))).as("e").inV()).times(2).
 has("name", "IAD").path().by("name").by(valueMap())
如果数据集是相对静态的,我建议创建快捷方式,例如预计算
route
顶点,它们与所有站点都有连接。例如:

g.V().has("airport","name","PDX").as("a0").
  V().has("airport","name","JFK").as("a1").
  V().has("airport","name","IAD").as("a2").
  addV("route").property("from", "PDX").
                property("to", "IAD").
                property("stops", 2).
                property("depTime", 9).
                property("arrTime", 14).as("r").
  addE("stop").from("r").to("a0").property("n", 0).
  addE("stop").from("r").to("a1").property("n", 1).
  addE("stop").from("r").to("a2").property("n", 2).iterate()
这些管线顶点将允许您快速查找任何长度的管线。第一次查询的结果可用于创建管线顶点;但是,由于它正在超时,您要么增加超时+内存,要么将其作为OLAP查询运行。后者是首选,但需要在查询中进行一些调整:

gremlin> g.V().has("airport","name","PDX").outE("flight").has("depTime", gt(9)).as("e").
           values("depTime").as("d").select("e").inV().
           emit().
             repeat(outE("flight").filter(project("a","b").by("depTime").by(select(last, "d")).
                      where("a", gt("b"))).as("e").
                      values("depTime").as("d").select(last, "e").inV()).times(2).
           has("name", "IAD").path()
==>[v[0],e[13][0-flight->8],13,e[13][0-flight->8],v[8]]
==>[v[0],e[12][0-flight->6],12,e[12][0-flight->6],v[6],e[16][6-flight->8],14,e[16][6-flight->8],v[8]]

我没有图表来测试它,但是这个查询应该可以做到:

g.V().has("airport","name","PDX").outE().has("depTime", gt("09:30")).as("e").inV().
 emit().
   repeat(outE().filter(project("a","b").by("depTime").by(select(last, "e").by("arrTime")).
            where("a", gt("b"))).as("e").inV()).times(2).
 has("name", "IAD").path().by("name").by(valueMap())
如果数据集是相对静态的,我建议创建快捷方式,例如预计算
route
顶点,它们与所有站点都有连接。例如:

g.V().has("airport","name","PDX").as("a0").
  V().has("airport","name","JFK").as("a1").
  V().has("airport","name","IAD").as("a2").
  addV("route").property("from", "PDX").
                property("to", "IAD").
                property("stops", 2).
                property("depTime", 9).
                property("arrTime", 14).as("r").
  addE("stop").from("r").to("a0").property("n", 0).
  addE("stop").from("r").to("a1").property("n", 1).
  addE("stop").from("r").to("a2").property("n", 2).iterate()
这些管线顶点将允许您快速查找任何长度的管线。第一次查询的结果可用于创建管线顶点;但是,由于它正在超时,您要么增加超时+内存,要么将其作为OLAP查询运行。后者是首选,但需要在查询中进行一些调整:

gremlin> g.V().has("airport","name","PDX").outE("flight").has("depTime", gt(9)).as("e").
           values("depTime").as("d").select("e").inV().
           emit().
             repeat(outE("flight").filter(project("a","b").by("depTime").by(select(last, "d")).
                      where("a", gt("b"))).as("e").
                      values("depTime").as("d").select(last, "e").inV()).times(2).
           has("name", "IAD").path()
==>[v[0],e[13][0-flight->8],13,e[13][0-flight->8],v[8]]
==>[v[0],e[12][0-flight->6],12,e[12][0-flight->6],v[6],e[16][6-flight->8],14,e[16][6-flight->8],v[8]]

如果您添加了一个小样本图作为可以在Gremlin控制台中运行的脚本,这将有所帮助。例如,请参阅,如果您添加了一个小样本图作为可以在Gremlin控制台中运行的脚本,这将有所帮助。例如,请参阅@stephen Thank you。我添加了一个示例图形脚本。当我使用limit(n)时,此查询有效。我有一张图表,上面有一百万条边连接着不同的机场。当我对此查询执行count()时,它会超时。看起来路径步骤是一个昂贵的步骤()。有没有更好的方法跟踪路径?我甚至尝试添加重复数据消除、simplePath和条件,以删除再次访问相同机场的情况,但查询仍然超时(等待5分钟)。我已更新我的答案,向您展示如何提高查询性能。您使用的是哪种图形数据库?谢谢Daniel。我使用的是DSE图5.1。我同意预计算。我试着在OLAP模式下运行它。它给了我一个错误“在GraphComputer上访问的路径元素id不能超过一个”。有什么好办法吗?@stephen,Daniel如果您有任何建议,我们将不胜感激。有什么方法可以在gremlin查询中传递多线程提示吗?没有,这将是底层图形数据库的任务。@stephen谢谢。我添加了一个示例图形脚本。当我使用limit(n)时,此查询有效。我有一张图表,上面有一百万条边连接着不同的机场。当我对此查询执行count()时,它会超时。看起来路径步骤是一个昂贵的步骤()。有没有更好的方法跟踪路径?我甚至尝试添加重复数据消除、simplePath和条件,以删除再次访问相同机场的情况,但查询仍然超时(等待5分钟)。我已更新我的答案,向您展示如何提高查询性能。您使用的是哪种图形数据库?谢谢Daniel。我使用的是DSE图5.1。我同意预计算。我试着在OLAP模式下运行它。它给了我一个错误“在GraphComputer上访问的路径元素id不能超过一个”。有什么好办法吗?@stephen,Daniel如果您有任何建议,我们将不胜感激。有什么方法可以在gremlin查询中传递多线程提示吗?没有,这将是底层图形数据库的任务。