1

我正在尝试计算边缘中间性。该查询在 gremlin 控制台上运行良好,但在 gremlin-python 中不起作用。

g.V().as_("v").
  repeat(identity().as_("src").
         bothE().as_("e").
         bothV().as_("v").
         where(neq("src")).
         simplePath()).
    emit().
  filter(project("x","y","z").
           by(select(first, "v")).
           by(select(last, "v")).
           by(select("v").count(local)).as_("triple").
         coalesce(select("x","y").as_("a").
                  select("triples").unfold().as_("t").
                  select("x","y").
                  where(eq("a")).
                  select("t"),
                  store("triples")).
         select("z").as_("length").
         select("triple").
         select("z").
         where(eq("length"))).
   select('e').
   unfold().
   groupCount()

错误是:TypeError: unhashable type: 'dict'

如果我将其更改为 vertex-betweenness,那么它就可以正常工作。我觉得的问题是如何在 python 中检索边缘,它是一张地图。当我进行分组计数时,它还会创建一个映射,其中键作为边缘,值作为计数。在 python 中,键本身不能是映射,因此会引发此错误。

如何解决这个问题?还请解释如何select(all, 'e')在 gremlin-python 中使用。

4

1 回答 1

1

您遇到了 gremlinpython的限制dict之一,即 Gremlin 可以返回Python 中不存在的值。您需要将这些键转换为可以在 Python 中作为键存在的东西,同时保留键包含的信息。我没有您的数据或输出样本,但我设计了以下内容作为演示:

gremlin> g.V().both().elementMap().groupCount().unfold()
==>{id=5, label=software, name=ripple, lang=java}=1
==>{id=2, label=person, name=vadas, age=27}=1
==>{id=4, label=person, name=josh, age=32}=3
==>{id=3, label=software, name=lop, lang=java}=3
==>{id=1, label=person, name=marko, age=29}=3
==>{id=6, label=person, name=peter, age=35}=1

使用 adict作为键,这在 python 中不起作用,我们会得到与你现在得到的相同的错误。有许多选项可用于将这个结果改造成 python 可以使用的东西,但这里有一个简单的选项,只是为了让你思考你可能会做什么:

gremlin> g.V().both().elementMap().groupCount().unfold().map(union(select(keys),select(values)).fold())
==>[[id:5,label:software,name:ripple,lang:java],1]
==>[[id:2,label:person,name:vadas,age:27],1]
==>[[id:4,label:person,name:josh,age:32],3]
==>[[id:3,label:software,name:lop,lang:java],3]
==>[[id:1,label:person,name:marko,age:29],3]
==>[[id:6,label:person,name:peter,age:35],1]

在上面,我将 a 解构dict为一list对。现在您在客户端知道每个结果都是服务器端的一个条目,dict其中第一个值是键,第二个是值。

于 2020-02-28T12:55:51.537 回答