Groovy 脚本无法将边添加到 Gremlin 图中

Groovy script fails to add edges to a Gremlin graph

我正在尝试使此脚本能够检索文件中存在的所有边缘,以及从用户到电影的评分。

new File('ratings.dat').eachLine{
    line ->

    components = line.split('::');

    userId = components[0].toInteger();
    movieId = components[1].toInteger();

    g.V().has('userId', userId).as('o').V().has('movieId', movieId).addE('rated').to('o');
}

如果我在这个闭包中添加一些调试打印,我可以看到所有信息都正确地加载到我的变量中,但是在执行结束时我计算了我的图中的边数并且它增加了当它应该是多个时,只有 1。一些调查表明,有效添加到图中的边是最后读取的。可能出了什么问题?

你永远不会执行你的遍历。您的代码应如下所示:

new File('ratings.dat').eachLine { def line ->
    def (userId, movieId) = line.split('::')*.toInteger()
    g.V().has('userId', userId).as('o').
      V().has('movieId', movieId).
      addE('rated').to('o').iterate()
}