I am now trying to learn how to connect to Neo4j server and run Cypher queries on it using Bulbflow from Python. And the thing I do not understand is the difference between two possibilities to connect to the neo4j server:
1) Graph
from bulbs.neo4jserver import Graph
g = Graph()
2) Neo4jClient
from bulbs.neo4jserver import Neo4jClient
client = Neo4jClient()
Could anyone please explain the conceptual difference here? And which way is it better to choose if I then want to execute (quite a lot of) Cypher queries against the server and ultimately in parallel?
PS: I do not have enough reputation to create a tag "bulbflow" for this question :)
Bulbs supports three different graph database servers -- Neo4j Server, Rexster, and now Titan.
Code specific to each backend server is contained within its own Python package (directory). You should see directories for: neo4jserver, rexster, titan:
Neo4jClient
is the low-level adapter for Neo4j Server -- you usually don't need to use this directly unless you are doing custom stuff -- use the high-level Graph class instead.See the Bulbs docs for...
The Bulbs Quickstart guide provides examples on using the
Graph
interface:However, your Bulbs objects always have access to the low-level client when you need it via the
_client
var.Lightbulb is an example app I created to show how to use and customize Bulbs models -- it's a Python blog engine that uses Git for source control and a graph database for persistence.
Lightbulb was originally designed for use with the free Neo4j Heroku Add On, but both Bulbs and Lightbulb make heavy use of Gremlin, and the Neo4j Heroko Add On no longer offers Gremlin in the free edition.
The Lightbulb model file contains a heavily customized
Entry
model and a customGraph
class -- theEntry
model makes use of the low-level client:As you can see in the
Entry
model, I have access to the low-level client via the_client
var, and I use it to get a Gremlin script from thescripts
library and then again to execute the Gremlin script.Here's the code for the
save_blog_entry
Gremlin script used by the Entry model:Unless you are doing something like customizing a model, you would normally use the
scripts
object and thegremlin
object stored on thegraph
object:See the Bulbs Neo4j Gremlin docs...
Likewise, when you want to execute a Neo4j Cypher query, use the
cypher
object stored on thegraph
object.There are three Cypher methods (unfortunately these aren't documented on the website yet):
g.cypher.query()
: Used when returning a list of nodes/relationships -- it will initialize them to objects.g.cypher.table()
: Used when returning Cypher table data.g.cypher.exectue()
: Used when returning arbitrary data -- it returns a genericResponse
object.You can look at the source code to see how they work...
Here are some examples of using the Cypher query() method (the query simply returns a relationship):
The query method automatically initializes elements to their type. If you created the element as a custom model, Bulbs will try to initialize it to the specific type, otherwise it will default to a generic
Vertex
orEdge
.Note that the Bulbs Cypher query() method returns an iterator.
You can loop over the iterator...
...or convert it to a list...
...or get the next item...
Please let me know if you have any more questions.