Gremlin / Bulbflow: how to get an integer result out of execute()

540 views Asked by At

Sorry if this question is too stupid to ask... I am a newbie on Python+Django+Bulbs+Neo4j.

I am attempting --without success-- to get an integer produced by g.gremlin.execute() while using Python+Django shell, as detailed below.

First, the query in Neo4j's Gremlin console:

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

What I intend to do it to get this result in Python+Django shell, passing it to a variable, as tried below:

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

I can't get any further from now on.

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

Could anyone please tell me what am I doing wrong? Many thanks!

1

There are 1 answers

0
espeed On BEST ANSWER

Raw result data is going to be in the Result object's raw attribute:

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

NOTE: result.data returns an element's property data, so it will be empty unless you are returning a vertex or edge, i.e. a node or relationship in Neo4j parlance.

See...

To see what Neo4j Server returned in the server response, you can output the Response headers and content:

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

And if you set the loglevel to DEBUG in Config, you'll be able to see what's being sent to the server on each request. When DEBUG is enabled, Bulbs also sets the raw attribute on the Response object (not to be confused with the raw attribute that is always set on the Result object). Response.raw will contain the raw server response:

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

See...

To turn off DEBUG, set the loglevel back to ERROR:

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

See...