Gremlin / Bulbflow:如何从execute()中获取整数结果



对不起,如果这个问题太愚蠢了,问不出来...我是Python+Django+Bulbs+Neo4j的新手。

我试图 - 没有成功 - 在使用Python+Django shell时获取由g.gremlin.execute()生成的整数,如下所述。

首先,Neo4j的Gremlin控制台中的查询:

gremlin> g.v(2).out
==> v[6]
==> v[4]
==> v[8]
==> v[7]
gremlin> g.v(2).out.count()
==> 4

我打算做什么来在 Python + Django shell 中获得这个结果,将其传递给一个变量,如下所示:

>>> from bulbs.neo4jserver import Graph
>>> from bulbs.model import Node,Relationship
>>> g = Graph()
>>> sc = " g.v(vertex_id).out.count()"
>>> params = dict(vertex_id = 2)
>>> val = g.gremlin.execute(sc,params)
>>> val
<bulbs.neo4jserver.client.Neo4jResponse object at 0x243cfd0>

从现在开始,我不能再进一步了。

>>> val.one()
<bulbs.neo4jserver.client.Neo4jResult object at 0x2446b90>
>>> val.one().data
>>> val.one().results
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Neo4jResult' object has no attribute 'results'

谁能告诉我我做错了什么?非常感谢!

原始结果数据将位于 Result 对象的 raw 属性中:

>>> from bulbs.neo4jserver import Graph
>>> from bulbs.model import Node,Relationship
>>> g = Graph()
>>> script = " g.v(vertex_id).out.count()"
>>> params = dict(vertex_id = 2)
>>> resp = g.gremlin.execute(script,params)
>>> result = resp.one()
>>> result.raw

注意:result.data返回元素的属性数据,因此除非您返回顶点或边,即 Neo4j 术语中的节点或关系,否则它将为空。

看。。。

  • https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L60
  • https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L88
  • https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L167

要查看 Neo4j 服务器在服务器响应中返回的内容,您可以输出Response标头和内容:

>>> from bulbs.neo4jserver import Graph
>>> from bulbs.model import Node,Relationship
>>> g = Graph()
>>> script = "g.v(vertex_id).out.count()"
>>> params = dict(vertex_id = 2)
>>> resp = g.gremlin.execute(script,params)
>>> resp.headers
>>> resp.content

如果将日志级别设置为 DEBUG in Config ,您将能够看到每个请求发送到服务器的内容。启用DEBUG后,Bulbs 还会在Response对象上设置 raw 属性(不要与始终在Result对象上设置的 raw 属性混淆)。 Response.raw将包含原始服务器响应:

>>> from bulbs.neo4jserver import Graph, DEBUG
>>> from bulbs.model import Node,Relationship
>>> g = Graph()
>>> g.config.set_logger(DEBUG)
>>> script = " g.v(vertex_id).out.count()"
>>> params = dict(vertex_id = 2)
>>> resp = g.gremlin.execute(script,params)
>>> resp.raw

看。。。

  • https://github.com/espeed/bulbs/blob/master/bulbs/config.py#L70
  • https://github.com/espeed/bulbs/blob/master/bulbs/neo4jserver/client.py#L221
  • http://bulbflow.com/quickstart/#enable-debugging

要关闭 DEBUG ,请将日志级别设置回 ERROR

>>> from bulbs.neo4jserver import ERROR
>>> g.config.set_logger(ERROR)

看。。。

  • http://bulbflow.com/quickstart/#disable-debugging

最新更新